创建存储过程phpmyadmin

时间:2018-04-24 10:47:29

标签: mysql sql

SQL说我在第3行附近有语法错误。请帮助

我的目标是计算药物价格的总和

这是来自phpmyadmin的我的SQL代码:

CREATE PROCEDURE spMEDICATION_FEE(IN PatientID CHAR(9))

BEGIN

DECLARE Sum2 INT;
DECLARE Sum3 INT;

SET Sum2 = (
    SELECT SUM(MPRICE)
    FROM USES_EXAM INNER JOIN MEDICATION ON USES_EXAM.MID = MEDICATION.MID
    WHERE PID_OUT = PatientID);

SET Sum3 = (
    SELECT SUM(MPRICE)
    FROM USES_TREAT INNER JOIN MEDICATION ON USES_TREAT.MID = MEDICATION.MID
    WHERE PID_IN = PatientID);

IF Sum2 IS NULL THEN
    SET Sum2 = 0;

IF Sum3 IS NULL THEN
    SET Sum3 = 0;

SELECT Sum2+ Sum3 AS 'Total fee';

END

1 个答案:

答案 0 :(得分:0)

如果您使用phpymadmin的SQL选项卡来运行此查询,则应定义分隔符而不是;。例如,试试这个:

DELIMITER $$

paste your create procedure code here

$$