条件中的 T-SQL USE 语句 [英] T-SQL USE satetement in condition

查看:39
本文介绍了条件中的 T-SQL USE 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像USE语句是用MS SQL Server 2008R2编译的.

It seems that USE statement is compiled by MS SQL Server 2008R2.

例如

if(1=0)
begin
   USE MyDB
end

结果将是在执行 USE MyDB 时忽略 IF 语句.

The result will be in executing USE MyDB while ignoring IF statement.

如何将其更改为使用条件?

How can it be changed to work with condition?

附言还尝试了 execute sp_executesql N'USE [MyDB]',它适用于条件,但不会更改当前上下文.

P.S. Also tried execute sp_executesql N'USE [MyDB]', it works with condition, but it does not change current context.

推荐答案

当你需要一个有条件的使用 MyDB"时,我会看看 sqlcmd 和变量.

When you need a conditional "Use MyDB", I would look at sqlcmd and variables.

Use $(DatabaseName) 
GO 

这是我发现获得动态"使用 MyDatabase 名称的唯一方法.

That's the only way I have found to get "dynamic" Use MyDatabase names.

参考文章:

technet.microsoft.com/en-us/library/ms188714.aspx

这篇关于条件中的 T-SQL USE 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