473,385 Members | 1,445 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,385 software developers and data experts.

Building Simple SQL Stored Procedure

I have just started working on DB2 which the syntax is a lot different.
I am trying to create a SQL stored procedure for generating reports.
I want to use temporary database which in this example it is trult not
needed but due to past experience it is key to developing good reports
for cliebt/server application. Here is my example which of course
dopes not work and documentation on the Internet is limited. Please if
anyone can clarify this procedure please let me know.

CREATE PROCEDURE LDBS_SP_SEL_MYTABLE ( IN BeginDate DATE,
IN EndDate DATE )
RESULT SETS 1
LANGUAGE SQL
------------------------------------------------------------------------
-- SQL Stored Procedure

------------------------------------------------------------------------
BEGIN

DECLARE GLOBAL TEMPORARY TABLE myResults AS
(SELECT
MYTABLE.LAST_NAME,
MYTABLE.FIRST_NAME,
MYTABLE.BIRTH_DATE
FROM ADMINISTRATOR.MYTABLE AS MYTABLE
WHERE MYTABLE.BIRTH_DATE >= BeginDate
AND MYTABLE.BIRTH_DATE < EndDate)
DEFINITION ONLY NOT LOGGED;

BEGIN
--Declare client cursor
DECLARE cursor1 CURSOR WITH RETURN TO CLIENT FOR
SELECT
LAST_NAME,
FIRST_NAME,
BIRTH_DATE
FROM SESSION.myResults;

--Cursor left open for client application
OPEN cursor1;
END;
END

Also I tried to use the INSERT INTO

BEGIN
DECLARE GLOBAL TEMPORARY TABLE myResults
LIKE ADMINISTRATOR.MYTABLE
ON COMMIT DELETE ROWS
NOT LOGGED
IN USERTEMPSPACE1;

INSERT INTO SESSION.myResults
SELECT
MYTABLE.LAST_NAME,
MYTABLE.FIRST_NAME,
MYTABLE.BIRTH_DATE
FROM ADMINISTRATOR.MYTABLE AS MYTABLE
WHERE MYTABLE.BIRTH_DATE >= BeginDate
AND MYTABLE.BIRTH_DATE < EndDate;

END

Also on the second code example I tried to show the result and am not
sure what is right.

-- Declare cursor
DECLARE cursor1 CURSOR WITH RETURN FOR
SELECT
MYRESULTS.LAST_NAME,
MYRESULTS.FIRST_NAME,
MYRESULTS.BIRTH_DATE
FROM SESSION.myResults AS MYRESULTS;

-- Cursor left open for client application
OPEN cursor1;

Or just

SELECT * FROM SESSION.myResults;

Both of these code block come up with errors. I have to develop a
standard for returning data which is readable in SQL format. I could
write one for other SQL engines which is a lot simpler but DB2 has
other standards.

Best Regards,

Bob Zagars

Mar 20 '06 #1
7 3697
rz*****@tampabay.rr.com wrote:
I have just started working on DB2 which the syntax is a lot different.
I am trying to create a SQL stored procedure for generating reports.
I want to use temporary database which in this example it is trult not
needed but due to past experience it is key to developing good reports
for cliebt/server application. Here is my example which of course
dopes not work and documentation on the Internet is limited. Please if
anyone can clarify this procedure please let me know.

CREATE PROCEDURE LDBS_SP_SEL_MYTABLE ( IN BeginDate DATE,
IN EndDate DATE )
RESULT SETS 1
LANGUAGE SQL
------------------------------------------------------------------------
-- SQL Stored Procedure

------------------------------------------------------------------------
BEGIN

DECLARE GLOBAL TEMPORARY TABLE myResults AS
(SELECT
MYTABLE.LAST_NAME,
MYTABLE.FIRST_NAME,
MYTABLE.BIRTH_DATE
FROM ADMINISTRATOR.MYTABLE AS MYTABLE
WHERE MYTABLE.BIRTH_DATE >= BeginDate
AND MYTABLE.BIRTH_DATE < EndDate)
DEFINITION ONLY NOT LOGGED;

BEGIN
--Declare client cursor
DECLARE cursor1 CURSOR WITH RETURN TO CLIENT FOR
SELECT
LAST_NAME,
FIRST_NAME,
BIRTH_DATE
FROM SESSION.myResults;

--Cursor left open for client application
OPEN cursor1;
END;
END

Also I tried to use the INSERT INTO

BEGIN
DECLARE GLOBAL TEMPORARY TABLE myResults
LIKE ADMINISTRATOR.MYTABLE
ON COMMIT DELETE ROWS
NOT LOGGED
IN USERTEMPSPACE1;

