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 > updating in stored procdure problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-13-11, 04:10
jemz jemz is offline
Registered User
 
Join Date: Aug 2011
Posts: 76
updating in stored procdure problem

Hello, I am creating the stored procedure which can update the lastname of employee using the for loop control,but when i update the lastname it doesn't change data lastname,can you help me please on this.Thank you in advance.


CREATE PROCEDURE SEARCH_ID (IN EMP_ID CHAR(6),
IN E_LASTNAME VARCHAR(30))




BEGIN

DECLARE EMPID CHAR(6);
DECLARE NOT_FOUND CONDITION FOR SQLSTATE '02000';
DECLARE CUR_SEARCH CURSOR WITH RETURN FOR

SELECT EMPNO
FROM EMPLOYEE;

OPEN CUR_SEARCH;

FOR ROW_VAR AS CUR_SEARCH CURSOR FOR

SELECT LASTNAME FROM EMPLOYEE
DO
FETCH CUR_SEARCH INTO EMPID;
IF EMPID=EMP_ID THEN
UPDATE EMPLOYEE
SET LASTNAME = E_LASTNAME
WHERE EMPNO = EMP_ID;
END IF;
END FOR;

SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT ='EMPLOYEE I.D DOES NOT EXIST';




END
Reply With Quote
  #2 (permalink)  
Old 08-15-11, 10:05
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
That seems to be a convoluted way to update just 1 row. Why not just use a simple update statement and a condition handler to let the caller know if the emp_id does not exists?

Andy
Reply With Quote
  #3 (permalink)  
Old 08-15-11, 12:23
jemz jemz is offline
Registered User
 
Join Date: Aug 2011
Posts: 76
Quote:
Originally Posted by ARWinner View Post
That seems to be a convoluted way to update just 1 row. Why not just use a simple update statement and a condition handler to let the caller know if the emp_id does not exists?

Andy

Hi Andy,

Thank you for the reply,I just want to try to use the for loop.can you help me please.
Reply With Quote
  #4 (permalink)  
Old 08-15-11, 16:42
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Why would you want to loop through all the rows of a table to update one row?

Andy
Reply With Quote
  #5 (permalink)  
Old 08-15-11, 18:00
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I also don't understand why you would want to use a loop. (Using procedural SQL where it is not necessary/helpful is a real problem in many situations.)

However, you may want to look at your cursor definitions. EMPID is a variable of type CHAR(6). If I'm not mistaken, then your loop fetches LASTNAME into that variable. The IF-condition will fail on that so that no UPDATE is done at all. I'd give all cursors a unique name to avoid such problems.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 08-16-11, 11:05
jemz jemz is offline
Registered User
 
Join Date: Aug 2011
Posts: 76
Quote:
Originally Posted by stolze View Post
I also don't understand why you would want to use a loop. (Using procedural SQL where it is not necessary/helpful is a real problem in many situations.)

However, you may want to look at your cursor definitions. EMPID is a variable of type CHAR(6). If I'm not mistaken, then your loop fetches LASTNAME into that variable. The IF-condition will fail on that so that no UPDATE is done at all. I'd give all cursors a unique name to avoid such problems.

hello,thank you for the reply,okay sir thank you for correcting my if statement,but sir how can i make the stored procedure that will check if the employee id is exist on the table then make an update and signal if the employee id does not exist.i really want to know how.please help me i am still beginner...Thank you in advance...more power to you.
Reply With Quote
  #7 (permalink)  
Old 08-16-11, 11:09
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I think I told you how to do it in my first post. Just create a condition handler if the row is not found that returns the SQLSTATE that you want and just issue the update statement.

Andy
Reply With Quote
  #8 (permalink)  
Old 08-16-11, 11:13
jemz jemz is offline
Registered User
 
Join Date: Aug 2011
Posts: 76
Quote:
Originally Posted by ARWinner View Post
Why would you want to loop through all the rows of a table to update one row?

Andy
Hello sir, I want to make a loop so that i can use signal and to display if the employee does not exist and make an update if the employee id is found...Okay sir i know that you know that my stored procedure is wrong.I just want to make a stored procedure that will update if the employee id is found and else if employee id does not exist and display employee does not exist...by the way sir i am using ibm data studio...can you help me how am i going to make this update and display if does not exist...hope you will help.more power to you.Thank you in advance.
Reply With Quote
  #9 (permalink)  
Old 08-16-11, 11:25
jemz jemz is offline
Registered User
 
Join Date: Aug 2011
Posts: 76
Quote:
Originally Posted by ARWinner View Post
I think I told you how to do it in my first post. Just create a condition handler if the row is not found that returns the SQLSTATE that you want and just issue the update statement.

Andy

Hi sir Andy,

sir can you help me on condition handler i really don't know the condition handler how to make that. That's why i find difficulty to do what you told me...can you please show some example sir?Thank you in advance.more power to you...
Reply With Quote
  #10 (permalink)  
Old 08-16-11, 11:32
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Have you tried reading the manual?

Andy
Reply With Quote
  #11 (permalink)  
Old 08-16-11, 11:38
jemz jemz is offline
Registered User
 
Join Date: Aug 2011
Posts: 76
Quote:
Originally Posted by ARWinner View Post
Have you tried reading the manual?

Andy

yes sir but i am confuse. this is the link sir correct me if i am wrong.
http://publib.boulder.ibm.com/infoce...d/c0009027.htm
Reply With Quote
  #12 (permalink)  
Old 08-16-11, 11:52
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
What DB2 version and OS are you using? What is so confusing?

Andy
Reply With Quote
  #13 (permalink)  
Old 08-16-11, 12:01
jemz jemz is offline
Registered User
 
Join Date: Aug 2011
Posts: 76
Quote:
Originally Posted by ARWinner View Post
What DB2 version and OS are you using? What is so confusing?

Andy

IBM® Data Studio Release 2.2.1.0,O.S(windows xp 2)...in using the handler types sir i am confuse i am looking at the example but still i could not get the idea.
Reply With Quote
  #14 (permalink)  
Old 08-16-11, 16:24
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I meant what DB2 version, not what Data Studio version. And what OS does DB2 run on--not Data Studio? I got that you are confused, but be specific as to what is confusing you. Just telling me you are confused over and over gets you no where.

Andy
Reply With Quote
  #15 (permalink)  
Old 08-17-11, 12:12
jemz jemz is offline
Registered User
 
Join Date: Aug 2011
Posts: 76
Quote:
Originally Posted by ARWinner View Post
I meant what DB2 version, not what Data Studio version. And what OS does DB2 run on--not Data Studio? I got that you are confused, but be specific as to what is confusing you. Just telling me you are confused over and over gets you no where.

Andy
DBV9.7.400.501, Okay sir here is my code.i am confuse in using the handler it's behavior the handler type and the conditions.where am i going to put the message "id no does not exist"?.please help me.more power to you always.

CREATE PROCEDURE SEARCH_ID (IN EMP_ID CHAR(6),
IN E_LASTNAME VARCHAR(30))




BEGIN

DECLARE EMPID CHAR(6);
DECLARE EXIT HANDLER FOR NOT FOUND;

UPDATE EMPLOYEE
SET LASTNAME=E_LASTNAME
WHERE EMPNO = EMP_ID;


END
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