0

I am trying to set variable but I get Incorrect syntax near '='..

    DECLARE @name NVARCHAR(100) = 'Some name'
    DECLARE @id INT
    EXECUTE ('SET ' + @id + ' = (SELECT mm.mmID FROM dbo.measurem AS mm
        WHERE mm.placeName = ''' + @name + ''')')
    PRINT @id

If I try like this:

SET @id= (SELECT mm.mmID FROM dbo.measurem AS mm
          WHERE mm.placeName = @name)

I get must declare variable @name although I have it as my input store procedure param.

2
  • You appear to be using Microsoft SQL Server syntax, but you have tagged your question mysql. Which database are you really using? What does SELECT @@version; return? Commented Dec 5, 2021 at 4:58
  • I am using MS SQL. It is top much 5 tags so I had to put something. Commented Dec 5, 2021 at 10:09

1 Answer 1

0

For declaring variable, you can go through Declaring Variable in mysql However you need to do something like this to make it run <>db-fiddle

SELECT @my_name := 'Some name';
SELECT @id;
SET @s = CONCAT (
        'Select mm.mmID into @id From measurem as mm where 
    mm.placeName='
        ,''''
        ,@my_name
        ,''''
        );
PREPARE stmt
FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT @id;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.