INSERT INTO SESSION.myResults
SELECT
MYTABLE.LAST_NAME,
MYTABLE.FIRST_NAME,
MYTABLE.BIRTH_DATE
FROM ADMINISTRATOR.MYTABLE AS MYTABLE
WHERE MYTABLE.BIRTH_DATE >= BeginDate
AND MYTABLE.BIRTH_DATE < EndDate;

END

Also on the second code example I tried to show the result and am not
sure what is right.

-- Declare cursor
DECLARE cursor1 CURSOR WITH RETURN FOR
SELECT
MYRESULTS.LAST_NAME,
MYRESULTS.FIRST_NAME,
MYRESULTS.BIRTH_DATE
FROM SESSION.myResults AS MYRESULTS;

-- Cursor left open for client application
OPEN cursor1;

Or just

SELECT * FROM SESSION.myResults;

Both of these code block come up with errors. I have to develop a
standard for returning data which is readable in SQL format. I could
write one for other SQL engines which is a lot simpler but DB2 has
other standards.

You have to combine the two attempts. The secret is in here:
DEFINITION ONLY
So DB2 will declare the temp for you but it will NOT do the inserts.
So you have to do them.
Assuming you want the result as a resultset you then open the cursor.

So:
DECLARE GLOBAL TEMPORARY .... AS SELECT ...
INSERT INTO SESSION.... AS SELECT...
BEGIN
DECLARE cur1 CURSOR WITH RETURN...
OPEN cur1;
END;

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 20 '06 #2
If DEFINITION ONLY Just declares the structure and then you can use
INSERT INTO to populate the temp table. This script should work but
does not and returns error. My table and overall structure is simple
but the syntax is evading me to start development.
CREATE PROCEDURE LDBS_SP_SEL_MYTABLE
( IN BeginDate DATE, IN EndDate DATE )
RESULT SETS 1
LANGUAGE SQL
------------------------------------------------------------------------
-- SQL Stored Procedure

------------------------------------------------------------------------
BEGIN
DECLARE GLOBAL TEMPORARY TABLE myResults AS
(SELECT
MYTABLE.LAST_NAME,
MYTABLE.FIRST_NAME,
MYTABLE.BIRTH_DATE
FROM ADMINISTRATOR.MYTABLE AS MYTABLE)
DEFINITION ONLY NOT LOGGED;

INSERT INTO SESSION.myResults
SELECT
MYTABLE.LAST_NAME,
MYTABLE.FIRST_NAME,
MYTABLE.BIRTH_DATE
FROM ADMINISTRATOR.MYTABLE AS MYTABLE
WHERE MYTABLE.BIRTH_DATE >= BeginDate
AND MYTABLE.BIRTH_DATE < EndDate;

-- Declare cursor
DECLARE cursor1 CURSOR WITH RETURN FOR
SELECT
MYRESULTS.LAST_NAME,
MYRESULTS.FIRST_NAME,
MYRESULTS.BIRTH_DATE
FROM SESSION.myResults AS MYRESULTS;

-- Cursor left open for client application
OPEN cursor1;
END

Bob Zagars
Senior Software Engineer

Mar 20 '06 #3
> CREATE PROCEDURE LDBS_SP_SEL_MYTABLE
( IN BeginDate DATE, IN EndDate DATE )
RESULT SETS 1
LANGUAGE SQL
BEGIN
DECLARE GLOBAL TEMPORARY TABLE myResults AS
(SELECT
MYTABLE.LAST_NAME,
MYTABLE.FIRST_NAME,
MYTABLE.BIRTH_DATE
FROM ADMINISTRATOR.MYTABLE AS MYTABLE)
DEFINITION ONLY NOT LOGGED;

INSERT INTO SESSION.myResults
SELECT
MYTABLE.LAST_NAME,
MYTABLE.FIRST_NAME,
MYTABLE.BIRTH_DATE
FROM ADMINISTRATOR.MYTABLE AS MYTABLE
WHERE MYTABLE.BIRTH_DATE >= BeginDate
AND MYTABLE.BIRTH_DATE < EndDate;
BEGIN -- Declare cursor
DECLARE cursor1 CURSOR WITH RETURN FOR
SELECT
MYRESULTS.LAST_NAME,
MYRESULTS.FIRST_NAME,
MYRESULTS.BIRTH_DATE
FROM SESSION.myResults AS MYRESULTS;

-- Cursor left open for client application
OPEN cursor1; END; END


DECLARE cursor is part of the header of a compound statement. It can't
be in the middle of the block.
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 20 '06 #4
That is different then SQL Server and Sybase T-SQL which a cursor can
be implied anywhere it just has to be opened and close plus DEALLOCATE
the cursor. So technically I could insert the DECLARE cursor at the
beginning before the INSERT INTO. Actually this works and I can
develop reports from this structure, I am just interested in how your
database store procedure work. On T-SQL a BEGIN and END is a set of
statements processed together which I can declare anywhere within.
Example

