MySQL语法错误与声明

时间:2014-02-22 06:25:24

标签: mysql stored-procedures

我正在尝试在MySQL中创建一个事务过程,并且我一直收到在我的变量声明中出现的以下语法错误:

ERROR 1064(42000): 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 ';
DECLARE emp_duplicate INT DEFAULT 0;
DECLARE old_empnum INT DEFAULT 0;
DECL' at line 6

以下是我的程序:

DELIMITER $$


DROP PROCEDURE IF EXISTS new_employee$$

CREATE PROCEDURE new_employee(in_Fname VARCHAR(15),in_Lname VARCHAR(15),
in_hire DATE, in_DOB DATE, in_sal Numeric(7,2))

BEGIN

    DECLARE out_message VARCHAR;
    DECLARE emp_duplicate INT DEFAULT 0;
    DECLARE old_empnum INT DEFAULT 0;
    DECLARE out_empnum INT DEFAULT 0;
    DECLARE time_created DATETIME;
    DECLARE user_var VARCHAR;
    DECLARE age_var Int DEFAULT 0;

    START TRANSACTION;

    Select MAX(Empno)
    Into old_empnum
    From emp;

    Select COUNT(*)
    Into emp_duplicate
    From emp
    Where (in_Fname=Fname AND in_Lname=Lname AND in_hire=HireDate AND in_DOB=DOB);

    IF emp_duplicate=0 THEN
        SET out_message = 'New employee update successful';
        SET out_empnum = old_empnum + 1;    
        SET time_created = CURRENT_TIMESTAMP();
        SET user_var = USER();
        SET age_var = DATEDIFF(CURRENT_DATE,in_DOB);
        INSERT INTO emp(Empno,Fname,Lname,HireDate,DOB,SAL)
        VALUES(in_Fname,in_Lname,in_hire,in_DOB,in_sal);
        INSERT INTO log(Empno,DateCreated,Who)
        VALUES(out_empnum,time_created,user_var);
        SELECT out_message;
        SELECT Empno, Fname, Lname, DATEDIFF(CURRENT_DATE,DOB), HireDate
        From emp;
    ELSE
        SET out_message = 'Employee already exists';
        SELECT out_message;
    END IF;
    COMMIT;
END$$
DELIMITER ;

我正在使用cmd和记事本来执行我的代码(以防万一有用)。对于我的生活,我无法看到我的声明错误?????

2 个答案:

答案 0 :(得分:0)

您忘记了DECLARE out_message VARCHAR (30)DECLARE user_var VARCHAR(30);

的数据类型大小
DECLARE out_message VARCHAR (30); <------------- here
DECLARE emp_duplicate INT DEFAULT 0;
DECLARE old_empnum INT DEFAULT 0;
DECLARE out_empnum INT DEFAULT 0;
DECLARE time_created DATETIME;
DECLARE user_var VARCHAR(30);    <------------- here
DECLARE age_var Int DEFAULT 0;

在插入语句中,您缺少Empno

的值
INSERT INTO emp(Empno,Fname,Lname,HireDate,DOB,SAL)
        VALUES(in_Fname,in_Lname,in_hire,in_DOB,in_sal);

答案 1 :(得分:0)

您需要像这样定义VARCHAR大小

DECLARE out_message VARCHAR(20);

试试这个

BEGIN

    DECLARE out_message VARCHAR(20); // // varchar size here
    DECLARE emp_duplicate INT DEFAULT 0;
    DECLARE old_empnum INT DEFAULT 0;
    DECLARE out_empnum INT DEFAULT 0;
    DECLARE time_created DATETIME;
    DECLARE user_var VARCHAR(20); // varchar size here
    DECLARE age_var Int DEFAULT 0;