要疯了,为什么放了好几百分,一个并不很难的问题却没人回答上来,是我太烂吗?
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
(
select 1 from t_userlogin
where
charindex(rtrim(tmpA.gsid),@companyname)> 0
) and zyid = tmp.zyid ))
)
这种情况时,就会出问题,而问题的的根源是这么长的一段取gsid值的代码所取出的值是如:
gsid
255
5554
545
878
这种记录集的样式,而这种样式如何能被gsid in (.....)这种形式接受,我不会写了,不知道要如何把这段:
(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 in 可以接受的形式,要如何表达?
3.当前给出的代码肯定是没有逻辑问题,这个我也知道,问题的根源就是当else时的取gsid代码如何接被接受的问题,我不知道要如何写了!
4.谢谢老大的回答,还请再指教!
[解决办法]
不妨看
gsid in (case when @companyname is not null and @companyname <> '0 ' then (一串的sql)
else gsid end )
[解决办法]
yowbell(二把刀;别看盖茨有钱 一点精神寄托没有) ( ) 信誉:100 Blog 2007-01-26 17:08:31 得分: 0
playwarcraft老大讲的试过了,不行
------------------------------------------------
什? 你的gsid什型?
[解决办法]
不是你的tt名用了2次的原因? 猜的哈...
另外charindex(rtrim(tmpA.gsid),@companyname)> 0
法,最好改成: charindex( ', '+rtrim(tmpA.gsid)+ ', ', ', '+@companyname+ ', ')> 0
[解决办法]
帮你UP下
[解决办法]
仔看了下你出的SQL,有奇怪,
如 t_userlogin ,就用到:(
[解决办法]
你的题比较难
一无数据结构,二无目的表述,三无方案表述,给一堆代码,是只要解析正确就算解决问题呢,还是要怎么样的结果???
难于上青天
[解决办法]
那个要单独改改可以这样:
and
(isnull(@companyname,0)=0 or
gsid in (select ..........)
)
[解决办法]
/* ------------------------ */
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 )
/* ------------------------ */
[解决办法]
你的t_userlogin表和duizhaobiao表之间有没有关联?
还有,你的zyid= tmp.zyid 中等号左边的zyid是那个表中的?
还有,你些了那么多嵌套,你的嵌套查询本身就有问题的!
建议你把下面的语句在查讯分析器中执行一下,看我问题的具体所在,因为你的代码中好多地方我没法明确,所以没办法给你具体之处错误之处!
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
)
[解决办法]
(case when then end) 要求前后数据类型一致,不管怎么写,前后都很有可能不一致,前者是个整个,后者是集合