Connecting Tech Pros Worldwide Forums | Help | Site Map

error in Stored procedure while using cursor

Newbie
 
Join Date: Jun 2008
Posts: 18
#1: Sep 18 '09
When I am trying to declare a cursor over a temproay table in my stored procedure I am getting some errors.
Below are the piece of code from my stored procedure in which I am getting error.

Expand|Select|Wrap|Line Numbers
  1. DECLARE GLOBAL TEMPORARY TABLE
  2.            SESSION.TEMP_TABLE(PERM_ID_TEMP VARCHAR(50));
  3.     INSERT INTO SESSION.TEMP_TABLE(PERM_ID_TEMP) VALUES(SELECT VARCHAR(elem, 50) FROM TABLE ( teimt00.elements(PERMISSIONS) ) AS t(elem));
  4.    DECLARE c1 CURSOR FOR 
  5.   SELECT PERM_ID_TEMP from SESSION.TEMP_TABLE;
I am getting following error
Expand|Select|Wrap|Line Numbers
  1. DB21034E  The command was processed as an SQL statement because it was not a 
  2. valid Command Line Processor command.  During SQL processing it returned:
  3. SQL0104N  An unexpected token "<cursor declaration>" was found following "".  
  4. Expected tokens may include:  "<SQL statement>".  LINE NUMBER=33.  
  5. SQLSTATE=4260
Pleae suggest what is the reason for this error.

vijay2082's Avatar
Newbie
 
Join Date: Aug 2009
Location: UK
Posts: 22
#2: Sep 18 '09

re: error in Stored procedure while using cursor


Hi,

Looks like a syntax error rather than any issue with the temporary table. Went fine in my case with the piece of code. Probably you
need to check at the specific location for the error and before the line for any missing clause etc (Expected tokens may include: "<SQL statement>". LINE NUMBER=33. )

D:\vijay>db2 connect to mydb user db2admin
Enter current password for db2admin:

Database Connection Information

Database server = DB2/NT 8.2.9
SQL authorization ID = DB2ADMIN
Local database alias = mydb


D:\vijay>db2 "DECLARE GLOBAL TEMPORARY TABLE SESSION.TEMP_TABLE(PERM_ID_TEMP VARCHAR(50))"
DB20000I The SQL command completed successfully.

D:\vijay>db2 "insert into SESSION.TEMP_TABLE(PERM_ID_TEMP) VALUES (select 'long character value for the column' from sysibm.sysdummy1)"
DB20000I The SQL command completed successfully.

D:\vijay>db2 "declare c1 cursor for select PERM_ID_TEMP from SESSION.TEMP_TABLE"
DB20000I The SQL command completed successfully.

D:\vijay>db2 +c select * from SESSION.TEMP_TABLE

PERM_ID_TEMP
--------------------------------------------------
long character value for the column


*************

SQL0104N An unexpected token "<token>" was found following
"<text>". Expected tokens may include:
"<token-list>".

Explanation:

A syntax error in the SQL statement or the input command string
for the SYSPROC.ADMIN_CMD procedure was detected at the specified
token following the text "<text>". The "<text>" field indicates
the 20 characters of the SQL statement or the input command
string for the SYSPROC.ADMIN_CMD procedure that preceded the
token that is not valid.

As an aid, a partial list of valid tokens is provided in the
SQLERRM field of the SQLCA as "<token-list>". This list assumes
the statement is correct to that point.

The statement cannot be processed.

User Response:

Examine and correct the statement in the area of the specified
token.

sqlcode : -104

sqlstate : 42601

***********

Cheers, Vijay
Newbie
 
Join Date: Jun 2008
Posts: 18
#3: Sep 22 '09

re: error in Stored procedure while using cursor


I checked the statement but couldn't find any error but still m getting same error in the line
[code]DECLARE C1 CURSOR FOR
SELECT PERM_ID_TEMP
from SESSION.TEMP_TABLE;[\code]
Reply