Sai Avinash Sai Avinash - 1 month ago 22
SQL Question

How to store value to a variable from SQL Query in Stored Proc in MS SQL Server

I am trying somw thing like inside a stored proc. But, I am getting error like incorrect

syntax near select statement.

declare @var int
@var=select max(id) from table1 where sname=@'XXX'

Answer Source

You are missing the SET keyword, and have an extra @ you dont need. Additionally, I have wrapped the Select command in brackets.

Try this:

DECLARE @var int
SET @var=(SELECT max(id) FROM table1 WHERE sname='XXX')