Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing variables into dynamic SQL

Posted on 2008-10-28
9
Medium Priority
?
633 Views
Last Modified: 2008-11-02
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

0
Comment
Question by:mchiber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 22821892
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
0
 

Author Comment

by:mchiber
ID: 22821948
Sorry, this is dynamic SQL for DB2
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 22822013

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
0
Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

 

Author Comment

by:mchiber
ID: 22822261
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
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 22822496
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
0
 

Author Comment

by:mchiber
ID: 22822704
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

0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 22822894
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
0
 

Accepted Solution

by:
mchiber earned 0 total points
ID: 22822977
Finally figured it out..Phew!

set ssql = 'update CapitalAssetsActivity set Period=  CHAR(?),  Amount = ?';
set ssql = ssql || ' where SliceDimension1 =  ?  and AssetID = ? and Sequence = ?' ;
set ssql = ssql || ' and Year = ? || CHAR(?)';			
			
			
PREPARE v_stmt from ssql;						
EXECUTE v_stmt USING prevMonth, tInBalance, slice1, assetId, sequence, prefix, prevYear;

Open in new window

0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 22824130

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
0

Featured Post

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question