0

Can anyone help me understand why when I run the following:

DELIMITER //
CREATE FUNCTION dbo.Profit (P_ProductID int, P_VendorID int, P_SellPrice decimal (12,4))
RETURNS decimal (12,4)
DETERMINISTIC
BEGIN
    DECLARE p_profit decimal (12,4)
    SET p_profit = P_SellPrice - Cost FROM VendorProduct
    WHERE ProductID = P_ProductID AND VendorID = P_VendorID
    RETURN p_profit
END //
DELIMITER;

I am getting the error: "[ERROR in query 1] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET p_profit = P_SellPrice - Cost FROM VendorProduct WHERE ProductID = P_Produc' at line 6 Execution stopped!"

I am using Sequel Pro, currently, and just trying to learn how to create functions.

natehoffy
  • 1
  • 2

2 Answers2

0

There should be a delimiter between separate commands, for instance, DECLARE... and SET... lines.

asd-tm
  • 3,381
  • 2
  • 24
  • 41
0

The following ended up working for me. I had some help from Raymond and then some other questions on this forum. Thanks to all who took a moment to respond and help here, putting me on the right path to solution:

 DELIMITER $$
    CREATE FUNCTION Profit (P_ProductID int, P_VendorID int, P_SellPrice decimal (12,4))
    RETURNS decimal (12,4)
    NOT DETERMINISTIC
    BEGIN
        DECLARE p_profit decimal (12,4);
        SELECT DISTINCTROW P_SellPrice - Cost INTO p_profit FROM VendorProduct 
        WHERE ProductID = P_ProductID AND VendorID = P_VendorID;
        RETURN p_profit;
    END$$
    DELIMITER ;
natehoffy
  • 1
  • 2