I'm working on SQL Server 2012. I try to declare two variables @max
and @rc
and use them in the insert statement:
DECLARE @max INT
SET @max = 100000
DECLARE @rc INT
SET @rc = 1
INSERT INTO dbo.Nums VALUES(1);
WHILE @rc * 2 <= @max
BEGIN
INSERT INTO dbo.Nums
SELECT n + @rc
FROM dbo.Nums
SET @rc = @rc * 2
END
INSERT INTO dbo.Nums
SELECT n + @rc
FROM dbo.Nums
WHERE n + @rc <= @max
GO
I get this error (four times):
[Error Code: 137, SQL State: 37000]
[Microsoft][ODBC SQL Server Driver][SQL Server]
Must declare the scalar variable "@rc".
When I try the following:
DECLARE @max INT
SET @max = 100000
DECLARE @rc INT
SET @rc = 1
INSERT INTO dbo.Nums VALUES(1);
WHILE @rc * 2 <= @max
BEGIN
DECLARE @rc INT
DECLARE @max INT
SET @max = 100000
INSERT INTO dbo.Nums
SELECT n + @rc
FROM dbo.Nums
SET @rc = @rc * 2
END
INSERT INTO dbo.Nums
SELECT n + @rc
FROM dbo.Nums
WHERE n + @rc <= @max
GO
I only get it once:
[Error Code: 137, SQL State: 37000]
[Microsoft][ODBC SQL Server Driver][SQL Server]
Must declare the scalar variable "@rc".
Can you help me figure out the problem?
Thanks
GO
to be consistent with native tools. The last thing you should be doing is removing semi-colons. – Aaron Bertrand