IF ...
BEGIN

...multiple statements including cursors...

END
ELSE...
BEGIN

...multiple statements

END

Anyway thanks for all your help. I can now develop some report using
your engine.

Bob Zagars
Senior Software Engineer

Mar 20 '06 #5
Bob Zagars wrote:
That is different then SQL Server and Sybase T-SQL which a cursor can
be implied anywhere it just has to be opened and close plus DEALLOCATE
the cursor. So technically I could insert the DECLARE cursor at the
beginning before the INSERT INTO. Actually this works and I can
develop reports from this structure, I am just interested in how your
database store procedure work. On T-SQL a BEGIN and END is a set of
statements processed together which I can declare anywhere within.

Same in DB2.
See my solution.
All you need to do is "push" a block. BEGIN..END can be added anywhere
within the SQL Procedure and it can be nested. To add local exception
handlers, variables, conditions and cursors.

BTW, there is an "open question" of mine on how this works in SQL
Server. Maybe you can clarify. :-)
As you have noted in DB2 you can specify WITH RETURN TO CALLER and WITH
RETURN TO CLIENT. The first returns the cursor to the invoker of the
stored procedure, which may be the client or a stored procedure. The
resultset can then be consumed by the invoking procedure.
The later returns the result set straight to the client.
It appears that "implicit" result set creation in T-SQL is always RETURN
TO CLIENT. So how does one pass a results set to a calling procedure in
T-SQL?

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 20 '06 #6
T-SQL examples

DECLARE @MyCursor CURSOR;
EXEC dbo.currency_cursor @currency_cursor = @MyCursor OUTPUT;
WHILE (@@FETCH_STATUS = 0)
BEGIN;
FETCH NEXT FROM @MyCursor;
END;
CLOSE @MyCursor;
DEALLOCATE @MyCursor;
GO

There is a CALLER statement when creating a procedure

CREATE PROCEDURE ssp_vendor_info_all
WITH EXECUTE AS CALLER

The first one is a way of walking through the result set from within a
stored procedure. The second I am not sure but does specify the CALLER
argument and might fit your needs. Thank you again for all the
information.

Best Regard,

Bob Zagars
Senior Software Engineer

Mar 20 '06 #7
Bob Zagars wrote:
T-SQL examples

DECLARE @MyCursor CURSOR;
EXEC dbo.currency_cursor @currency_cursor = @MyCursor OUTPUT;
WHILE (@@FETCH_STATUS = 0)
BEGIN;
FETCH NEXT FROM @MyCursor;
END;
CLOSE @MyCursor;
DEALLOCATE @MyCursor;
GO OK, but that does not work with implicit cursors, right?
You need to explicitly type:
SET @currency_cursor = FOR SELECT * FROM T;
inside the dbo.currency_cursor stored procedure.
There is a CALLER statement when creating a procedure
CREATE PROCEDURE ssp_vendor_info_all
WITH EXECUTE AS CALLER

Different thing, we call that "invoker's rights" in DB2, nothing to do
with result sets.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 20 '06 #8

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

Similar topics

5
by: Lili | last post by:
I'm having problems creating a simple stored procedure in DB2. Can someone help? Here is the screen dump when I tried to load the stored procedure. Thanks for any help. Create procedure...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
3
by: Nils Magnus Englund | last post by:
Hi, I've made a HttpModule which deals with user authentication. On the first request in a users session, it fetches data from a SQL Server using the following code: using (SqlConnection...
1
by: E.T. Grey | last post by:
I have been busting my nut over this for pretty much most of the day and it is driving me nuts. I posted this to an mySQL ng yesterday and I have not had any response (I'm pulling my hair out...
4
by: James | last post by:
How can I see/set the timeout for a postback? I have a button click event which loops through a DataSet and calls a stored procedure for every record in the dataset. This can be a very large...
1
by: peaceburn | last post by:
Hi, I'm gonna pull my hair in the coming days with these DB2 stored procedures. So the issue, let's assume a simple stored procedure like this : CREATE PROCEDURE MYSCHEMA.PROCEDURE1 ( )...
1
by: gregoryday | last post by:
I am battling to get a stored procedure to build: I have a stored procedure working in SQL Server 2000, and now need to port it to DB2. I will be accessing the stored procedure via VB.NET. The...
1
by: Ted | last post by:
In MS SQL I used the following to create a stored procedure. USE AdventureWorks; GO IF OBJECT_ID ( 'HumanResources.usp_My_Search', 'P' ) IS NOT NULL DROP PROCEDURE HumanResources.usp_My_Search;...
7
by: CSharper | last post by:
Yesterday I had a heated discussion with my colleagues on what is a data centric application and having business logic in sql. I have group of people who wants to include all the business logic in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.