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

要疯了,为什么放了好几百分,一个并不很难的问题却没人回答上来,是我太烂吗?
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   的列时发生语法错误。


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

============================
1.gsid   in   (case   when   @companyname   is   null   or   @companyname=0   then   gsid   这一段测试没有问题,能够正常出结果
  2.这一段
(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   ))

单独取gsid的值没有问题,测试过了;
有问题的是当gsid   in   (
(select   gsid   from   duizhaobiao   as   tmp
where   exists
(
select   *   from   duizhaobiao   as   tmpA
where   exists
(