存储过程取字段值遇到问题
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没有取到值造成的,
其他的几个参数都可以传进来的,
各位老大知道是什么原因吗?