关于将一个变量代替数据表的查询语句的该如何写?

ruoyasoftware 2005-03-08 09:09:23
如果用变量取得用变量名作为数据表的sql语句里面的值??
请看下面的语句,错了吗?该怎么样改,高手帮忙??
declare @Isnd int,@stutable char(90)
set @stutable ='试卷表'
set @lsnd=(select avg(难度系数) from @stutable where 题型='单选题')
select @isnd

...全文
128 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Softlee81307 2005-03-08
  • 打赏
  • 举报
回复
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
ruoyasoftware 2005-03-08
  • 打赏
  • 举报
回复
declare @Isnd varchar(1000),@stutable char(90)
set @stutable ='考生试卷样表'
set @lsnd='select avg(难度系数) from '+@stutable +' where 题型=''单选题'''
exec(@isnd)

这样我试过,老是提示:
服务器: 消息 137,级别 15,状态 1,行 4
必须声明变量 '@lsnd'。
最后用select @isnd 可以返回:select avg(难度系数) from 考生试卷样表 where 题型='单选题'
不过知道怎么才能返回avg(难度系数)的值。
请各位指点:sp_executesql 存储过程的参数该怎么用??
谢谢各位!


ruoyasoftware 2005-03-08
  • 打赏
  • 举报
回复
我要用@isnd 来获取avg(难度系数)的值。
用上面的语句,好像不行。不知道是哪里错了。
提示:过程需要参数 '@parameters' 为 'ntext/nchar/nvarchar' 类型。
Softlee81307 2005-03-08
  • 打赏
  • 举报
回复
如果要輸出執行語句
declare @Isnd int,@stutable nvarchar(90),@s nvarchar(1000)
set @stutable ='试卷表'
set @s='select @Isnd =avg(难度系数) from '+@stutable+' where 题型=''单选题'''

exec sp_executesql @s,'@Isnd int out',@Isnd

select @Isnd
Softlee81307 2005-03-08
  • 打赏
  • 举报
回复
如果要執行語句
declare @Isnd varchar(1000),@stutable char(90) ----@isnd改成字符形
set @stutable ='试卷表'
set @lsnd='select avg(难度系数) from '+@stutable +' where 题型=''单选题'''
exec(@isnd) --------這裡改成exec來執行
Softlee81307 2005-03-08
  • 打赏
  • 举报
回复
declare @Isnd varchar(1000),@stutable char(90) ----@isnd改成字符形
set @stutable ='试卷表'
set @lsnd='select avg(难度系数) from '+@stutable +' where 题型=''单选题'''
select @isnd
lingdian000 2005-03-08
  • 打赏
  • 举报
回复
exec sp_executesql @s,'@Isnd int out',@Isnd

要改为:

exec sp_executesql @s,N'@Isnd int out',@Isnd out
lingdian000 2005-03-08
  • 打赏
  • 举报
回复
exec sp_executesql @s,'@Isnd int out',@Isnd

要改为:

exec sp_executesql @s,'@Isnd int out',@Isnd out
lsxaa 2005-03-08
  • 打赏
  • 举报
回复
declare @Isnd int,@stutable nvarchar(90),@s nvarchar(1000)
set @stutable ='试卷表'
set @s='select @Isnd =avg(难度系数) from '+@stutable+' where 题型=''单选题'''

exec sp_executesql @s,'@Isnd int out',@Isnd

select @Isnd
lsxaa 2005-03-08
  • 打赏
  • 举报
回复
declare @Isnd int,@stutable nvarchar(90),@s nvarchar(1000)
set @stutable ='试卷表'
set @s='select @lsnd=avg(难度系数) from '+@stutable+' where 题型=''单选题'''

exec sp_executesql @s,'@lsnd int out',@lsnd

select @isnd
qq3080 2005-03-08
  • 打赏
  • 举报
回复
declare @Isnd int,@stutable char(90)
set @stutable ='试卷表'
select @lsnd=avg(难度系数) from @stutable where 题型='单选题'

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