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 > Update or Insert DB2 stored procedure.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-14-09, 01:40
asp_crazy_guy asp_crazy_guy is offline
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.
Reply With Quote
  #2 (permalink)  
Old 04-14-09, 04:18
asp_crazy_guy asp_crazy_guy is offline
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
Reply With Quote
  #3 (permalink)  
Old 04-14-09, 04:39
Marcus_A Marcus_A is offline
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
Reply With Quote
  #4 (permalink)  
Old 04-14-09, 05:21
asp_crazy_guy asp_crazy_guy is offline
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)
Reply With Quote
  #5 (permalink)  
Old 04-14-09, 06:08
asp_crazy_guy asp_crazy_guy is offline
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
Reply With Quote
  #6 (permalink)  
Old 04-14-09, 06:55
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
How about this on Command Editor?
!db2 .....
Reply With Quote
  #7 (permalink)  
Old 04-14-09, 23:42
asp_crazy_guy asp_crazy_guy is offline
Registered User
 
Join Date: Mar 2009
Posts: 73
its not working.

Quote:
Originally Posted by tonkuma
How about this on Command Editor?
!db2 .....
Reply With Quote
  #8 (permalink)  
Old 04-15-09, 00:00
Marcus_A Marcus_A is offline
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
Reply With Quote
  #9 (permalink)  
Old 04-18-09, 03:03
tonkuma tonkuma is offline
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.
Reply With Quote
  #10 (permalink)  
Old 04-22-09, 00:42
asp_crazy_guy asp_crazy_guy is offline
Registered User
 
Join Date: Mar 2009
Posts: 73
thanks guys that helped
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