创建过程MySQL

时间:2012-10-05 22:47:02

标签: mysql

我试图将一个过程从使用sql plus转换为mysql但是在它所说的第三行(W_IN IN NUMBER)中出现语法错误,并且它突出显示为语法错误。

SQL Plus:

CREATE OR REPLACE PROCEDURE PRC_CUS_BALANCE_UPDATE (W_IN IN NUMBER) AS
W_CUS NUMBER := 0;
W_TOT NUMBER := 0;
BEGIN
-- GET THE CUS_CODE
SELECT CUS_CODE INTO W_CUS 
FROM INVOICE
WHERE INVOICE.INV_NUMBER = W_IN;

-- UPDATES CUSTOMER IF W_CUS > 0
IF W_CUS > 0 THEN
    UPDATE CUSTOMER
    SET CUS_BALANCE = CUS_BALANCE + 
                (SELECT INV_TOTAL FROM INVOICE WHERE INV_NUMBER = W_IN)
    WHERE CUS_CODE = W_CUS;
END IF;
END;

MySQL的:

-- Trigger DDL Statements
DELIMITER $$

CREATE PROCEDURE prc_cus_balance_update (W_IN IN NUMBER) 
AS
W_CUS NUMBER = 0;
W_TOT NUMBER = 0;
BEGIN
-- GET CUS_CODE
SELECT CUS_CODE INTO W_CUS 
FROM INVOICE
WHERE INVOICE.INV_NUMBER = W_IN;

-- UPDATES CUSTOMER IF W_CUS > 0
IF W_CUS > 0 THEN
    UPDATE CUSTOMER
    SET CUS_BALANCE = CUS_BALANCE + 
                (SELECT INV_TOTAL FROM INVOICE WHERE INV_NUMBER = W_IN)
    WHERE CUS_CODE = W_CUS;
END IF;
END $$

DELIMITER ;

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:23)

这在MySQL 5.5.23中编译:

-- Trigger DDL Statements
DELIMITER $$

DROP PROCEDURE IF EXISTS prc_cus_balance_update;

CREATE PROCEDURE prc_cus_balance_update (IN W_IN INT UNSIGNED) 
BEGIN
DECLARE W_CUS INT UNSIGNED DEFAULT 0;
DECLARE W_TOT DOUBLE DEFAULT 0; -- NOT USED?
-- GET CUS_CODE
SELECT CUS_CODE INTO W_CUS 
FROM INVOICE
WHERE INVOICE.INV_NUMBER = W_IN;

-- UPDATES CUSTOMER IF W_CUS > 0
IF W_CUS > 0 THEN
    UPDATE CUSTOMER
    SET CUS_BALANCE = CUS_BALANCE + 
                (SELECT INV_TOTAL FROM INVOICE WHERE INV_NUMBER = W_IN)
    WHERE CUS_CODE = W_CUS;
END IF;
END $$

DELIMITER ;

当然,在这种情况下,不需要存储过程,因为以下查询将更快地(更容易理解)执行相同的功能:

UPDATE 
    CUSTOMER c
INNER JOIN
    INVOICE i ON i.CUS_CODE = c.CUS_CODE
SET 
    c.CUS_BALANCE = c.CUS_BALANCE + i.INV_TOTAL 
WHERE 
    i.INV_NUMBER = W_IN

答案 1 :(得分:2)

该文件似乎在说

 [ IN | OUT | INOUT ] param_name type