473,657 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Execute Immediate in cursor on ibm db2?

1 New Member
I'm having difficulties creating a SP in which I pass in a name of a table and query the SYS2 library to find out if it has an auto-increment field. If it does I query for the max value of that field in the table and then alter the table so the next used value is that result plus 1. This is for use when migrating production data over to development.

I'm not sure if it is possible to use "Execute Immediate" as part of a cursor declaration. I'm still fairly new to db2 in general, never mind for IBM. So any assistance would be greatly appreciated.

I'm getting the error on the Cursor declaration (line 10), but here is the exact error code I'm getting:

Expand|Select|Wrap|Line Numbers
  1. SQL State: 42601
  2. Vendor Code: -199
  3. Message: [SQL0199] Keyword IMMEDIATE not expected. Valid tokens: <END-OF-STATEMENT>. Cause . . . . . :   The keyword IMMEDIATE was not expected here.  A syntax error was detected at keyword IMMEDIATE.  The partial list of valid tokens is <END-OF-STATEMENT>. This list assumes that the statement is correct up to the unexpected keyword.  The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery  . . . :   Examine the SQL statement in the area of the specified keyword.  A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.
  4.  
And then finally here is my SP

Expand|Select|Wrap|Line Numbers
  1. /* Creating procedure DLLIB.SETNXTINC@ */
  2. CREATE OR REPLACE PROCEDURE DLLIB.SETNXTINC@(IN TABLE CHARACTER (10) ) LANGUAGE SQL CONTAINS SQL PROGRAM TYPE SUB CONCURRENT ACCESS RESOLUTION DEFAULT DYNAMIC RESULT SETS 0 OLD SAVEPOINT LEVEL COMMIT ON RETURN NO 
  3. SET @STMT1 = 'SELECT COLUMN_NAME ' || 
  4. 'FROM QSYS2.SYSCOLUMNS ' ||
  5. 'WHERE TABLE_SCHEMA =''DLLIB'' and table_name = ''' || TRIM(TABLE) || '''' ||
  6. 'AND HAS_DEFAULT = ''I'' ' ||
  7. 'OR HAS_DEFAULT = ''J'';';
  8.  
  9. DECLARE cursor1 CURSOR FOR
  10. EXECUTE IMMEDIATE @STMT1;
  11.  
  12. OPEN cursor1;
  13.  
  14. WHILE (sqlcode == 0){
  15. FETCH cursor1 INTO field;
  16. SET @STMT2 = 'ALTER TABLE DLLIB.' || TRIM(TABLE) || ''' ' ||
  17. 'ALTER COLUMN ' || TRIM(field) || ' RESTART WITH ( ' || 
  18.     'SELECT MAX(' || TRIM(field) || ') ' || 
  19.     'FROM   DLLIB.' || TRIM(TABLE) || ');';
  20. EXECUTE IMMEDIATE @STMT2;
  21. };;
  22.  
  23. /* Setting label text for DLLIB.SETNXTINC@ */
  24. LABEL ON ROUTINE DLLIB.SETNXTINC@ ( CHAR() )  IS 'Set the next auto-increment';
  25.  
  26. /* Setting comment text for DLLIB.SETNXTINC@ */
  27. COMMENT ON PARAMETER ROUTINE DLLIB.SETNXTINC@ ( CHAR() ) (TABLE IS 'Table from DLLIB' ) ;
  28.  
Mar 26 '15 #1
0 2301

Sign in to post your reply or Sign up for a free account.

Similar topics

2
22383
by: michi | last post by:
Hello there... Can anybody tell me what is the difference when I excecute a sql statement within pl sql with/without "execute immediate" statement Thanks Michi :)
4
28419
by: finlma | last post by:
I'm trying to run an EXECUTE IMMEDIATE within a PL/SQL if loop but it doesn't work for me. I'm trying to create a column conditionally but it doesn't work. It fails because there are apostrophes within the statement. How do I override the apostrophe? DECLARE cCount NUMBER; BEGIN SELECT count(*) INTO cCount FROM all_tab_columns WHERE owner = 'Owner'
2
7294
by: sultana | last post by:
I want to pass paramete in from caluse in a parameterized cursor & use this in Execute Immediate. Is it possible?
70
6168
kiss07
by: kiss07 | last post by:
Hi, i want 2 and 3 rd max salary in a table .i want a single query. please help.. Arun
1
2328
by: lakon15 | last post by:
Dear all, I'll try to convert from SQL server Store Procedure to DB2 Store Procedure. I've make SP under DB2 like this CREATE PROCEDURE GetSearchedRecords (pKeyWords VARCHAR(1000), pIsAll INTEGER, pIsExact INTEGER, pDateCondition INTEGER, pDateType INTEGER, ...
5
7770
by: umesh049 | last post by:
Hello, I want to delete all the date of all the table in a scheman. but i got error at execute immediate statement. can any body help me. thanks
3
6979
by: nghivo | last post by:
My environment DB2 9.1.4 on Sun OS I write a C embedded SQL to load data. I declare host vars as: EXEC SQL BEGIN DECLARE SECTION; SQL TYPE IS CLOB(599999) sqlStr; EXEC SQL END DECLARE SECTION;
3
6953
by: Rahul Babbar | last post by:
Hi, I have the following doubt. Suppose I use the execute immediate statement and the statement to be executed is a Select statement from the sysibm.sysdummy1 table which will always return me one row. Further, i need to capture the result of the execute Immediate into a
6
4428
by: Oliver | last post by:
I'm fairly new to DB2. I have been assigned to build a delete trigger that finds the data type of each of the table's fields so that the trigger can then build a string consisting of OLD values pre-wrapped in quote marks as needed. The deleted record's field values, all strung together as a single string, would then be inserted into a single archiving table (an architecture I inherited and cannot change). I've got the trigger doing...
2
11136
by: eeriehunk | last post by:
Hi All, I was going through DBMS_SQL package when I tried to research on Dynamic SQL. I couldn't understand the necessity for this package since we can use EXECUTE IMMEDIATE to execute any DDL inside a PLSQL procedure. Why make things complicated using the PARSE procedure supplied with the DBMS_SQL package. Regards, Aj
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.