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

04-14-09, 01:40
|
|
Registered User
|
|
Join Date: Mar 2009
Posts: 73
|
|
|
Update or Insert DB2 stored procedure.
|
|
Hi I'm really new to db2 and in a scenario in our application where there is an update else insert situation on a grid.
Can someone help me write a stored procedure inside DB2 for this and help regarding db2 stored procedure syntax?
We are using db2 express C and .NET.
Regards, Mike.
|
|

04-14-09, 04:18
|
|
Registered User
|
|
Join Date: Mar 2009
Posts: 73
|
|
I tried to write a sproc but Im having some errors.
An unexpected token "END-OF-STATEMENT" was found following "URITYSRNO = CompSrNo". Expected tokens may
include: "<psm_semicolon>". LINE NUMBER=17. SQLSTATE=42601
--
CREATE PROCEDURE ADMINISTRATOR.FtUpdSecSh(
IN CompSrNo INTEGER,
IN TrtSrNo INTEGER,
IN CltCode INTEGER,
IN CltShare DECIMAL(10, 6),
IN CltComm DECIMAL(10, 6)
)
LANGUAGE SQL
BEGIN
UPDATE ADMINISTRATOR.UWFTRTYSCUTY
SET GENCLIENTCODE = CltCode,
GENCLIENTSHARE = CltShare,
GENCOMMPERCENT = CltComm
WHERE a.GENFORTREATYSRNO = TrtSrNo
AND GENSECURITYSRNO = CompSrNo ;
IF (ROW_COUNT = 0)
INSERT
INTO ADMINISTRATOR.UWFTRTYSCUTY(GENFORTREATYSRNO,
GENCOMMPERCENT,
GENCLIENTSHARE,
GENCLIENTCODE)
VALUES (TrtSrNo,
CltComm,
CltShare,
CltCode) ;
END IF;
END
|
|

04-14-09, 04:39
|
|
Registered User
|
|
Join Date: May 2003
Location: USA
Posts: 5,196
|
|
|
|
SP's and UDF's are tricky to create using the command processor because they use ";" terminators in the body of the SP, which is interpreted by the command processor as the end of the SQL statement (but the end of the SP is not until the very end). So do the following:
CREATE PROCEDURE ADMINISTRATOR.FtUpdSecSh(
IN CompSrNo INTEGER,
IN TrtSrNo INTEGER,
IN CltCode INTEGER,
IN CltShare DECIMAL(10, 6),
IN CltComm DECIMAL(10, 6)
)
LANGUAGE SQL
BEGIN
UPDATE ADMINISTRATOR.UWFTRTYSCUTY
SET GENCLIENTCODE = CltCode,
GENCLIENTSHARE = CltShare,
GENCOMMPERCENT = CltComm
WHERE a.GENFORTREATYSRNO = TrtSrNo
AND GENSECURITYSRNO = CompSrNo ;
IF (ROW_COUNT = 0)
INSERT
INTO ADMINISTRATOR.UWFTRTYSCUTY(GENFORTREATYSRNO,
GENCOMMPERCENT,
GENCLIENTSHARE,
GENCLIENTCODE)
VALUES (TrtSrNo,
CltComm,
CltShare,
CltCode) ;
END IF;
END@
db2 -td@ -vf <file-name-with-SP-text-above>
But I think you may need to use this syntax to check the rows updated:
GET DIAGNOSTICS rcount = ROW_COUNT;
IF (rcount= 0)
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
|
|

04-14-09, 05:21
|
|
Registered User
|
|
Join Date: Mar 2009
Posts: 73
|
|
Thanks for the reply Mr. Marcus. I saved the stored procedure inside a text file (.sql), but when I run the command on db2 express c command editor
its giving an error .
db2 -td@ -vf <C:\SP_CompShare.sql>
An unexpected token "db2" was found following "BEGIN-OF-STATEMENT". Expected tokens may include: "SELECT".
Quote:
|
Originally Posted by Marcus_A
SP's and UDF's are tricky to create using the command processor because they use ";" terminators in the body of the SP, which is interpreted by the command processor as the end of the SQL statement (but the end of the SP is not until the very end). So do the following:
CREATE PROCEDURE ADMINISTRATOR.FtUpdSecSh(
IN CompSrNo INTEGER,
IN TrtSrNo INTEGER,
IN CltCode INTEGER,
IN CltShare DECIMAL(10, 6),
IN CltComm DECIMAL(10, 6)
)
LANGUAGE SQL
BEGIN
UPDATE ADMINISTRATOR.UWFTRTYSCUTY
SET GENCLIENTCODE = CltCode,
GENCLIENTSHARE = CltShare,
GENCOMMPERCENT = CltComm
WHERE a.GENFORTREATYSRNO = TrtSrNo
AND GENSECURITYSRNO = CompSrNo ;
IF (ROW_COUNT = 0)
INSERT
INTO ADMINISTRATOR.UWFTRTYSCUTY(GENFORTREATYSRNO,
GENCOMMPERCENT,
GENCLIENTSHARE,
GENCLIENTCODE)
VALUES (TrtSrNo,
CltComm,
CltShare,
CltCode) ;
END IF;
END@
db2 -td@ -vf <file-name-with-SP-text-above>
But I think you may need to use this syntax to check the rows updated:
GET DIAGNOSTICS rcount = ROW_COUNT;
IF (rcount= 0)
|
|
|

04-14-09, 06:08
|
|
Registered User
|
|
Join Date: Mar 2009
Posts: 73
|
|
Ok I managed to find where to run the db2 command which I messed up previously , Now I'm running into another error which says on db2 CLP
DB21034E the command was processed as a sql statement because it was not a valid CLP command.During SQL processing it returned : SQL0104N
An unexpected token "-" was found following "BEGIN-OF STATEMENT".Expected tokens may include "SELECT" SQLState=42601
|
|

04-14-09, 06:55
|
|
Registered User
|
|
Join Date: Feb 2008
Location: Japan
Posts: 2,193
|
|
How about this on Command Editor?
!db2 .....
|
|

04-14-09, 23:42
|
|
Registered User
|
|
Join Date: Mar 2009
Posts: 73
|
|
its not working.
Quote:
|
Originally Posted by tonkuma
How about this on Command Editor?
!db2 .....
|
|
|

04-15-09, 00:00
|
|
Registered User
|
|
Join Date: May 2003
Location: USA
Posts: 5,196
|
|
Use the DB2 Command Window, not the DB2 Command Processor. Do not include the "<" or ">" in the file name. I am not sure if you can use the drive letter. It would be safer to cd to the path were the file aleady exists and do this:
cd c:\ (or whatever you need to do in Windows to get to were your file is located.)
db2 -td@ -vf SP_CompShare.sql
All of this is explained in the Command Reference manual. I think you should look at it.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
|
|

04-18-09, 03:03
|
|
Registered User
|
|
Join Date: Feb 2008
Location: Japan
Posts: 2,193
|
|
You can write your procedure body by one MERGE statement. So, BEGIN and END BIGIN are not necessary.
Then, you don't worry about statement terminator.
One terminator(";") is enough for end of procedure body(a MERGE statement) and end of procedure.
|
|

04-22-09, 00:42
|
|
Registered User
|
|
Join Date: Mar 2009
Posts: 73
|
|
|
|
| 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
|
|
|
|
|