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 > Data Access, Manipulation & Batch Languages > ANSI SQL > stored procedure update

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-13-03, 14:03
izy izy is offline
Registered User
 
Join Date: Jul 2003
Posts: 10
stored procedure update

I redid the program, and I am able to get everything working and the right output, except updating the order_cost field. The order_cost update gives me errors and I have no idea what the problem is. Here is what I have so far:

ACCEPT prodno PROMPT 'Enter prodno: '
ACCEPT newprice PROMPT 'Enter new price: '


CREATE OR REPLACE PROCEDURE udpatePrice

AS

myprice PRODUCTS.PRICE%TYPE;





begin

select PRICE into myprice from PRODUCTS where PNO = &prodno;



end;

/

begin




update PRODUCTS set PRICE = &newprice
where PNO = &prodno;


update ODETAILS set COST = &newprice * QTY
where PNO = &prodno;


update ORDERS set ORDER_COST = SUM(COST)
where PNO = &prodno;





end;
/
Reply With Quote
  #2 (permalink)  
Old 07-14-03, 14:14
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool

Try this:

UPDATE ORDERS
SET ORDER_COST = (
SELECT SUM(COST) FROM ODETAILS
WHERE ODETAILS.ONO = ORDERS.ONO
AND ODETAILS.PNO = &prodno);
Reply With Quote
  #3 (permalink)  
Old 07-14-03, 23:58
izy izy is offline
Registered User
 
Join Date: Jul 2003
Posts: 10
This compiles fine, but i don't think it got updated. Also, i have added in an exception in case there is no product number. This compiles fine, but also, it doesn't print out the error. I have tried so many times, but I'm having no luck.

ACCEPT prodno PROMPT 'Enter prodno: '
ACCEPT newprice PROMPT 'Enter new price: '


CREATE OR REPLACE PROCEDURE udpatePrice

AS

myprice PRODUCTS.PRICE%TYPE;


NO_DATA_FOUND EXCEPTION;




begin

select PRICE into myprice from PRODUCTS where PNO = &prodno;
RAISE NO_DATA_FOUND;



end;

/

begin





update PRODUCTS set PRICE = &newprice
where PNO = &prodno;


update ODETAILS set COST = &newprice * QTY
where PNO = &prodno;


UPDATE ORDERS SET ORDER_COST = (SELECT SUM(COST) FROM ODETAILS
WHERE ODETAILS.ONO = ORDERS.ONO AND ODETAILS.PNO = &prodno);


EXCEPTION
when NO_DATA_FOUND then
dbms_output.put_line('Error: ' || SQLERRM);




end;
/


Quote:
Originally posted by LKBrwn_DBA
Try this:

UPDATE ORDERS
SET ORDER_COST = (
SELECT SUM(COST) FROM ODETAILS
WHERE ODETAILS.ONO = ORDERS.ONO
AND ODETAILS.PNO = &prodno);
Reply With Quote
  #4 (permalink)  
Old 07-15-03, 05:52
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally posted by izy
This compiles fine, but i don't think it got updated. Also, i have added in an exception in case there is no product number. This compiles fine, but also, it doesn't print out the error. I have tried so many times, but I'm having no luck.

ACCEPT prodno PROMPT 'Enter prodno: '
ACCEPT newprice PROMPT 'Enter new price: '


CREATE OR REPLACE PROCEDURE udpatePrice

AS

myprice PRODUCTS.PRICE%TYPE;


NO_DATA_FOUND EXCEPTION;




begin

select PRICE into myprice from PRODUCTS where PNO = &prodno;
RAISE NO_DATA_FOUND;



end;

/

begin





update PRODUCTS set PRICE = &newprice
where PNO = &prodno;


update ODETAILS set COST = &newprice * QTY
where PNO = &prodno;


UPDATE ORDERS SET ORDER_COST = (SELECT SUM(COST) FROM ODETAILS
WHERE ODETAILS.ONO = ORDERS.ONO AND ODETAILS.PNO = &prodno);


EXCEPTION
when NO_DATA_FOUND then
dbms_output.put_line('Error: ' || SQLERRM);




end;
/
Why do you create a procedure called udpatePrice that doesn't do anything, and never gets called?

Also, an UPDATE will never raise NO_DATA_FOUND, so that exception handler is redundant.

Your code can be reduced to:

Code:
ACCEPT  prodno  PROMPT 'Enter prodno: '
ACCEPT  newprice  PROMPT 'Enter new price: '

begin 

  update PRODUCTS set PRICE = &newprice 
  where PNO = &prodno;
 
  update ODETAILS set COST = &newprice * QTY
  where PNO = &prodno;

  UPDATE ORDERS SET ORDER_COST = (SELECT SUM(COST) FROM ODETAILS
  WHERE ODETAILS.ONO = ORDERS.ONO AND ODETAILS.PNO = &prodno);
 
end;
/
That last update looks wrong: it updates the order_cost of all orders to be the sum of associated odetails.cost for one specific product only. What if the order has other products also? Don't the cost of those products count towards the total?
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 07-15-03, 08:34
izy izy is offline
Registered User
 
Join Date: Jul 2003
Posts: 10
creating stored procedure updates

I am told to write a stored procedure that processes updates of PRICE, COST and ORDER_COST. I am told to include an exception handler for the exception raised, in case the product number is not in the PRODUCTS table.
Reply With Quote
  #6 (permalink)  
Old 07-15-03, 08:41
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: creating stored procedure updates

Quote:
Originally posted by izy
I am told to write a stored procedure that processes updates of PRICE, COST and ORDER_COST. I am told to include an exception handler for the exception raised, in case the product number is not in the PRODUCTS table.
In that case, move all the code into the procedure:

Code:
create or replace procedure update_price
  ( p_pno in products.pno%TYPE
  , p_newprice in number
  )
is
  myprice product.price%TYPE;
begin 

  select PRICE into myprice from PRODUCTS where PNO = p_pno;

  update PRODUCTS set PRICE = p_price 
  where PNO = p_pno;
 
  update ODETAILS set COST = p_price * QTY
  where PNO = p_pno;

  UPDATE ORDERS SET ORDER_COST = (SELECT SUM(COST) FROM ODETAILS
  WHERE ODETAILS.ONO = ORDERS.ONO AND ODETAILS.PNO = p_pno);

exception
  when no_data_found then
    raise_application_error(-20001,'Product not found' ); 
end;
/
Then to update a price:

begin
update_price( 'XYZ', 19.99 );
commit;
end;
/

(I'm still suspicious about that last update though).
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
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