I have a simple script updating and showing a useraccount. (working with the management studio 2010) For preventing user errors I wanna use a variable in SQL (never did this before).
When reading tutorials it should be as simple as codesample below except I i'm getting an error message. Searching the web for people with the same error, I end up seeing very complex code with the same error. Can someone give me a clue.
DECLARE @Username nvarchar(256)
Set @Username = 'theUsername'
UPDATE aspnet_Membership
SET IsLockedOut = 0
WHERE UserId IN (SELECT U.UserId
FROM aspnet_Users as U inner join aspnet_Membership M on U.UserId = M.UserId
WHERE u.UserName = @Username)
GO
SELECT U.UserId, U.UserName, M.Password, M.IsLockedOut, U.LastActivityDate
FROM aspnet_Users as U inner join aspnet_Membership M on U.UserId = M.UserId
WHERE u.UserName = @Username
Msg 137, Level 15, State 2, Line 3 Must declare the scalar variable "@Username".
Scalar variables are used to represent individual fixed-size data objects, such as integers and pointers. Scalar variables can also be used for fixed-size objects that are composed of one or more primitive or composite types.
A scalar variable stores a value with no internal components. The value can change. A scalar variable declaration specifies the name and data type of the variable and allocates storage for it. The declaration can also assign an initial value and impose the NOT NULL constraint.
The scope of variable in Transact-SQL is limited by batch. Your script contains two batches separated by "GO"
There is a GO
inside your script, GO
divides your script into two batches so have to re-define all used variables after GO
, because the scope is limited to this batch.
BTW: I don't think, that this GO
is necessary, isn't it?
Thanks to @gbn and @alexm giving hint, that GO
separate statements into batches and not into transactions, see also http://msdn.microsoft.com/en-us/library/ms188037.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With