加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 为什么即使条件求值为false也会在IF语句中声明变量?

发布时间:2020-12-12 08:42:15 所属栏目:MsSql教程 来源:网络整理
导读:由于@A从未被声明,所以sql server应该会抛出一个错误,但是它不会.这是为什么? DECLARE @i int = 1;IF @i 10BEGIN DECLARE @A int = 100;ENDPRINT @A; // doesn't return any result 感谢名单 解决方法 SQL Server没有块级变量范围. 它是每批次/存储过程等 Fr
由于@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没有块级变量范围.

它是每批次/存储过程等

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读