【问题标题】:Setting NOEXEC ON still executes "USE" statement设置 NOEXEC ON 仍然执行“USE”语句
【发布时间】:2016-07-16 04:34:31
【问题描述】:

在 SSMS 中执行以下操作时;

if not exists (select * from sys.databases where name = 'SWFUAT')
begin
    print 'The UAT database (SWFUAT) does not exist...'
    set noexec on;
end
go

use SWFUAT;
go

显示如下;

UAT 数据库 (SWFUAT) 不存在...
消息 911,第 16 层,状态 1,第 20 行
数据库 'SWFUAT' 不存在。确保名称输入正确。

编译器不应该忽略“use”语句吗?

【问题讨论】:

标签: sql-server tsql ssms


【解决方案1】:

如果我正确理解你的问题,那么你想执行

use SWFUAT;

仅当SWFUAT 数据库存在时。

因为这句话很遗憾

USE 在编译和执行时都执行,并立即生效。因此,在 USE 语句之后出现的批处理语句会在指定的数据库中执行。

描述了here,你不能简单地使用SET NOEXEC。 为了实现你所需要的,你应该用例如这个替换你的代码

if not exists (select * from sys.databases where name = 'SWFUAT')
begin
    print 'The UAT database (SWFUAT) does not exist...' 
end else begin
    use SWFUAT;
end
go

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多