急!!!存储过程赋值问题 如何让 exec执行的SQL语句中的数据赋值给存储过程中的output变量?

huangjianzhao 2008-09-17 06:13:46
用来统计一列数据有多少为空的存储过程中有:
@sql='select (case when c1 is null then 1 else 0 end)
+(case when c2 is null then 1 else 0 end)
+(case when c2 is null then 1 else 0 end) nullnum from 表名 where ID=1'
用exec(@sql)执行的结果如下:
nullnum
1 2
我想在存储过程中引入输出参数(例如:@c int output)并将执行结果赋给@c,请问怎么做呀?
...全文
183 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangjianzhao 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 libin_ftsafe 的回复:]
try:

SQL code
declare @sql nvarchar(4000),@pc int
@sql=N'select @pc=(case when c1 is null then 1 else 0 end)
+(case when c2 is null then 1 else 0 end)
+(case when c2 is null then 1 else 0 end) from 表名 where ID=1'

exec sp_executesql @sql,N'@pc int output',@pc output
[/Quote]
上面要不要改为:
... @pc=((case when c1 is null then 1 else 0 end)
+(case when c2 is null then 1 else 0 end)
+(case when c2 is null then 1 else 0 end))......
加上一个括号
子陌红尘 2008-09-17
  • 打赏
  • 举报
回复
try:

declare @sql nvarchar(4000),@pc int
@sql=N'select @pc=(case when c1 is null then 1 else 0 end)
+(case when c2 is null then 1 else 0 end)
+(case when c2 is null then 1 else 0 end) from 表名 where ID=1'

exec sp_executesql @sql,N'@pc int output',@pc output
huangjianzhao 2008-09-17
  • 打赏
  • 举报
回复
请大家帮帮忙,谢谢!!!
huangjianzhao 2008-09-17
  • 打赏
  • 举报
回复
在@sql中加入@pc=nullnum并且改为
exec sp_executesql @sql,N'@pc int output',@pc output
也不行。

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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