标题:存储过程问题,请大侠指导
取消只看楼主
lgp740401
Rank: 1
等 级:新手上路
帖 子:52
专家分:5
注 册:2008-5-18
结帖率:91.67%
已结贴  问题点数:39 回复次数:4 
存储过程问题,请大侠指导
在过程中,我有两个表,第1个是操作人的具体操作项目,第2个是操作的人(也就是姓名表),记录数不对等,如9个人只做1件事,那么第1个表(#d_sjtmp)只有1条记录,但第2个表(#D_xm)中就有9行记录,再如2个人做了5个项目,那么第1个表就有5条记录,但第2个表中只有2行记录,想插入或更新到另一个表(tb_工资日报_b)中,存储怎么写?
  select FCODE,DDH,bxcode,bxTHICK,bxWIDTH,bxLONGTH,,scthick,scwidth,sclongth,drscl  into #d_sjtmp from tb_原始数据_b  where flayer=3 And gx=@gx And rtrim(ltrim(jlsjh_tb_原始数据))=@jlsjh
     alter table #d_sjtmp add [id] [int] IDENTITY(1,1)
     alter table #d_sjtmp add [jlsjh_tb_工资日报] [char(20)]
     update #d_sjtmp set jlsjh_tb_工资日报=@日报sjh
     select @d_sjCount=max(id) from #d_sjtmp
     set @d_sjRec=1
     while @d_sjRec<=@d_sjCount
     if @D_xmCount<=@d_sjCount
        begin
          insert into tb_工资日报_b select * from #d_sjtmp where id=@d_sjRec
          if @d_sjRec<=@D_xmCount
          update tb_工资日报_b set xm=(select 代单号1 from #D_xm where id=@d_sjRec),zbjt=(select 代单号2 from #D_xm where id=@d_sjRec),gzje=(select 单号3 from #D_xm where id=@d_sjRec)
        set @d_sjRec=@d_sjRec+1
        end
    else if @D_xmCount>@d_sjCount
         begin
          。。。。。。。
         end
end        
Data.rar (3.83 MB)


[此贴子已经被作者于2017-12-6 16:19编辑过]

搜索更多相关主题的帖子: 工资 select from where set 
2017-12-06 14:54
lgp740401
Rank: 1
等 级:新手上路
帖 子:52
专家分:5
注 册:2008-5-18
得分:0 
是我没说清楚呢,还是没遇上大侠,还是大侠不乐意出手?
2017-12-07 22:00
lgp740401
Rank: 1
等 级:新手上路
帖 子:52
专家分:5
注 册:2008-5-18
得分:0 
     .....
     set @gxRec=1
     while @gxRec<=@gxCount
     begin
           .....
           select @D_xmCount=max(id) from #D_xm
         
           select FCODE,DDH,bxcode,bxTHICK,bxWIDTH,bxLONGTH,ylly,sz,grade,jldw,drll,sccode,scthick,scwidth,sclongth, bxthrj,drscl,drcp,zbwb,gs,dw,dj,zljdj, xj,fxxj,gx,gztype,岗位,模块编号,pjhs into #d_sjtmp from tb_原始数据_b  where flayer=3 and jlsjh_tb_原始数据=@cjlsjh
           alter table #d_sjtmp add [id] [int] IDENTITY(1,1)
           alter table #d_sjtmp add [jlsjh_tb_工资日报] [char(20)]
           update #d_sjtmp set jlsjh_tb_工资日报=@日报sjh
           select @d_sjCount=max(id) from #d_sjtmp
           if @D_xmCount<=@d_sjCount
               select a.*,b.代单号1 as xm,b.代单号2 as zbjt,b.单号3 as gzje from #d_sjtmp a left join #D_xm b on a.id=b.id
  
               set @d_sjRec=1
               while @d_sjRec<=@d_sjCount
               begin
                     insert into tb_工资日报_b select * from #d_sjtmp where id=@d_sjRec
                     set @d_sjRec=@d_sjRec+1
               end
           else  if @d_sjCount<@D_xmCount ---就是这行提示附近错误
                 select a.代单号1 as xm,a.代单号2 as zbjt,a.单号3 as gzje,a.jlsjh_tb_工资日报,b.FCODE,b.DDH,b.bxcode,b.bxTHICK,b.bxWIDTH,b.bxLONGTH,b.ylly,b.sz,b.grade,b.jldw,b.drll,b.sccode,b.scthick,b.scwidth,b.sclongth,b.bxthrj,b.drscl,b.drcp,b.zbwb,b.gs,b.dw,b.dj,b.zljdj,b.xj,b.fxxj,b.gx,b.gztype,b.岗位,b.模块编号,b.pjhs from #D_xm a left join #d_sjtmp b on a.id=b.id
  
                 set @d_sjRec=1
                 while @d_sjRec<=@D_xmCount
                 begin
                      insert into tb_工资日报_b select * from #D_xm where id=@d_sjRec
                      set @d_sjRec=@d_sjRec+1
                 end
    set @gxRec=@gxRec+1
    end
不然这循环哪出问题了,老是报"第 65 行
关键字 'else' 附近有语法错误。"

[此贴子已经被作者于2017-12-8 13:43编辑过]

2017-12-08 11:23
lgp740401
Rank: 1
等 级:新手上路
帖 子:52
专家分:5
注 册:2008-5-18
得分:0 
好的
2017-12-08 19:26
lgp740401
Rank: 1
等 级:新手上路
帖 子:52
专家分:5
注 册:2008-5-18
得分:0 
回复 5楼 mywisdom88
mlmy_mis.zip (4 MB)
,这是一个sql2005数据,内有一个表:Tb_单据树表模板_b,我要写一个存储过程:sp_根据原始数据生成工资日报,意思根据"Tb_单据树表模板_b"的记录中的"模板编号",找到"主表名称"的表(即:tb_原始数据)、"保存名称"的表(即:tb_原始数据_b)和"保存编号"进行第①个大循环,然后根据每个表(tb_原始数据_b)的gx字段(即:日期字段)进行第②个大循环(因每月会有28-31天的本岗位生产记录数据),要自动产生表:tb_工资日报(汇总表),tb_工资日报_b(明细记录)
   现表(tb_工资日报,tb_工资日报_b)内的数字是我人工填写的,(tb_原始数据,tb_原始数据_b)是表单已经完成
   提一下:点工是一个人操作的,给个人,粗裁是一个人操作的,给个人,高速刨是2个合作的,应该按zbjt(职补津贴)中的比例1.3:1,也就是215元,一个人是215*1.3/(1.3+1),另一个当然:215*1/(1.3+1),然后加个各个人的数据就是gzje(实发金额)
2017-12-08 20:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-483159-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024826 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved