简体   繁体   中英

Creating Stored Procedures and joining tables

I am using a database called salesshort used mostly for learning MySQL. I am trying to create a stored procedure but for some odd reason when I run my code is not even creating the store procedure I think my syntax is wrong.

here are the instructions and my code.

Create a stored procedure 'gbSaleP' that returns the following 'order types': "we are loosing money" if the actual profit for each order (ie, OrderNumber)- is lower than or equal to zero; "good sale" if the difference between potential profit and actual profit is $2500 or lower; and "bad sale" if the difference between potential profit and actual profit is greater than $2500.


delimiter //

create procedure gbSaleP ( in orderNumber int(10), out SaleStatus varchar (40))
begin
    set gbsaleP = (select sum(o.quantityOrdered*p.MSRP - o.quantityOrdered*p.buyPrice) - abs(sum(o.quantityOrdered*o.priceEach - o.quantityOrdered*p.buyPrice))
from orderdetails as o
join products as p
using(productCode)
group by orderNumber
having porderNumber = orderName);

if gbSaleP <= 2500 then set SalesStatus = "good sale"
elseif gbSaleP => 2500 then set SaleStatus = "bad sale"
else set gbSaleP = "unknown"
end if;

end;
delimiter //

If your amount calculation script is right, you may use below.

DELIMITER //

CREATE procedure gbSaleP (IN orderNumber int(10), OUT SaleStatus varchar (40))

BEGIN

   DECLARE amount DECIMAL(20,2);
    set amount = (/*.......Correct SQL script*/);
    
   if amount <= 2500 then 
     set SaleStatus = 'good sale';
   elseif amount > 2500 then 
     set SaleStatus = 'bad sale';
   else 
     set SaleStatus = 'unknown';
   end if;

END; //

DELIMITER ;

You can test it with:

CALL `gbSaleP` (7, @`SaleStatus`);
SELECT @SaleStatus;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM