| |
|
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.
|
 |

12-16-09, 16:33
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 5
|
|
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
|
|

12-16-09, 16:43
|
|
Registered User
|
|
Join Date: Jul 2009
Location: NY
Posts: 886
|
|
Quote:
Originally Posted by StewartFJ
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
|
|

12-16-09, 16:46
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 5
|
|
|
|
Told you I was new to this
|
|

12-16-09, 16:55
|
|
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
|
|

12-16-09, 16:56
|
|
Registered User
|
|
Join Date: Jul 2009
Location: NY
Posts: 886
|
|
Quote:
Originally Posted by StewartFJ
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
|
|

12-16-09, 17:20
|
|
Registered User
|
|
Join Date: Jul 2009
Location: NY
Posts: 886
|
|
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
|
|

12-16-09, 18:29
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 5
|
|
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
|
|

12-16-09, 18:35
|
|
:-)
|
|
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.
|
|

12-16-09, 18:43
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 5
|
|
Thank you so much I have been able to create the procedure, appreciate the help.
|
|

12-16-09, 18:49
|
|
Registered User
|
|
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
|
|
Quote:
Originally Posted by Lenny77
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
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|