且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

存储过程中的 SQL Server 变量作用域

更新时间:2023-02-07 11:29:02

来自 书籍在线:

变量的范围是可以引用该变量的 Transact-SQL 语句的范围.变量的范围从它被声明的那一刻开始,直到它被声明的批处理或存储过程结束.

The scope of a variable is the range of Transact-SQL statements that can reference the variable. The scope of a variable lasts from the point it is declared until the end of the batch or stored procedure in which it is declared.

不过.没有什么可以阻止您这样做:

However. Nothing keeps you from doing this:

create procedure Foo as begin

declare @bob int

if exists (x)
begin
    set @bob = 1
end
else
begin
    set @bob = 2
end

end