MySQL DECLARE / SELECT INTO语法错误?

时间:2014-10-10 15:33:11

标签: mysql

老实说,我不知道我在这里做错了什么。

Routine Name: procInsertName
Type: PROCEDURE
Parameters: IN Name VARCHAR 30 Charset

DECLARE tempNameID INT;

SELECT nameID
INTO tempNameID
FROM name
WHERE value = @Name;

IF tempNameID IS NULL THEN

    INSERT INTO name (value)
    VALUES (@Name);

END IF;

Is deterministic: unchecked
Definer: <blank>
Security type: DEFINER
SQL data access: CONTAINS SQL
Comment: <blank>

我出现了错误:

One or more errors have occurred while processing your request:

The following query has failed: "CREATE PROCEDURE `procInsertName`(IN `Name` VARCHAR(30)) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER DECLARE   tempNameID INT;

SELECT  nameID
INTO    tempNameID
FROM    name
WHERE   value = @Name;

IF tempNameID IS NULL THEN

    INSERT INTO name (value)
    VALUES (@Name);

END IF;"

MySQL said: #1064 - 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  tempNameID INT;

SELECT  nameID
INTO    tempNameID
FROM    name
WHERE   valu' at line 1

任何帮助将不胜感激,我花了最后30分钟在网上四处寻找,找不到任何东西。感谢。

1 个答案:

答案 0 :(得分:1)

我认为错误可能位于@符号中......您必须来自SQL Server后台; MySQL没有对变量使用@表示法,因此您的参数实际上只被称为Name。尝试将Name参数重命名为p_Name,然后将@Name的2个实例替换为存储过程正文中的p_Name

即。像这样:

CREATE PROCEDURE `procInsertName`(IN `p_Name` VARCHAR(30)) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER 
DECLARE   tempNameID INT;

SELECT  nameID
INTO    tempNameID
FROM    name
WHERE   value = p_Name;

IF tempNameID IS NULL THEN

    INSERT INTO name (value)
    VALUES (p_Name);

END IF;