Question
· Nov 25

Problem by using String Var in embedded SQL where criteria

Hallo, I am new on programming Object Script.

My problem: How can I use a String variable in embedded SQL?

The below example does not work by using: namevar with ' ' 

set namevar = 'Ali'

&sql(DECLARE C2 CURSOR FOR SELECT name INTO :name FROM person where name = 'namevar')

Only below will work: person where name = 'Ali' 

Thanks in advance

Discussion (8)5
Log in or sign up to continue

To use a string variable within embedded SQL in ObjectScript, you should pass the value to the SQL statement as a host variable. This is done using the colon : to denote the variable in the SQL statement. Here's an example that aligns with your scenario:

SET namevar = "Ali"
&sql(
    DECLARE C2 CURSOR FOR
    SELECT name INTO :name FROM person WHERE name = :namevar
)

The colon before namevar indicates that this is a host variable, and the value of namevar will be dynamically passed into the SQL query at runtime. This eliminates the need to hard-code values like 'Ali' directly in the SQL query [1][2][3].

Sources:

@Enrico Parisi , with Global Masters, we get a few points for having an answer marked as correct on the community. People often only mark the first correct answer they see. So if the user who posted the question didn't see this thread before the AI bot's post appeared, it's somewhat likely that the bot's post will be marked as the correct answer even though a human user answered the question first. That's all. Seems just a little unfair to me.