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 > PREPARE statement with VARCHAR variable

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-01-10, 16:23
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
PREPARE statement with VARCHAR variable

I'm trying to EXECUTE a prepared statement that has a combination of INTEGER and VARCHAR values and am getting an error. Can you help.. thanks!


PID and PVAL are INTEGERS and PNAME is VARCHAR(24).
I have SET name = 'Warehouse' for example --

SET insertStatement = 'INSERT INTO ' || insertTable
|| ' (PID, PNAME, PVAL)'
|| ' VALUES ('|| CHAR(100) || ', ' || name || ', ' || CHAR(100) || ')';

PREPARE s1 FROM insertStatement;

I get this error :

SQL0206N "Warehouse" is not valid in the context where it is used.
SQLSTATE=42703



Btw, it works just fine when name is an INTEGER type and I execute this --

For example, SET name = 12;

SET insertStatement = 'INSERT INTO ' || insertTable
|| ' (PID, PNAME, PVAL)'
|| ' VALUES ('|| CHAR(100) || ', ' || CHAR(name) || ', ' || CHAR(100) || ')';

PREPARE s1 FROM insertStatement;

Also, the column type of PNAME in the insertTable is VARCHAR(24) in the first case and INTEGER in the second case.

Last edited by db2user24; 06-01-10 at 16:29.
Reply With Quote
  #2 (permalink)  
Old 06-01-10, 16:46
dbzTHEdinosaur dbzTHEdinosaur is offline
Registered User
 
Join Date: Jun 2007
Location: germany
Posts: 96
on the host (mainframe),
PNAME would have to be a group item consisting of:
2 bytes of binary providing the length of the varchar - warehouse - 9 in this case
and
a string item to contain the actual value 'warehouse'.
__________________
Dick Brenholtz, Ami in Deutschland
Reply With Quote
  #3 (permalink)  
Old 06-01-10, 16:58
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
Hi, not sure I understand what you mean.. I think I have a syntax error in the 1st INSERT statement.. but don't know how to fix it.
Reply With Quote
  #4 (permalink)  
Old 06-01-10, 18:14
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
Update -- the only way I could get it to work is by setting it up this way :

PID and PVAL are INTEGERS and PNAME is VARCHAR(24).
I have SET name = 'Warehouse' for example --

SET insertStatement = 'INSERT INTO ' || insertTable
|| ' (PID, PNAME, PVAL)'
|| ' VALUES ('|| CHAR(100) || ', ? , ' || CHAR(100) || ')';

PREPARE s1 FROM insertStatement;
EXECUTE s2 USING name;


But there must be a way to have 'name' directly in the insertStatement variable.. any thoughts?
Reply With Quote
  #5 (permalink)  
Old 06-01-10, 19:12
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
This must be a common mistake when using dynamic SQL.

Suppose, the value of insertStatement after SET statement.
If, the value of name was 'Warehouse',
insertStatement would be...
Code:
INSERT INTO <insertTable> (PID, PNAME, PVAL)
 VALUES (100        , Warehouse, 100        )
(where <insertTable> is the value of insertTable.)

So, you should include quotation marks around name, like...
SET insertStatement = 'INSERT INTO ' || insertTable
|| ' (PID, PNAME, PVAL)'
|| ' VALUES ('|| CHAR(100) || ', ''' || name || ''', ' || CHAR(100) || ')';
Reply With Quote
  #6 (permalink)  
Old 06-01-10, 23:12
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
Quote:
Originally Posted by tonkuma View Post
So, you should include quotation marks around name, like...
SET insertStatement = 'INSERT INTO ' || insertTable
|| ' (PID, PNAME, PVAL)'
|| ' VALUES ('|| CHAR(100) || ', ''' || name || ''', ' || CHAR(100) || ')';


Thank you! Actually I put two extra single quotes on each end ( not double quotes) and it works.. thanks much!
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