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

又只有二十分了,答对了前面的一百多分同送,为什么没人理呢,郁闷啊,高手教教偶吧
CREATE   proc   zjreport_release
      (@companyname   varchar(1000)=null,
        @startdate   datetime=null,
        @enddate   datetime=null
        )
as  
begin
select   公司名称zy   as   公司名称,sum(申请用款总额)   as   借款额   from  
(
select   公司id,公司名称,sum(批复金额)   as   申请用款总额  
from  
(
  select   *   from   v_shenp
  union   all
  select   *   from   shenp_v_b)   tt
  where     审批意见= '同意 '  
/*   ------------------------   */
and  
gsid   in   (case   when   @companyname   is   null   or   @companyname=0   then   gsid   else  
(select   gsid   from   duizhaobiao   as   tmp
where   exists
(
select   *   from   duizhaobiao   as   tmpA
where   exists
(
select   1   from   t_userlogin
where  
charindex(rtrim(tmpA.gsid),@companyname)> 0
)   and   zyid   =   tmp.zyid   ))
end   )
/*   ------------------------   */
and   请求时间> (case   when   @startdate   is   null   then   请求时间-1   else   @startdate   end)
and   请求时间 <(case   when   @enddate   is   null   then   请求时间+1   else   @enddate   end)
group   by   公司id,公司名称
)tt   ,duizhaobiao   aa   where   tt.公司id   =   aa.gsid   group   by   公司名称zy
end
GO


/*   ---请高手们注意,这个存储过程现在已经实现了当gsid为null或是为0时的查询功能,正常使用了,不正常的就是后面的半句,也就是:
else  
(select   gsid   from   duizhaobiao   as   tmp
where   exists
(
select   *   from   duizhaobiao   as   tmpA
where   exists
(
select   1   from   t_userlogin
where  
charindex(rtrim(tmpA.gsid),@companyname)> 0
)   and   zyid   =   tmp.zyid   ))
end   )
这一段,而这里面的查询gsid值的写法是一位老大给写的,也没有问题,唯一有问题的就是最后汇总的写法else   ....   end   中间的这一段最后应如何表示,不知道如何表达了,如果按照现在的写法,当输入@companyname参数如   420,425,474,475,533   之类,则会报错,错误如下:
服务器:   消息   245,级别   16,状态   1,过程   zjreport_release,行   8
将   varchar   值   '420,425,474,475,533 '   转换为数据类型为   int   的列时发生语法错误。


请高手指点一下,这种情况要如何写?


------解决方案--------------------
用replace转换有 ', '
------解决方案--------------------
420,425,474,475,533 不能转换为int型
用replace将,替换掉就可以了
------解决方案--------------------
charindex(rtrim(tmpA.gsid),@companyname)> 0
) and zyid = tmp.zyid ))

当中的括号可能括错了,试试这个行不行

charindex(rtrim(tmpA.gsid),@companyname))> 0
and zyid = tmp.zyid ))
------解决方案--------------------
对不起,我的答案错误!

gsid in (case when @companyname is null or @companyname=0 then gsid else

改成

gsid in (case when @companyname is null or @companyname= ' ' or @companyname= '0 ' then gsid else