gpt4 book ai didi

sql-server - 设置 NOEXEC ON 仍然执行 "USE"语句

转载 作者:行者123 更新时间:2023-12-03 10:02:22 28 4
gpt4 key购买 nike

在 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

显示如下;

The UAT database (SWFUAT) does not exist...
Msg 911, Level 16, State 1, Line 20
Database 'SWFUAT' does not exist. Make sure that the name is entered correctly.

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

最佳答案

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

use SWFUAT;

仅当SWFUAT 数据库存在时。

不幸的是因为这句话

USE is executed at both compile and execution time and takes effect immediately. Therefore, statements that appear in a batch after the USE statement are executed in the specified database.

描述 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

关于sql-server - 设置 NOEXEC ON 仍然执行 "USE"语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38319250/

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