If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > passing sql query as a parameter to db2 stored procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-30-11, 10:21
DushyantG DushyantG is offline
Registered User
 
Join Date: Mar 2011
Posts: 4
passing sql query as a parameter to db2 stored procedure

Hi,
Someone plz tell me how to pass an sql query to a stored proc. My stored proc is like:
Code:
MYPROC(IN IN_SQL VARCHAR(10000))
I am using EXECUTE IMMEDIATE to execute this query that has been passed as argument. As the input parameter would take an sql query it would contain single quotes around date fields and other char fields where assignments are done (eg.insert into myTable(col1,col2) values('abcdef', '01-01-11') ). So I cant call it like call MYPROC(" insert into myTable(col1,col2) values('abcdef', '01-01-11') ") , can I? Usually we pass strings to a proc in single quotes.
Reply With Quote
  #2 (permalink)  
Old 03-30-11, 10:41
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Try:
call MYPROC('insert into myTable(col1,col2) values(''abcdef'', ''01-01-11'') ')
Reply With Quote
  #3 (permalink)  
Old 03-30-11, 10:42
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You will have to escape the nested single-quotes by doubling them. Another option is to pass the string via parameter markers.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 03-31-11, 01:27
DushyantG DushyantG is offline
Registered User
 
Join Date: Mar 2011
Posts: 4
@tonkuma Thank u very much for your reply. I tried
Code:
call MYPROC('insert into myTable(col1,col2) values(''abcdef'', ''01-01-11'') ')
but it doesnt seem to work.

@stolze
Thank u very much. Escaping nested single quotes with single quotes worked brilliantly.
So this works:
Code:
call MYPROC(' insert into myTable(col1,col2) values(''abcdef'', ''01-01-11'') ')
Now Suppose the proc's definition is changed to
Code:
MYPROC(IN IN_SQL CLOB(1M))
In this case how would we call the proc , again if we have to pass a query? Would escaping the nested single quotes work again? Thanks in advance.
Reply With Quote
  #5 (permalink)  
Old 03-31-11, 04:00
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Quote:
Originally Posted by DushyantG View Post
Now Suppose the proc's definition is changed to
Code:
MYPROC(IN IN_SQL CLOB(1M))
In this case how would we call the proc , again if we have to pass a query? Would escaping the nested single quotes work again? Thanks in advance.
Same as above. No difference
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #6 (permalink)  
Old 03-31-11, 04:16
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Quote:
@tonkuma Thank u very much for your reply. I tried
Code:
call MYPROC('insert into myTable(col1,col2) values(''abcdef'', ''01-01-11'') ')
but it doesnt seem to work.

@stolze
Thank u very much. Escaping nested single quotes with single quotes worked brilliantly.
So this works:

Code:
call MYPROC(' insert into myTable(col1,col2) values(''abcdef'', ''01-01-11'') ')
DushyantG,
What error did you received from my code?

I saw only difference between my code and Stolze's was a blank before "insert ...".
Reply With Quote
Reply

Tags
passing arguments, stored procedure

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On