日期:2014-05-18 浏览次数:20575 次
declare @tbname varchar(20) select @tbname from tb where id='01' exec('select * from '+@tbname) --表名或字段名为变量需要用exec动态执行SQL语句
------解决方案--------------------
if object_id('tempdb..#tempuf_tb1') is not null drop table #tempuf_tb1 go create table #tempuf_tb1 (id int, name varchar(10)) go declare @name varchar(max) select @name=name from tempdb..sysobjects where name like '#tempuf[_]%' exec('select * from '+@name) /* id name ----------- ---------- (0 行受影响) */
------解决方案--------------------
declare @tbname varchar(100)
select @tbname=name from sysobjects where name like'%tempuf_%'
exec('select * from ['+@tbname+']')
注意空格。
------解决方案--------------------
declare @tbname varchar(100) select @tbname=name from sysobjects where name like'%tempuf_%' exec('select * from ['+@tbname+']')