0

I honestly have no idea what I'm doing wrong here.

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>

I'm coming out with an error of:

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

Any help would be appreciated, I've spent the last 30 minutes looking around online and can't find anything. Thanks.

2
  • You didn't initialize DECLARE to a value, that may be why. For example, DECLARE tempNameID INT unsigned DEFAULT 1; Commented Oct 10, 2014 at 15:36
  • Thanks @ryekayo, but sadly that did nothing. Commented Oct 10, 2014 at 15:43

1 Answer 1

1

I think the error might by lying in the @ symbol... You must be coming from an SQL Server background; MySQL doesn't use the @ notation for variables so your parameter is effectively just called Name. Try to rename your Name parameter to p_Name and then replace the 2 instances of @Name with p_Name in the body of your stored proc.

I.e. like this:

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;
Sign up to request clarification or add additional context in comments.

2 Comments

I've seen examples go both ways. Maybe it was dependent on the version. However, I changed up my procedure and this actually helped it as well. Thanks.
I think @Name is a legal identifier in MySQL (as I am also pretty sure I've seen it used, but don't know if its legal in all versions) but then you'd have to declare your parameter as @Name in the stored proc's signature as well (i.e. IN @Name VARCHAR(30)). That being said, I know that by convention the @ symbol is used for something else in MySQL (possibly local variables, but my memory fails me).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.