SYbase存储过程写法

chaoxy21cn 2008-05-29 08:58:47
各位达人,由于不熟悉数据库东东,下面是我东拉西扯一个存储过程.请帮忙改成可执行,谢谢!  
create procedure P_BranchTdTotalSMnumAverageCount @tradeKey varchar(200),@startDate char(8),@endDate char(8),@deptID varchar(30),@termKey varchar(20)
   as
BEGIN
select deptDesc,deptName,sum(num) as totaltd,count(1) as totalsm,totaltd/totalsm as average from (
   select d.deptDesc,d.deptName,count(1) as num,a.smNo,count(a) from TradeLog a,SelfMachineInfo b,Dept c,Dept d
   where a.smNo=b.smNo and b.deptID=c.deptID and d.deptID=c.prarentDeptID and a.tradeCode!='3954'
if(@tradeKey!='')
and (a.tradeCode like '%@tradeKey%' or a.tradeDesc like '%@tradeKey%')
if(@startDate!='')
and a.tradeDate >= '@startDate'
if(@endDate!='')
and a.tradeDate <= '@endDate'

if(@deptID!='')
begin
and (c.deptDesc='@deptID'
declare deptList cursor for select b.deptDesc from Dept a,Dept b where a.deptDesc = '@deptID' and b.prarentDeptID = a.deptID
open deptList
declare @subDeptID varchar(30)
if(游标的列数 > 0)
begin
or c.deptDesc in ( '
fetch deptList into @subDeptID
while @@sqlstatus != 2
begin
if @@sqlstatus =1
begin
   raiserror 20001 "select fail"
return
end
if(不是游标最后一列)
@subDeptID ','
else
@subDeptID
end
')
end
close deptList
deallocate cursor deptList
)
end

if(@termKey!='')
and a.smNo like '%@termKey%'
group by d.deptDesc,d.deptName,c.prarentDeptID,a.smNo
) group by deptDesc order by average
END
...全文
130 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
icq_power 2008-06-02
  • 打赏
  • 举报
回复
说实话,写得很烂,很多语法的错误,也没有注释,完全不知道你要做什么。
chaoxy21cn 2008-05-30
  • 打赏
  • 举报
回复
没人回,分不够开贴另给啊!
chaoxy21cn 2008-05-29
  • 打赏
  • 举报
回复
游标的列数为游标的行数
最后一列是最后一行

2,596

社区成员

发帖
与我相关
我的任务
社区描述
Sybase相关技术讨论区
社区管理员
  • Sybase社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