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 > Cursor

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-15-10, 13:57
Rafaelcl Rafaelcl is offline
Registered User
 
Join Date: Jun 2010
Posts: 8
Cursor

I just start to use Db2 Cursor, I was trying to do a simple exemple,


DECLARE C1 Cursor for
select CHANGE_ID from CHANGES
ORDER BY START_DATE;

OPEN C1;

FETCH C1 INTO :ch_id;

But it give me this error, what's wrong??

FETCH C1 INTO :ch_id
SQL0104N An unexpected token "INTO" was found following "<identifier>".
Expected tokens may include: "END-OF-STATEMENT". SQLSTATE=42601

SQL0104N An unexpected token "INTO" was found following "<identifier>". Expected tokens may include: "END-OF-STATEMENT".
Reply With Quote
  #2 (permalink)  
Old 06-15-10, 14:00
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
What DB2 version and OS are you using?

Andy
Reply With Quote
  #3 (permalink)  
Old 06-15-10, 14:18
Rafaelcl Rafaelcl is offline
Registered User
 
Join Date: Jun 2010
Posts: 8
Db2 9
Windows XP
Reply With Quote
  #4 (permalink)  
Old 06-15-10, 14:22
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Syntactically it looks OK. Can you post the entire code? There maybe a problem elsewhere that is causing this.

Andy
Reply With Quote
  #5 (permalink)  
Old 06-15-10, 14:23
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Is this code in a stored procedure?

Andy
Reply With Quote
  #6 (permalink)  
Old 06-15-10, 14:58
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Question

Quote:
Originally Posted by Rafaelcl View Post
I just start to use Db2 Cursor, I was trying to do a simple exemple,


DECLARE C1 Cursor for
select CHANGE_ID from CHANGES
ORDER BY START_DATE;

OPEN C1;

FETCH C1 INTO :ch_id;

But it give me this error, what's wrong??

FETCH C1 INTO :ch_id
SQL0104N An unexpected token "INTO" was found following "<identifier>".
Expected tokens may include: "END-OF-STATEMENT". SQLSTATE=42601

SQL0104N An unexpected token "INTO" was found following "<identifier>". Expected tokens may include: "END-OF-STATEMENT".
Take a look on your code:
Code:
DECLARE C1 Cursor for
select CHANGE_ID from CHANGES
ORDER BY START_DATE;
Where you use START_DATE on SELECT ?

Also, if you are coding for Mainfraime you have to use:

Code:
EXEC SQL 
  DB2 statement
END-EXEC
Lenny

Last edited by Lenny77; 06-15-10 at 15:02.
Reply With Quote
  #7 (permalink)  
Old 06-15-10, 15:23
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by ARWinner View Post
Is this code in a stored procedure?

Andy
That's the real question here. If it is in external code (e.g. C/C++), things are fine and should work. Otherwise (in a stored procedure or on the DB2 console), you must not have a ':'. The colon is just an indicator in embedded SQL to tell the DB2 precompiler that a host variable follows.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 06-15-10, 16:03
dbzTHEdinosaur dbzTHEdinosaur is offline
Registered User
 
Join Date: Jun 2007
Location: germany
Posts: 96
Quote:
Where you use START_DATE on SELECT ?
don't need it. vsn7 or 8 and above on mainframe.
__________________
Dick Brenholtz, Ami in Deutschland
Reply With Quote
  #9 (permalink)  
Old 06-15-10, 16:22
Rafaelcl Rafaelcl is offline
Registered User
 
Join Date: Jun 2010
Posts: 8
It's in a stored procedure. What should I change??
Reply With Quote
  #10 (permalink)  
Old 06-15-10, 16:46
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Like this:

Code:
DECLARE CH_ID INT;

DECLARE C1 Cursor for
select CHANGE_ID from CHANGES
ORDER BY START_DATE;

OPEN C1;

FETCH C1 INTO CH_ID;
Andy
Reply With Quote
  #11 (permalink)  
Old 06-15-10, 17:46
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Removed because of my misunderstandings.

Last edited by tonkuma; 06-15-10 at 17:49. Reason: Removed because of my misunderstandings.
Reply With Quote
  #12 (permalink)  
Old 06-16-10, 16:13
Rafaelcl Rafaelcl is offline
Registered User
 
Join Date: Jun 2010
Posts: 8
I think it's some problem in my version. I try to execute the procedure I got on the IBM site , DB2 Universal Database, on the command editor. I create the staff table, and I haven't change anything on the code.

But still gives me errors like that:
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "END-OF-STATEMENT" was found following "ecords
INT DEFAULT 1". Expected tokens may include: "<psm_semicolon>". LINE
NUMBER=5. SQLSTATE=42601

DECLARE c2 CURSOR WITH RETURN FOR SELECT name, job, CAST(salary AS INTEGER) FROM staff WHERE salary > medianSalary ORDER BY salary
SQL0104N An unexpected token "RETURN" was found following "WITH". Expected
tokens may include: "HOLD". SQLSTATE=42601


Here is the code

CREATE PROCEDURE MEDIAN_RESULT_SET (OUT medianSalary DOUBLE)
RESULT SETS 1
LANGUAGE SQL
BEGIN
DECLARE v_numRecords INT DEFAULT 1;
DECLARE v_counter INT DEFAULT 0;

DECLARE c1 CURSOR FOR
SELECT CAST(salary AS DOUBLE)
FROM staff
ORDER BY salary;
DECLARE c2 CURSOR WITH RETURN FOR
SELECT name, job, CAST(salary AS INTEGER)
FROM staff
WHERE salary > medianSalary
ORDER BY salary;

DECLARE EXIT HANDLER FOR NOT FOUND
SET medianSalary = 6666;

SET medianSalary = 0;
SELECT COUNT(*) INTO v_numRecords
FROM STAFF;
OPEN c1;
WHILE v_counter < (v_numRecords / 2 + 1)
DO
FETCH c1 INTO medianSalary;
SET v_counter = v_counter + 1;
END WHILE;
CLOSE c1;
OPEN c2;
END e code:
Reply With Quote
  #13 (permalink)  
Old 06-16-10, 16:51
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
What command are you issuing to create the stored procedure?

Andy
Reply With Quote
  #14 (permalink)  
Old 06-16-10, 17:03
Rafaelcl Rafaelcl is offline
Registered User
 
Join Date: Jun 2010
Posts: 8
I am trying to run that on the command Editor
Reply With Quote
  #15 (permalink)  
Old 06-16-10, 17:20
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
The command editor has a default terminator of ";", but within a SP there are many semi-colons that do not signify the end of the SP. What you want is one SP source code with semi-colons after certain lines of code, and a different terminator at the very end (such as "@"). The when you create the SP you will need to specify a different terminator, for example:

db2 -td@ -f input_file.sql > output_file.out
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
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