473,394 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

DB2 Stored Proc help -- Urgent please

sri
My environment is DB2v9.1, Windows/AIX and I am trying to compile the
stored proc below and I am getting an error, "ERROR [42601] [IBM][DB2/
NT] SQL0104N An unexpected token "WITH" was found following "User_ID
= UsrID; ". Expected tokens may include: "SELECT". LINE
NUMBER=11. SQLSTATE=42601"
CREATE PROCEDURE TestProc(IN UsrID INT)
LANGUAGE SQL
SPECIFIC TestProc
DYNAMIC RESULT SETS 1

BEGIN
DECLARE UserID INT DEFAULT -1;
SELECT * FROM tab_user where User_ID = UsrID;
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
END

Thanks for your help

Apr 17 '07 #1
7 6119
You have an extraneous semi colon here:
SELECT * FROM tab_user where User_ID = UsrID; <----
before you finished your SQL statement.

Careful with those semi-colons! They are needed, but only in the
right places.

Phil Jackson
On Apr 16, 10:02 pm, sri <lssv...@hotmail.comwrote:
My environment is DB2v9.1, Windows/AIX and I am trying to compile the
stored proc below and I am getting an error, "ERROR [42601] [IBM][DB2/
NT] SQL0104N An unexpected token "WITH" was found following "User_ID
= UsrID; ". Expected tokens may include: "SELECT". LINE
NUMBER=11. SQLSTATE=42601"

CREATE PROCEDURE TestProc(IN UsrID INT)
LANGUAGE SQL
SPECIFIC TestProc
DYNAMIC RESULT SETS 1

BEGIN
DECLARE UserID INT DEFAULT -1;
SELECT * FROM tab_user where User_ID = UsrID;
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
END

Thanks for your help

Apr 17 '07 #2
sri wrote:
My environment is DB2v9.1, Windows/AIX and I am trying to compile the
stored proc below and I am getting an error, "ERROR [42601] [IBM][DB2/
NT] SQL0104N An unexpected token "WITH" was found following "User_ID
= UsrID; ". Expected tokens may include: "SELECT". LINE
NUMBER=11. SQLSTATE=42601"
CREATE PROCEDURE TestProc(IN UsrID INT)
LANGUAGE SQL
SPECIFIC TestProc
DYNAMIC RESULT SETS 1

BEGIN
DECLARE UserID INT DEFAULT -1;
SELECT * FROM tab_user where User_ID = UsrID;
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
END
Do you have a Sybase/SQL Server background?
In the SQL Standard you cannot just write a query and it turns into a
result set. You need to DECLARE a cursor WITH RETURN and OPEN that cursor.
E.g.
BEGIN
DECLARE cur WITH RETURN TO CLIENT FOR
SELECT * FROM tab_user WHERE User_ID = Usr_ID;
OPEN cur;
END

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 17 '07 #3
sri
On Apr 17, 7:34 am, Serge Rielau <srie...@ca.ibm.comwrote:
sri wrote:
My environment is DB2v9.1, Windows/AIX and I am trying to compile the
storedprocbelow and I am getting an error, "ERROR [42601] [IBM][DB2/
NT] SQL0104N An unexpected token "WITH" was found following "User_ID
= UsrID; ". Expected tokens may include: "SELECT". LINE
NUMBER=11. SQLSTATE=42601"
CREATE PROCEDURE TestProc(IN UsrID INT)
LANGUAGE SQL
SPECIFIC TestProc
DYNAMIC RESULT SETS 1
BEGIN
DECLARE UserID INT DEFAULT -1;
SELECT * FROM tab_user where User_ID = UsrID;
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
END

Do you have a Sybase/SQL Server background?
In the SQL Standard you cannot just write a query and it turns into a
result set. You need to DECLARE a cursor WITH RETURN and OPEN that cursor.
E.g.
BEGIN
DECLARE cur WITH RETURN TO CLIENT FOR
SELECT * FROM tab_user WHERE User_ID = Usr_ID;
OPEN cur;
END

--
Serge RielauDB2Solutions Development
IBM Toronto Lab- Hide quoted text -

