我正在尝试创建一些方法来根据发送到数据库的 ID 动态选择要在其上运行的过程的表。像这样的东西:
@TableId int
As
Declare @nameoftable varchar(50)
select @nameoftable = Nameoftable from tablelist where id = @tableid
-- returning on selected table
Select somestuff
from @nameoftable
有什么想法吗?
最佳答案
你需要使用动态SQL
@TableId int
As
Declare @nameoftable varchar(50)
select @nameoftable = Nameoftable from tablelist where id = @tableid
-- returning on selected table
declare @sql nvarchar(1000)
set @sql = 'Select somestuff from ' + Quotename(@nameoftable)
exec(@sql)
关于SQL表选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4968017/