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 > Errors in SQL Proc when initializing variable with SELECT INTO clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-10-04, 19:26
hiolgc hiolgc is offline
Registered User
 
Join Date: Mar 2004
Posts: 61
Errors in SQL Proc when initializing variable with SELECT INTO clause

I have the following SQL stored proc that won't compile. It will compile if I don't try and initialize the fiscalyr variable via the SELECT ... INTO fiscalyr statement.

The error is:

SQL0104N An unexpected token "<cursor declaration>" was found following "". Expected tokens may include: "<SQL statement>". LINE NUMBER=21. SQLSTATE=42601

The proc is:

CREATE PROCEDURE ADMIN.Proc1 (IN wk_end_date timestamp)
DYNAMIC RESULT SETS 1
LANGUAGE SQL
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN

DECLARE fiscalyr CHAR(7);

SELECT MAX(fiscal_year) INTO fiscalyr
FROM ADMIN.ACTIVITY_DATES
WHERE week_ending_date = wk_end_date;

DECLARE c1 CURSOR WITH RETURN TO CLIENT FOR

WITH CONTACTS AS (
SELECT loc_id, CONCAT (CONCAT (fname, ' '), lname) AS full_name
FROM ADMIN.LOC_CONTACT lc , ADMIN.CONTACT c
WHERE title_code = 'SAR' AND lc.contact_id = c.contact_id
AND fiscal_year = fiscalyr)

SELECT * FROM CONTACTS;

OPEN c1;

END P1

Anybody have any suggestions or know what the problem is?

Please note that everything after the DECLARE CURSOR statement works correctly if I hard-code the fiscalyr variable with a value. The stored proc compiles and returns the correct data when called from the client app. So the problem must be above in the SELECT INTO statement.

Thanks
Reply With Quote
  #2 (permalink)  
Old 05-10-04, 20:00
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Does this work ?

CREATE PROCEDURE ADMIN.Proc1 (IN wk_end_date timestamp)
DYNAMIC RESULT SETS 1
LANGUAGE SQL
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN

DECLARE fiscalyr CHAR(7);



DECLARE c1 CURSOR WITH RETURN TO CLIENT FOR

WITH CONTACTS AS (
SELECT loc_id, CONCAT (CONCAT (fname, ' '), lname) AS full_name
FROM ADMIN.LOC_CONTACT lc , ADMIN.CONTACT c
WHERE title_code = 'SAR' AND lc.contact_id = c.contact_id
AND fiscal_year = fiscalyr)

SELECT * FROM CONTACTS;

SELECT MAX(fiscal_year) INTO fiscalyr
FROM ADMIN.ACTIVITY_DATES
WHERE week_ending_date = wk_end_date;

OPEN c1;

END P1
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
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