- Show quoted text -
Thanks for your reply -- I just copied some snippets of the procedure
and hence this in complete code, I am sorry for that. This is exactly
what I am trying to acheive. I want to use Common Table Expressions in
stored procedure when I run the following statement

BEGIN
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
END

independently then it runs just fine but when I put the same into a
stored procedure then I get the following error message

ERROR [42601] [IBM][DB2/NT] SQL0104N An unexpected token "WITH" was
found following "1 BEGIN ". Expected tokens may include:
"SELECT". LINE NUMBER=9. SQLSTATE=42601
Thanks,

Sri
Apr 17 '07 #4
sri wrote:
On Apr 17, 7:34 am, Serge Rielau <srie...@ca.ibm.comwrote:
>sri wrote:
>>My environment is DB2v9.1, Windows/AIX and I am trying to compile the
storedprocbelow and I am getting an error, "ERROR [42601] [IBM][DB2/
NT] SQL0104N An unexpected token "WITH" was found following "User_ID
= UsrID; ". Expected tokens may include: "SELECT". LINE
NUMBER=11. SQLSTATE=42601"
CREATE PROCEDURE TestProc(IN UsrID INT)
LANGUAGE SQL
SPECIFIC TestProc
DYNAMIC RESULT SETS 1
BEGIN
DECLARE UserID INT DEFAULT -1;
SELECT * FROM tab_user where User_ID = UsrID;
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
END
Do you have a Sybase/SQL Server background?
In the SQL Standard you cannot just write a query and it turns into a
result set. You need to DECLARE a cursor WITH RETURN and OPEN that cursor.
E.g.
BEGIN
DECLARE cur WITH RETURN TO CLIENT FOR
SELECT * FROM tab_user WHERE User_ID = Usr_ID;
OPEN cur;
END

--
Serge RielauDB2Solutions Development
IBM Toronto Lab- Hide quoted text -

- Show quoted text -

Thanks for your reply -- I just copied some snippets of the procedure
and hence this in complete code, I am sorry for that. This is exactly
what I am trying to acheive. I want to use Common Table Expressions in
stored procedure when I run the following statement

BEGIN
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
END

independently then it runs just fine but when I put the same into a
stored procedure then I get the following error message
I think you meant to say BEGIN ATOMIC?
Now when you use BEGIN ATOMIC you also get no result back.
DB2 supports queries like as an easy way to achieve side-effects. Such
as sending email or so. In your case chances are DB2 will throw away the
query because it does nothing.
>
ERROR [42601] [IBM][DB2/NT] SQL0104N An unexpected token "WITH" was
found following "1 BEGIN ". Expected tokens may include:
"SELECT". LINE NUMBER=9. SQLSTATE=42601
Yep.. you need a DECLARE cursor statement here.

If I had it my way I'd raise a -104 for the standalone BEGIN ATOMIC as
well. Before my time. :-(

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 17 '07 #5
sri
On Apr 17, 9:13 am, Serge Rielau <srie...@ca.ibm.comwrote:
sri wrote:
On Apr 17, 7:34 am, Serge Rielau <srie...@ca.ibm.comwrote:
sri wrote:
My environment is DB2v9.1, Windows/AIX and I am trying to compile the
storedprocbelow and I am getting an error, "ERROR [42601] [IBM][DB2/
NT] SQL0104N An unexpected token "WITH" was found following "User_ID
= UsrID; ". Expected tokens may include: "SELECT". LINE
NUMBER=11. SQLSTATE=42601"
CREATE PROCEDURE TestProc(IN UsrID INT)
LANGUAGE SQL
SPECIFIC TestProc
DYNAMIC RESULT SETS 1
BEGIN
DECLARE UserID INT DEFAULT -1;
SELECT * FROM tab_user where User_ID = UsrID;
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
END
Do you have a Sybase/SQL Server background?
In the SQL Standard you cannot just write a query and it turns into a
result set. You need to DECLARE a cursor WITH RETURN and OPEN that cursor.
E.g.
BEGIN
DECLARE cur WITH RETURN TO CLIENT FOR
SELECT * FROM tab_user WHERE User_ID = Usr_ID;
OPEN cur;
END
--
Serge RielauDB2Solutions Development
IBM Toronto Lab- Hide quoted text -
- Show quoted text -
Thanks for your reply -- I just copied some snippets of the procedure
and hence this in complete code, I am sorry for that. This is exactly
what I am trying to acheive. I want to use Common Table Expressions in
stored procedure when I run the following statement
BEGIN
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
END
independently then it runs just fine but when I put the same into a
stored procedure then I get the following error message

