如何在点后连接变量 [英] How Do I Concatenate A Variable After Dot
本文介绍了如何在点后连接变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我想在'。'运算符之后使用一个变量:
Hello i want to use a variable after '.' operator like this:
SELECT * FROM DestinationDatabase.dbo.@TableName
但我得到语法错误。关于如何规避这个的任何想法?
感谢帮助。
Yet i get syntax error. Any idea on how to circumvent this?
Appreciating the help.
推荐答案
你需要使用动态SQL
有一个名为的SQL命令sp_ExecuteSQL
这将允许你构建一个这样的字符串并执行它:
You need to useDynamic SQL
There is an SQL command calledsp_ExecuteSQL
This will allow you build a string such as this and execute it:
DECLARE @MySQL VARCHAR(MAX)
SET @MySQL = 'SELECT * FROM DestinationDatabase.dbo.' + @TableName
EXEC sp_ExecuteSQL @MySQL
警告!!!警告!!!警告!!!
确保您了解使用动态SQL的安全隐患。大多数网站黑客都是通过在动态SQL执行中注入额外的SQL来完成的。
这是sp_ExecuteSQL的MSDN文章
http://msdn.microsoft.com/en-gb/library/ms188001.aspx [ ^ ]
这是一篇涵盖安全风险的文章。
动态SQL的诅咒与祝福 [ ^ ]
对于SQL中的表名,不能使用变量(以@
开头),如果你需要动态:
1)以字符串格式在客户端创建查询并在服务器上执行。
2)创建查询的sql字符串变量和EXEC()
it(请注意执行用户的SQL权限将适用)。
You can't use variables (starting with@
) for table names in SQL, if you need to be dynamic either :
1) create your queries on the client in string format and execute on your server.
2) create a sql string variable of your query andEXEC()
it (be aware that SQL permissions of the user executing will apply).
这篇关于如何在点后连接变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文