Link to home
Start Free TrialLog in
Avatar of mchiber
mchiber

asked on

Passing variables into dynamic SQL

I am calling the EXEC SQL EXECUTE statement and passing in variables. However, i keep getting this error:
An unexpected token "EXEC SQL EXECUTE sql USING " was found
following "Year = ? + CHAR(?)';".  Expected tokens may include:  
"<psm_labellable_stmt>".  LINE NUMBER=1.  SQLSTATE=42601
Here is the statement inside a stored proc..



set sql = 'update CapitalAssetsActivity set Period=  CHAR(?),  Amount = ?';
set sql = sql || ' where SliceDimension1 =  ?  and AssetID = ? and Sequence = ?' ;
set sql = sql || ' and Year = ? + CHAR(?)';			
			
EXEC SQL EXECUTE sql USING : prevMonth, :tInBalance, :slice1, :assetId, :sequence, :prefix, :prevYear;

Open in new window

Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi mchiber,

Looking at the code snippet, I can't tell what the underlying language is.  It could be COBOL, it could be SQL, or and of several other languages.

EXEC SQL isn't needed if you're writing an SQL Stored Procedure or Function.


What language are you using?

Kent
Avatar of mchiber
mchiber

ASKER

Sorry, this is dynamic SQL for DB2

If the statement that you're creating is complete (doesn't need any variables substituted into it) use the EXECUTE IMMEDIATE statement to have it parsed and executed.

If you do need variables substituted into it (and it appears that you do) initiate the command with the PREPARE statement.  Then call the EXECUTE statement as you've done, passing the parameters to be inserted into the query.


Good Luck,
Kent
Avatar of mchiber

ASKER

Thanks Kent.
Can you give me the right syntax for Prepare? Also, the string variable containing the dynamically prepared statement, what data type can i use for it? A vargraphic? I am only know MS SQL and have no idea about DB2 syntax.
thanks
Prepare's one of the easier statements.  :)

    EXEC SQL
        PREPARE STMT1 FROM :SQL;
    END-EXEC.

    EXEC SQL
        EXECUTE STMT1 USING :prevMonth, :tInBalance, :slice1, :assetId, :sequence, :prefix, :prevYear;
    END-EXEC;


And there's actually a lot that can be done with Dynamic SQL.  Here's one of the IBM Redbooks that covers it pretty well:

  http://www.redbooks.ibm.com/abstracts/sg246418.html


Kent
Avatar of mchiber

ASKER

So here is what i have now and when i execute the create stored proc statement in the DB2 command Editor containing the following SQL in the body, i get an error:
An unexpected token "EXEC SQL" was found following "ear = ? ||
CHAR(?)';".  Expected tokens may include:  "<psm_statement>".  LINE NUMBER=1.  
SQLSTATE=42601


set ssql = 'update CapitalAssetsActivity set Period=  CHAR(?),  Amount = ?';
set ssql = ssql || ' where SliceDimension1 =  ?  and AssetID = ? and Sequence = ?' ;
set ssql = ssql || ' and Year = ? || CHAR(?)';	
			
EXEC SQL;
PREPARE v_stmt from ssql;
END-EXEC;

Open in new window

Ahha.  Didn't quite understand what you were doing.  :)

You don't need the EXEC SQL and END-EXEC statements in the command editor.  They only come into play if you're writing dynamic SQL within another (non-SQL) language like COBOL or C.


Kent
ASKER CERTIFIED SOLUTION
Avatar of mchiber
mchiber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Glad to help...

By the way, you can accept and answer and close questions any time that you want.  There's no need to put them into a delayed close.

And welcome to EE!

Kent