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 > Help required on cursor and procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-16-09, 16:33
StewartFJ StewartFJ is offline
Registered User
 
Join Date: Dec 2009
Posts: 5
Question Help required on cursor and procedure

Hello,

I am new to DB2 SQL and am struggling trying to create a procedure, basically I need loop through entries in a table which contains rules required for calulcating territories of data, I then need to run a select statement based on the entry in the table and insert a value in a table if this query returns a result, this is the code but I am struggling getting the procedure created, any help would be great I am running out of time.

Here is the code....

create procedure Tags.ApplyTAG(in strUNID varchar(32), out strResult varchar(1))
LANGUAGE SQL
MODIFIES SQL DATA
DECLARE cr CURSOR FOR
Select TAGS.DAVDATATAGRULES.BuiltSQLQuery, TAGS.TAGS.TAGCODE From TAGS.DAVDATATAGRULES inner join tags.tags on TAGS.DAVDATATAGRULES.TAGNAME = tags.TAGS.TAGNAME for read only;
Open cr;
Fetch from cr Into :workonBuiltSQLQuery, :workonTAGCode, :workonTagName, :workonTagType;
IF EXISTS (SELECT #UNID FROM TABLE.ORGS where "#UNID"= strUNID and concat :workonBuiltSQLQuery)
Insert into TAGS.TAGMAP (ORGUNID, TAGCODE) values (strUNID, :workonTagCode)
end
Close cr;
end

Thanks
Fiona
Reply With Quote
  #2 (permalink)  
Old 12-16-09, 16:43
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs down

Quote:
Originally Posted by StewartFJ View Post
Hello,

I am new to DB2 SQL and am struggling trying to create a procedure, basically I need loop through entries in a table which contains rules required for calulcating territories of data, I then need to run a select statement based on the entry in the table and insert a value in a table if this query returns a result, this is the code but I am struggling getting the procedure created, any help would be great I am running out of time.

Here is the code....

create procedure Tags.ApplyTAG(in strUNID varchar(32), out strResult varchar(1))
LANGUAGE SQL
MODIFIES SQL DATA
DECLARE cr CURSOR FOR
Select TAGS.DAVDATATAGRULES.BuiltSQLQuery, TAGS.TAGS.TAGCODE From TAGS.DAVDATATAGRULES inner join tags.tags on TAGS.DAVDATATAGRULES.TAGNAME = tags.TAGS.TAGNAME for read only;
Open cr;
Fetch from cr Into :workonBuiltSQLQuery, :workonTAGCode, :workonTagName, :workonTagType;
IF EXISTS (SELECT #UNID FROM TABLE.ORGS where "#UNID"= strUNID and concat :workonBuiltSQLQuery)
Insert into TAGS.TAGMAP (ORGUNID, TAGCODE) values (strUNID, :workonTagCode)
end
Close cr;
end

Thanks
Fiona
Who can help you ?

Where is your Declare Section for host variables:
Quote:
:workonBuiltSQLQuery, :workonTAGCode, :workonTagName, :workonTagType;
???
How you make loop ? How you exit from loop ?

Lenny
Reply With Quote
  #3 (permalink)  
Old 12-16-09, 16:46
StewartFJ StewartFJ is offline
Registered User
 
Join Date: Dec 2009
Posts: 5
Told you I was new to this
Reply With Quote
  #4 (permalink)  
Old 12-16-09, 16:55
StewartFJ StewartFJ is offline
Registered User
 
Join Date: Dec 2009
Posts: 5
So any pointers

I thought the loop was carried out via the fetch from the cursor?

The more I search for information the more I get confused
Reply With Quote
  #5 (permalink)  
Old 12-16-09, 16:56
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Question

Quote:
Originally Posted by StewartFJ View Post
Told you I was new to this
You want to study DB2 on the Forums ?

Is this new way ?
Let me know I want to study Japanese. Maybe I can find the forum....

Lenny
Reply With Quote
  #6 (permalink)  
Old 12-16-09, 17:20
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb

You don't need any cursors.

Following query will do for you what do you need:

Code:
Insert into TAGS.TAGMAP (ORGUNID, TAGCODE) 
Select t1.BuiltSQLQuery, t2.TAGCODE 
From TAGS.DAVDATATAGRULES t1 inner join tags.tags  t2
on t1.TAGNAME = t2.TAGNAME
That's it ! You have to understand DB2 and all become easy....

Lenny
Reply With Quote
  #7 (permalink)  
Old 12-16-09, 18:29
StewartFJ StewartFJ is offline
Registered User
 
Join Date: Dec 2009
Posts: 5
Wink Thanks for the pointers

Are you able to see why this would not work, I get error -206 42703

create procedure Tags.ApplyTAG(in strUNID varchar(32))
LANGUAGE SQL
P1: BEGIN

declare workonBuiltSQLQuery varchar(200);
declare workonTAGCode integer;
declare tmpValue varchar(32);

DECLARE c0 CURSOR FOR
Select TAGS.DAVDATATAGRULES.BuiltSQLQuery, TAGS.TAGS.TAGCODE From TAGS.DAVDATATAGRULES
inner join tags.tags on TAGS.DAVDATATAGRULES.TAGNAME = tags.TAGS.TAGNAME for read only;

declare c1 cursor for v_stmt;

open c0;

Fetch from c0 Into workonBuiltSQLQuery, workonTAGCode;
while sqlcode <> 100 do

set v_sql ='SELECT "#UNID" FROM TAB.ORGS where '|| workonBuiltSQLQuery;
prepare v_stmt from v_sql;
open c2;
fetch from c2 into tmpValue;
while sqlcode <> 100 do
Insert into TAGS.TAGMAP(ORGUNID, TAGCODE) values (tmpValue, workonTagCode);
end while;
close c2;
end while;
close c0;
END P1
Reply With Quote
  #8 (permalink)  
Old 12-16-09, 18:35
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
SQL0206N may indicate that one of the database object identifiers (table name or column name) is invalid. Also, you seem to be using a lot of undeclared variables in your code.
Reply With Quote
  #9 (permalink)  
Old 12-16-09, 18:43
StewartFJ StewartFJ is offline
Registered User
 
Join Date: Dec 2009
Posts: 5
Thank you so much I have been able to create the procedure, appreciate the help.
Reply With Quote
  #10 (permalink)  
Old 12-16-09, 18:49
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by Lenny77 View Post
You have to understand DB2 and all become easy....
Actually, that's basic/standard SQL.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Tags
cursor, db2, procedure

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