13

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

SQL加:

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 ;

任何帮助是极大的赞赏!

4

2 回答 2

24

这在 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
于 2012-10-05T23:48:17.237 回答
2

医生似乎说

 [ IN | OUT | INOUT ] param_name type
于 2012-10-05T23:04:18.067 回答