Error with Declare usage

138 Views Asked by At

I would like to learn how to use declare.

My query is like:

declare @iter int
set @iter = 1

error:

Msg 137, Level 15, State 1, Line 1 Must declare the scalar variable "@iter".

Please help!

3

There are 3 best solutions below

0
On

That can be done in one or two lines:

declare @iter int = 1
go 
-- or
declare @iter int 
set @iter = 1
1
On

You just need to put thw two instructions in different lines

declare @iter int
set @iter = 1
0
On

I execute these two line separately that's why it won't work cause before I execute set @iter = 1, SQL have cleared the declare command.