注册 登录
编程论坛 SQL Server论坛

存储过程问题,请大侠指导

lgp740401 发布于 2017-12-06 14:54, 2734 次点击
在过程中,我有两个表,第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        
只有本站会员才能查看附件,请 登录


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

6 回复
#2
lgp7404012017-12-07 22:00
是我没说清楚呢,还是没遇上大侠,还是大侠不乐意出手?
#3
lgp7404012017-12-08 11:23
     .....
     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编辑过]

#4
mywisdom882017-12-08 16:37
没看明白,你要做什么,
#5
mywisdom882017-12-08 16:41
把2个表的结构写出来.再来点数据
#6
lgp7404012017-12-08 19:26
好的
#7
lgp7404012017-12-08 20:50
回复 5楼 mywisdom88
只有本站会员才能查看附件,请 登录
,这是一个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(实发金额)
1