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 > Using Single Quotes in Dynamic SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-14-04, 14:40
mferrell mferrell is offline
Registered User
 
Join Date: Aug 2003
Posts: 13
Using Single Quotes in Dynamic SQL

Okay, here's the deal. I've got a stored procedure on UDB DB2 and I'm passing in a parm that is integer that needs to be compared with a field on a table that was defined as character, but is really numeric.

In the stored procedure, I'm building a dynamic SQL statement and I need to put single quote marks around the passed-in parm so it can be compared to the table.

For example:

Quote:
WHEN 'SubClass' THEN
IF in_sub > 0 AND in_class > 0 THEN
SET WHERESTMT = WHERESTMT || 'AND A.lot = ' || in_class ;
END IF;
In the above, in_class needs to have single quotes around it. I've tried using double quotes, triple-single quotes, double-single quotes (like in ASP), double quotes around single quotes and escape characters (\'), but no luck. It simply doesn't want to do it.

Any ideas how I can do it?
Reply With Quote
  #2 (permalink)  
Old 07-14-04, 15:03
J Petruk J Petruk is offline
Registered User
 
Join Date: Mar 2004
Location: Toronto, ON, Canada
Posts: 513
Quote:
Originally Posted by mferrell
Okay, here's the deal. I've got a stored procedure on UDB DB2 and I'm passing in a parm that is integer that needs to be compared with a field on a table that was defined as character, but is really numeric.

In the stored procedure, I'm building a dynamic SQL statement and I need to put single quote marks around the passed-in parm so it can be compared to the table.

For example:



In the above, in_class needs to have single quotes around it. I've tried using double quotes, triple-single quotes, double-single quotes (like in ASP), double quotes around single quotes and escape characters (\'), but no luck. It simply doesn't want to do it.

Any ideas how I can do it?
'' (ie. two single quotes) is supposed to do it, that's the way to escape a single quote in db2... that's not working???
__________________
--
Jonathan Petruk
DB2 Database Consultant
Reply With Quote
  #3 (permalink)  
Old 07-14-04, 15:25
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I suspect mferrell can't concatenate an integer to a string, aside from the quotes problem. The statement should look something like
Code:
... SET WHERESTMT = WHERESTMT || 'AND A.lot =''' || ltrim(char(in_class)) || ''''
Reply With Quote
  #4 (permalink)  
Old 07-14-04, 15:50
mferrell mferrell is offline
Registered User
 
Join Date: Aug 2003
Posts: 13
You guys were right on both counts. Simple mistakes, simple mistakes.

Here's the final version that worked:

Quote:
SET WHERESTMT = WHERESTMT || 'AND A.lot = ''' || cast(in_class as char(10)) || '''';
Much appreciated!
Reply With Quote
Reply

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