带有变量的sql use语句
我正在尝试使用 SQL 语句切换当前数据库。 我已尝试以下操作,但所有尝试都失败了:
-- 1
USE @DatabaseName
-- 2
EXEC sp_sqlexec @Sql -- where @Sql = 'USE [' + @DatabaseName + ']'
添加更多细节。
编辑:我想在两个单独的数据库上执行几件事,其中两个数据库都配置了变量。 像这样的事情:
USE Database1
SELECT * FROM Table1
USE Database2
SELECT * FROM Table2
I'm trying to switch the current database with a SQL statement.
I have tried the following, but all attempts failed:
-- 1
USE @DatabaseName
-- 2
EXEC sp_sqlexec @Sql -- where @Sql = 'USE [' + @DatabaseName + ']'
To add a little more detail.
EDIT: I would like to perform several things on two separate database, where both are configured with a variable. Something like this:
USE Database1
SELECT * FROM Table1
USE Database2
SELECT * FROM Table2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
前者的问题在于您正在做的是
USE 'myDB'
而不是USE myDB
。你正在传递一个字符串; 但 USE 正在寻找明确的参考。
后一个例子对我有用。
The problem with the former is that what you're doing is
USE 'myDB'
rather thanUSE myDB
.you're passing a string; but USE is looking for an explicit reference.
The latter example works for me.
数据库更改仅持续完成 @sql
http://blog.sqlauthority.com/2007/07/02/sql-server-2005-comparison-sp_executesql-vs-executeexec/
The DB change only lasts for the time to complete @sql
http://blog.sqlauthority.com/2007/07/02/sql-server-2005-comparison-sp_executesql-vs-executeexec/
我也有同样的问题,我用一组丑陋但有用的 GOTO 克服了它。
我在所有事情之前称之为“脚本运行程序”的原因是,我想对任何只想使用实际脚本的开发人员隐藏复杂性和丑陋的方法。 同时,我可以确保脚本以完全相同的方式在两个(可扩展到三个或更多)数据库中运行。
通过这种方法,您可以保留变量,并且如果您碰巧两次执行 DECLARE 语句,SQL Server 也不会惊慌。
I have the same problem, I overcame it with an ugly -- but useful -- set of GOTOs.
The reason I call the "script runner" before everything is that I want to hide the complexity and ugly approach from any developer that just wants to work with the actual script. At the same time, I can make sure that the script is run in the two (extensible to three and more) databases in the exact same way.
With this approach you get to keep your variables and SQL Server does not freak out if you happen to go over a DECLARE statement twice.
只是想感谢 KM 提供的宝贵解决方案。
我自己实现它是为了减少 SQLServer 上收缩数据库请求中的行数。
这是我的 SQL 请求,如果它可以帮助任何人:
Just wanted to thank KM for his valuable solution.
I implemented it myself to reduce the amount of lines in a shrinkdatabase request on SQLServer.
Here is my SQL request if it can help anyone :
尝试这个:
try this:
我的情况是有人需要一个解决方案,这是一个:
如果您使用动态 USE 语句,则所有查询都需要是动态的,因为它需要是同一上下文中的所有内容。
您可以尝试使用 SYNONYM,基本上是特定表的别名,此 SYNONYM 被插入到 sys.synonyms 表中,因此您可以从任何上下文访问它
看看这个静态语句:
现在是动态的:
现在只需更改 @CATALOG 的值您将能够列出相同的表,但来自不同的目录。
I case that someone need a solution for this, this is one:
if you use a dynamic USE statement all your query need to be dynamic, because it need to be everything in the same context.
You can try with SYNONYM, is basically an ALIAS to a specific Table, this SYNONYM is inserted into the sys.synonyms table so you have access to it from any context
Look this static statement:
Now dynamic:
Now just change the value of @CATALOG and you will be able to list the same table but from different catalog.
如果 SQLCMD 是一个选项,它支持的脚本变量超出了直接 T-SQL 的功能。 例如: http://msdn.microsoft.com/en-us/library /ms188714.aspx
If SQLCMD is an option, it supports scripting variables above and beyond what straight T-SQL can do. For example: http://msdn.microsoft.com/en-us/library/ms188714.aspx
您可以这样做:
这意味着您可以执行如下递归选择:
You can do this:
This means you can do a recursive select like the following:
使用 exec sp_execsql @Sql
示例
Use
exec sp_execsql @Sql
Example