gpt4 book ai didi

sql - 为什么即使条件计算结果为 false,也会创建在 IF 语句内声明的变量?

转载 作者:行者123 更新时间:2023-12-02 03:59:33 25 4
gpt4 key购买 nike

由于@A从未被声明过,sql server应该抛出一个错误,但它没有。这是为什么?

DECLARE @i int = 1;
IF @i > 10
BEGIN
DECLARE @A int = 100;
END

PRINT @A; // doesn't return any result

谢谢

最佳答案

SQL Server 没有 block 级变量作用域。

它是按批处理/存储过程等

From MSDN (我的粗体)

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.

关于sql - 为什么即使条件计算结果为 false,也会创建在 IF 语句内声明的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2952553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com