I have the following statement:
DECLARE @Nr_Karton int;
SELECT @Nr_Karton = ISNULL(MAX(Nr), 1000) FROM W_Karton;
SET @Nr_Karton = @Nr_Karton + 1;
INSERT INTO W_Karton (Container_ID, Nr, Beschrieb, CreationDate, Location)
VALUES ('1', @Nr_Karton, '', getDate(), 'Bösingen');
But I get the error:
[SQL] SELECT @Nr_Karton = ISNULL(MAX(Nr), 1000) FROM W_Karton
[Err] 42000 – [SQL Server]Must declare the scalar variable “@Nr_Karton”.
How to get rid of the error?
I did some playing with this. The fictional schema I created was:
Whilst it parsed and ran fine on my local 2008R2 box, the same code did not work when pasted into a SQL Fiddle.
However, if you remove all the semi-colons apart from the last one as per this SQL Fiddle you can see it seems to work fine!
I believe it shouldn’t make any difference, but if you would rather it worked and don’t care about the why, give it a try…