如果用变量取得用变量名作为数据表的sql语句里面的值??
请看下面的语句,错了吗?该怎么样改,高手帮忙??
declare @Isnd int,@stutable char(90)
set @stutable ='试卷表'
set @lsnd=(select avg(难度系数) from @stutable where 题型='单选题')
select @isnd
...全文
12811打赏收藏
关于将一个变量代替数据表的查询语句的该如何写?
如果用变量取得用变量名作为数据表的sql语句里面的值?? 请看下面的语句,错了吗?该怎么样改,高手帮忙?? declare @Isnd int,@stutable char(90) set @stutable ='试卷表' set @lsnd=(select avg(难度系数) from @stutable where 题型='单选题') select @isnd
declare @Isnd varchar(1000),@stutable char(90) ,@i int
set @stutable ='考生试卷样表'
set @lsnd='select bb=avg(难度系数) into ##c from '+@stutable +' where 题型=''单选题'''
exec(@isnd)
select @i=bb from ##c
drop table ##c
如果要輸出執行語句
declare @Isnd int,@stutable nvarchar(90),@s nvarchar(1000)
set @stutable ='试卷表'
set @s='select @Isnd =avg(难度系数) from '+@stutable+' where 题型=''单选题'''
如果要執行語句
declare @Isnd varchar(1000),@stutable char(90) ----@isnd改成字符形
set @stutable ='试卷表'
set @lsnd='select avg(难度系数) from '+@stutable +' where 题型=''单选题'''
exec(@isnd) --------這裡改成exec來執行
declare @Isnd varchar(1000),@stutable char(90) ----@isnd改成字符形
set @stutable ='试卷表'
set @lsnd='select avg(难度系数) from '+@stutable +' where 题型=''单选题'''
select @isnd