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 > Parametrized VIEW or STORED PROCEDURE returning data set

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-05-09, 10:35
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
Question Parametrized VIEW or STORED PROCEDURE returning data set

I'm newbie in DB2 and i'm not able to realize following task.

I need view or stored procedure with parameters to return folowing SELECT from the database

SELECT col1, col2 FROM AAA
WHERE aValidFrom <= PAR1 AND aValidTo >= PAR2
AND ID NOT IN (SELECT ID FROM BBB WHERE bValidFrom <= PAR1 AND bValidTo >= PAR2)

The problem is i don't know
- how to return data set (SELECT) from stored procedure (if possible)
and / or
- how to pass parameters PAR1 and PAR2 to the view (if possible)

Does anybody have a solution, please?

Last edited by frun; 05-05-09 at 10:41.
Reply With Quote
  #2 (permalink)  
Old 05-05-09, 10:49
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
What DB2 version and OS?

Andy
Reply With Quote
  #3 (permalink)  
Old 05-05-09, 11:10
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
DB2 v9.5.0.808 (Express), OS Win 2003 Server

Sorry, I forgot it again. I have feeling the SQL should be version and OS independent.
Reply With Quote
  #4 (permalink)  
Old 05-05-09, 11:16
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Another idea might be a table UDF.

Change <data type 1>, <data type 2>, <data type 3> and <data type 4> with approprate data types.
Code:
CREATE FUNCTION udf_name(par1 <data type 1>, par2 <data type 2>)
RETURNS TABLE (col1 <data type 3>, col2 <data type 4>)
READS SQL DATA
DETERMINISTIC
NO EXTERNAL ACTION
RETUEN
SELECT col1, col2 FROM AAA
WHERE aValidFrom <= PAR1 AND aValidTo >= PAR2
AND ID NOT IN (SELECT ID FROM BBB WHERE bValidFrom <= PAR1 AND bValidTo >= PAR2)
;
Code:
SELECT col1, col2
  FROM TABLE( udf_name(xxxxx, yyyyy) ) q
;
Reply With Quote
  #5 (permalink)  
Old 05-05-09, 11:17
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Not for DB2 for LUW. There maybe newer features in newer versions, but the syntax in older version usually works in the newer.

What you need is something like this:

create procedure Myschema.MyProcedure(IN par1, IN par2)
BEGIN
DECLARE CURSOR1 CURSOR WITH RETURN TO CALLER FOR
SELECT col1, col2 FROM AAA
WHERE aValidFrom <= PAR1 AND aValidTo >= PAR2
AND ID NOT IN (SELECT ID FROM BBB WHERE bValidFrom <= PAR1 AND bValidTo >= PAR2) ;

OPEN CURSOR1; -- cursor left open for returning result set
END @

Look in the manual for the entire syntax for the create procedure statement.

Andy
Reply With Quote
  #6 (permalink)  
Old 05-05-09, 11:24
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
Quote:
Originally Posted by tonkuma
Another idea might be a table UDF.

Change <data type 1>, <data type 2>, <data type 3> and <data type 4> with approprate data types.
Code:
CREATE FUNCTION udf_name(par1 <data type 1>, par2 <data type 2>)
RETURNS TABLE (col1 <data type 3>, col2 <data type 4>)
READS SQL DATA
DETERMINISTIC
NO EXTERNAL ACTION
RETUEN
SELECT col1, col2 FROM AAA
WHERE aValidFrom <= PAR1 AND aValidTo >= PAR2
AND ID NOT IN (SELECT ID FROM BBB WHERE bValidFrom <= PAR1 AND bValidTo >= PAR2)
;
Code:
SELECT col1, col2
  FROM TABLE( udf_name(xxxxx, yyyyy) ) q
;
It seems good, but I'm affraid FUNCTION can't be mapped using LINQ to DB2 provider that I'm using from my .NET code.
Reply With Quote
  #7 (permalink)  
Old 05-06-09, 09:38
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
Quote:
Originally Posted by ARWinner
What you need is something like this:

create procedure
...
OPEN CURSOR1; -- cursor left open for returning result set
END @

Andy
Thank you, it works, but isn't it any resource leak to leave cursor opened? Or should I close it somehow?
Reply With Quote
  #8 (permalink)  
Old 05-06-09, 09:44
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I believe that the cursor gets closed when you have read all the rows from it.
There is no way to close it explicitly with SQL.

Andy
Reply With Quote
  #9 (permalink)  
Old 05-06-09, 10:09
rdutton rdutton is offline
Registered User
 
Join Date: Dec 2008
Posts: 76
The syntax is CLOSE CURSOR1;
Reply With Quote
  #10 (permalink)  
Old 05-06-09, 10:15
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Quote:
Originally Posted by rdutton
The syntax is CLOSE CURSOR1;
This will not work. The cursor is declared in the stored procedure. You cannot see it or reference its name. If you call the SP and then try to close the cursor like this you will get the error:

DB21028E The cursor "CURSOR1" has not been declared.

Andy
Reply With Quote
  #11 (permalink)  
Old 05-06-09, 10:31
rdutton rdutton is offline
Registered User
 
Join Date: Dec 2008
Posts: 76
Sorry - didn't look closely enough at the previous posts, didn't notice the declaration WITH RETURN TO CALLER. Responded to the statement out of context .

Last edited by rdutton; 05-06-09 at 10:34.
Reply With Quote
  #12 (permalink)  
Old 05-06-09, 11:44
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by frun
Thank you, it works, but isn't it any resource leak to leave cursor opened? Or should I close it somehow?
The cursor will be closed upon the next COMMIT.
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