存储过程取字段值遇到问题

color2002 2005-02-23 03:03:15
create proc search_d_pro
@sid int,
@d varchar(50),
@year int,
@month int,
@ddd varchar(50) out
as
begin
--select @ddd=isnull(sum(cast(@d as float)),0) from pw_data
--where s_id=@sid and year(dateandtime)=@year and month(dateandtime)=@month and isnumeric(@d)=1
if(@d='d1')
select @ddd=isnull(sum(cast(d1 as float)),0) from pw_data
where s_id=@sid and year(dateandtime)=@year and month(dateandtime)=@month and isnumeric(d1)=1
else if(@d='d6')
select @ddd=isnull(sum(cast(d6 as float)),0) from pw_data
where s_id=@sid and year(dateandtime)=@year and month(dateandtime)=@month and isnumeric(d6)=1
else
select @ddd=isnull(sum(cast(d7 as float)),0) from pw_data
where s_id=@sid and year(dateandtime)=@year and month(dateandtime)=@month and isnumeric(d7)=1
end
go

请大家注意看一下注释掉的两行:
--select @ddd=isnull(sum(cast(@d as float)),0) from pw_data
--where s_id=@sid and year(dateandtime)=@year and month(dateandtime)=@month and isnumeric(@d)=1
如果这样写的话,@d传递过来的值为三个中的一个:d1,d6,d7,
取出来的值一直为null,
页通过下面判断@d的值,分别写SQL语句就能取出来,
if(@d='d1')
select @ddd=isnull(sum(cast(d1 as float)),0) from pw_data
where s_id=@sid and year(dateandtime)=@year and month(dateandtime)=@month and isnumeric(d1)=1
else if(@d='d6')
select @ddd=isnull(sum(cast(d6 as float)),0) from pw_data
where s_id=@sid and year(dateandtime)=@year and month(dateandtime)=@month and isnumeric(d6)=1
else
select @ddd=isnull(sum(cast(d7 as float)),0) from pw_data
where s_id=@sid and year(dateandtime)=@year and month(dateandtime)=@month and isnumeric(d7)=1
end
由此我判断是在select @ddd=isnull(sum(cast(@d as float)),0) from pw_data中,@d没有取到值造成的,
其他的几个参数都可以传进来的,
各位老大知道是什么原因吗?
...全文
138 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
color2002 2005-02-24
  • 打赏
  • 举报
回复
照Softlee81307(孔腎)的方法,报如下错误:
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '=' 附近有语法错误。

(所影响的行数为 1 行)

可能是exec(
'select '+@ddd+'=isnull(sum(cast('+@d+' as float)),0) from pw_data 这行中的=号报错
Softlee81307 2005-02-24
  • 打赏
  • 举报
回复
exec(
'select '+@ddd+'=isnull(sum(cast('+@d+' as float)),0) from pw_data
where s_id='+cast(@sid as varchar(6))+' and year(dateandtime)='+cast(@year as varchar(4))+' and month(dateandtime)='+cast(@month as varchar(2)) )
web_gus 2005-02-24
  • 打赏
  • 举报
回复
up
Softlee81307 2005-02-24
  • 打赏
  • 举报
回复
select @ddd=isnull(sum(cast(@d as float)),0) from pw_data
這段要改成動態的
exec('select '+@ddd+'=isnull(sum(cast('+@d+' as float)),0) from pw_data')
color2002 2005-02-24
  • 打赏
  • 举报
回复
没人知道吗?
doubon 2005-02-23
  • 打赏
  • 举报
回复
顶!

34,576

社区成员

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

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