Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试创建一个存储过程,该过程从将参数化的数据库名称中进行选择。但是,我在这样做时遇到了很多麻烦。我正在尝试执行如下简单的操作:
DECLARE @tableName VARCHAR SET @tableName = 'MY_TABLE_NAME' SELECT * FROM @tableName
这会产生错误:Incorrect syntax near '@tableName'.
Incorrect syntax near '@tableName'.
有人可以告诉我如何从参数化表名中进行选择吗?
在这种情况下执行动态 SQL。
DECLARE @tableName VARCHAR SET @tableName = 'MY_TABLE_NAME' exec(' SELECT * FROM ' + @tableName )