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 table name dynamically to insert during runtime.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-05-09, 02:23
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
passing table name dynamically to insert during runtime.

BEGIN

declare vtablename varchar(50);
declare vtablecount integer;

select count(1) into vtablecount from SYSCAT.TABLES Where TABSCHEMA='snerc';

IF vtablecount>0 Then

FOR RECORD_COUNT AS select TABNAME from SYSCAT.TABLES Where TABSCHEMA='snerc'
DO
SET vtablename=TABNAME;
insert into snerc.RECORD_COUNT(TABLE_NAME,NUMBER_OF_ROWS) SELECT vtablename ,count(1) from snerc.vtablename;
END FOR;
COMMIT;
END IF;
END

when i pass table name dynamically during runtime.

snerc.vtablename is an undefined name.
Reply With Quote
  #2 (permalink)  
Old 06-05-09, 03:31
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
'snerc' shld be in upper case .
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #3 (permalink)  
Old 06-05-09, 03:32
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
moreover you need to make the string and execute it
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #4 (permalink)  
Old 06-05-09, 03:36
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
value in the table is case sensitive.but the object names not case sensitive

declared a variable for statement.

set stmt='insert into snerc.RECORD_COUNT(TABLE_NAME,NUMBER_OF_ROWS) SELECT' || vtablename|| ',count(1) from' || 'snerc.'||vtablename
EXEC SQL stmt;

trying above.if works will post reply.

Last edited by laknar; 06-05-09 at 03:40.
Reply With Quote
  #5 (permalink)  
Old 06-05-09, 05:03
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
Quote:
Originally Posted by laknar
value in the table is case sensitive.but the object names not case sensitive.
Thats true , but in system tables they are always stored in upper case unless you create table in lower case enclosed in " "
..... so TABSCHEMA shld be in upper
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #6 (permalink)  
Old 06-05-09, 05:40
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
ok i changed.

how can i prepare a statement to run

SELECT COLUMN1 FROM SNERC.VTABLENAME WHERE COLUMN2='ABC'
Reply With Quote
  #7 (permalink)  
Old 06-05-09, 07:34
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You build a string that contains the statement and prepare the string using the PREPARE statement.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 06-05-09, 08:15
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
I prpared what you suggested.

when i pass the variable in where clause it is throwing error called.

S0022(-206)[IBM][CLI Driver][DB2/AIX64] SQL0206N "ABC" is not valid in the context where it is used. SQLSTATE=42703

because its taking within quates.

how can i overcome this error.
Reply With Quote
  #9 (permalink)  
Old 06-05-09, 09:06
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
Problem solved.
Thanks.
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