SQL 不明白错误信息:必须声明标量变量“@Username”。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8739609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:51:50  来源:igfitidea点击:

Don't understand error message: Must declare the scalar variable "@Username".

sql

提问by Luuk Krijnen

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).

我有一个简单的脚本更新和显示用户帐户。(与 management studio 2010 合作)为了防止用户错误,我想在 SQL 中使用一个变量(以前从未这样做过)。

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".

消息 137,级别 15,状态 2,第 3 行 必须声明标量变量“@Username”。

回答by alexm

The scope of variable in Transact-SQL is limited by batch. Your script contains two batches separated by "GO"

Transact-SQL 中变量的范围受批处理的限制。您的脚本包含以“GO”分隔的两个批次

回答by rabudde

There is a GOinside your script, GOdivides your script into two batches so have to re-define all used variables after GO, because the scope is limited to this batch.

GO你的脚本里面有一个,GO把你的脚本分成两批,所以必须在 之后重新定义所有使用的变量GO,因为范围仅限于此批。

BTW: I don't think, that this GOis necessary, isn't it?

BTW:我不认为,这GO是必要的,不是吗?

Thanks to @gbn and @alexm giving hint, that GOseparate statements into batches and not into transactions, see also http://msdn.microsoft.com/en-us/library/ms188037.aspx

感谢@gbn 和@alexm 给出提示,GO将语句分成批次而不是事务,另见http://msdn.microsoft.com/en-us/library/ms188037.aspx

回答by ErikE

GOseparates batches and is a clientinstruction, not a serverone. So the server receives the second batch separately as a new query and in this the variable is not declared.

GO分离批次并且是客户端指令,而不是服务器指令。因此,服务器将第二批作为新查询单独接收,并且在此未声明变量。

If you try to do the following you will get an error, because the server does not understand the command GO:

如果您尝试执行以下操作,您将收到错误消息,因为服务器不理解该命令GO

DECLARE @SQL varchar(1000);
SET @SQL = 'PRINT ''hello'';
GO
PRINT ''goodbye'';';

The server won't split this into two batches, because batches are things submitted to the server by the client.

服务器不会将其拆分为两个批次,因为批次是客户端提交给服务器的东西。

回答by Amar Palsapure

Once you write GO, @Username is not available to SELECT query.

编写 GO 后,@Username 不可用于 SELECT 查询。

回答by StaWho

you have to redefine @Usernameafter GOstatement

你必须@UsernameGO声明之后重新定义