日期:2014-05-17  浏览次数:20474 次

关于表名是变量的问题,请帮忙
如下语句,怎么写可以执行,即 from后面的表名是变量怎么写

declare @bname char(20)
set @bname='msdb..zc_khls_201209'
select *
from @bname
where date_bf='20120926'


------解决方案--------------------
SQL code

create procedure sp_xxxx 
@bname nvarchar(100)

--declare @bname char(20)
set @bname='msdb..zc_khls_201209'
select *
from @bname
where date_bf='20120926'

------解决方案--------------------
SQL code
declare @bname char(20)
set @bname='msdb..zc_khls_201209'
exec('select *
from' +@bname
'where date_bf='20120926'')

------解决方案--------------------
SQL code
declare @bname char(20)
 set @bname='msdb..zc_khls_201209'
 DECLARE @sql NVARCHAR(max)
 SET @sql='select *
 from '+@bname+'
 where date_bf=''20120926'' '
 PRINT @sql
 EXEC (@sql)

------解决方案--------------------
晕,网速卡了一下就2个人回复了。