I think you meant to say BEGIN ATOMIC?
Now when you use BEGIN ATOMIC you also get no result back.
DB2 supports queries like as an easy way to achieve side-effects. Such
as sending email or so. In your case chances are DB2 will throw away the
query because it does nothing.
ERROR [42601] [IBM][DB2/NT] SQL0104N An unexpected token "WITH" was
found following "1 BEGIN ". Expected tokens may include:
"SELECT". LINE NUMBER=9. SQLSTATE=42601

Yep.. you need a DECLARE cursor statement here.

If I had it my way I'd raise a -104 for the standalone BEGIN ATOMIC as
well. Before my time. :-(

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab- Hide quoted text -

- Show quoted text -
I am a newbie hence please bear with my ignorance. Here is the
complete stored proc

CREATE PROCEDURE TestProc(IN UsrID INT)
LANGUAGE SQL
SPECIFIC TestProc
DYNAMIC RESULT SETS 1
BEGIN
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
END
When i compile I get the error mentioned above. What am I doing wrong?

Apr 17 '07 #6
sri wrote:
When i compile I get the error mentioned above. What am I doing wrong?
CREATE PROCEDURE TestProc(IN UsrID INT)
LANGUAGE SQL
SPECIFIC TestProc
DYNAMIC RESULT SETS 1
BEGIN
DECLARE result CURSOR WITH RETURN FOR
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
OPEN result;
END

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 17 '07 #7
sri
On Apr 17, 4:29 pm, Serge Rielau <srie...@ca.ibm.comwrote:
sri wrote:
When i compile I get the error mentioned above. What am I doing wrong?

CREATE PROCEDURE TestProc(IN UsrID INT)
LANGUAGE SQL
SPECIFIC TestProc
DYNAMIC RESULT SETS 1
BEGIN
DECLARE result CURSOR WITH RETURN FOR
WITH TempView (clnt_id) AS
(SELECT clnt_id FROM clients)
SELECT CLNT_ID FROM TempView;
OPEN result;
END

Cheers
Serge
--
Serge RielauDB2Solutions Development
IBM Toronto Lab
I used your example as my base and built my stored proc, it works just
fine and I am getting the expected result. Thanks a lot for your help.

--Sri

Apr 17 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE...
3
by: Mo | last post by:
Hi, I have a webform which has vb.net code behind it and I would like it to submit the entries in the fields into a sql server db using a stored procedure. I have a central .vb file in my...
2
by: Mike Hutton | last post by:
I have a rather odd problem. I have a SP which uses temp. tables along the way, and then returns a table of results: CREATE PROCEDURE dbo.usp_myproc( @pNameList VARCHAR(6000) ) AS
14
by: Roy | last post by:
Apologies for the cross-post, but this truly is a two-sided question. Given the option of creating Looping statements within a stored proc of sql server or in the code-behind of an .net webpage,...
5
by: John | last post by:
Hi all, I'm sorry I'm reposting this but the original was urgent and I do need closure on this. I'm calling a stored proc which does 4 "selects" and then I populate a dataset looping through...
0
by: balaji krishna | last post by:
Hi, I need to handle the return set from COBOL stored procedure from my invoking Java program. I do not know, how many rows the stored proc SQL fetches.I have declared the cursor in that proc, but i...
0
by: Thamzyne | last post by:
Hi guys Scenario: There are 4 db owners: wh1, wh2, wh3, wh4. I have created a stored proc for wh2 and it works. Problem: I want to make my stored proc wise enough to be able to see and...
4
by: davinski | last post by:
Hello, it's been a while since my last post, hope everyone is fine :P I'm stuck with what seems to be a simple task, but I'm getting confused on how to complete this. Basically, I have been given...
0
by: mirandacascade | last post by:
Questions toward the bottom of the post. Situation is this: 1) Access 97 2) SQL Server 2000 3) The Access app: a) sets up pass-thru query b) .SQL property of querydef is a string, the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.