日期:2014-05-18  浏览次数:20430 次

使用Gridview绑定SQL存储过程出现问题..
create   proc     cj_data
@p_cj_bid   int
as
begin
declare   @sql   varchar(8000)
set   @sql= 'select     cj_name   as   姓名,cj_xh   as   学号, '
select   @sql=@sql+ '[ '+km_name+ ']=sum(case   when   cj_kmid= ' ' '+km_dh+ ' ' '   then   cj_cj   else   0   end), '
from   (select   distinct   km_name,km_dh   from   jw_cj   a   join   jw_km   b   on   a.cj_kmid=b.km_dh   and   a.cj_bid=@p_cj_bid)   a
set   @sql=substring(@sql,1,len(@sql)-1)
set   @sql=@sql+ '   from   jw_cj   where   cj_bid=@p_cj_bid   group   by   cj_name,cj_xh '
exec(@sql)
end
go

测试查询的时候出现   必须声明变量 '@p_cj_bid '
求一正确方法.

------解决方案--------------------
--try


create proc cj_data
@p_cj_bid int
as
begin
declare @sql varchar(8000)
set @sql= 'select cj_name as 姓名,cj_xh as 学号, '
select @sql=@sql+ '[ '+km_name+ ']=sum(case when cj_kmid= ' ' '+km_dh+ ' ' ' then cj_cj else 0 end), '
from (select distinct km_name,km_dh from jw_cj a join jw_km b on a.cj_kmid=b.km_dh and a.cj_bid=@p_cj_bid) a
set @sql=substring(@sql,1,len(@sql)-1)
set @sql=@sql+ ' from jw_cj where cj_bid= '+@p_cj_bid+ ' group by cj_name,cj_xh '
exec(@sql)
end
go