离职同事写的一段SQL
if object_id('tempdb..#t1') is not null Begin drop table #t1 end select '[code]' code,'[opr]' opr,'[opr_code]' opr_code,'[place]' place,'[localcode]' localcode,'[localtor]' localtor into #t1 if object_id('tempdb..#tt2') is not null Begin drop table #tt2 end select * into #tt2 from #t1 a left join 抽样单流水表 b on a.code=b.流水号 if object_id('tempdb..#t3') is not null Begin drop table #t3 end select case when b.姓名 is not null then '工号' when c.id is not null then '档案盒' when d.id is not null then case when left(a.code,1)=代码 then '文件' else '其它' end else '其它' end 类型,a.,case when isnull(a.place,'')=''then '—' else a.place end 档案盒编号,isnull(b.姓名,'') 姓名,isnull((select top 1 place from 档案_流转记录表 where code=a.place order by id desc),'—') 档案盒位置,f.部门 into #t3 from #tt2 a left join 人员信息 b on a.code=b.工号 left join 档案_档案盒表 c on a.code=c.code left join 档案_文件表 d on a.code=d.code left join 人员信息 e on a.localcode=e.工号 left join 档案_部门信息表 f on e.小部门=f.部门 update a set 类型='文件' from #t3 a where 部门='大客户客服组' and 信息 is not null if object_id('tempdb..#t4') is not null Begin drop table #t4 end select ,case 类型 When '工号' then 姓名+'登陆' when '档案盒' then '切换至档案盒'+code when '文件' then '文件入库' else '未确定的操作类型' end 操作内容 into #t4 from #t3 ALTER TABLE #t4 ALTER COLUMN code VARCHAR(50) if (select case when isnull(opr,'')='' or isnull(opr_code,'')='' then 0 else 1 end from #t4)=0 begin if (select 类型 from #t4)<>'工号' begin update #t4 set 操作内容='请先扫工牌登陆' end end else if (select 类型 from #t4)='工号' begin update #t4 set 档案盒编号='—',档案盒位置='—' end else begin if (select isnull(place,'') from #t4 )='' begin if (select 类型 from #t4)<>'档案盒' begin update #t4 set 操作内容='请先扫档案盒位置' end else begin update #t4 set 档案盒编号='—',档案盒位置='—' end end else begin if (select 类型 from #t4)='档案盒' begin update #t4 set 档案盒编号='—',档案盒位置='—' end if (select 类型 from #t4 )='文件' begin if (select charindex(档案盒编号,code) from #t4)>0 begin if (select c.id from #t4 a left join 档案_文件表 b on a.code=b.code left join 档案_档案盒表 c on b.place=c.code) is null begin update #t4 set 操作内容='文件入库' insert 档案_流转记录表 select a.code,'文件',isnull(b.place,'初次入库'),'文件入库',a.place,getdate(),opr_code,opr,localcode,localtor from #t4 a left join 档案_文件表 b on a.code=b.code update b set place=a.place,updatetime=getdate() from #t4 a left join 档案_文件表 b on a.code=b.code where b.id is not null end else begin update a set 操作内容='不能重复入库',档案盒编号=isnull(b.place,''),档案盒位置=c.place from #t4 a left join 档案_文件表 b on a.code=b.code left join 档案_档案盒表 c on b.place=c.code end end else if (select count() from #t4 where 部门='大客户客服组' and 信息 is not null)>0 begin if object_id('tempdb..#t4_1') is not null Begin drop table #t4_1 end select left(filename,charindex('|',filename)-1) 流水号,a.,b.code bcode into #t4_1 from 档案_文件表 a left join 档案_档案盒表 b on a.place=b.code where filename like '%|%' if object_id('tempdb..#t4_2') is not null Begin drop table #t4_2 end select a.,b.code bcode,(select count() from #t4_1 where a.流水号=流水号) 在库数,(select top 1 code from #t4_1 where a.流水号=流水号 and bcode is null order by id) 编号 into #t4_2 from #t4 a left join 档案_档案盒表 b on a.place=b.code if (select count() from #t4_2 where 在库数>0 and 编号 is not null)>0 begin insert 档案_流转记录表 select 编号,'文件',isnull(b.place,'初次入库'),'文件入库',a.place,getdate(),opr_code,opr,localcode,localtor from #t4_2 a left join 档案_文件表 b on a.编号=b.code where 在库数>0 and 编号 is not null delete #t4 insert #t4 select 类型,编号,opr,opr_code,a.place,localcode,localtor,委托单位,信息,流水号,档案盒编号,姓名,档案盒位置,部门,操作内容 from #t4_2 a left join 档案_文件表 b on a.编号=b.code where b.id is not null update b set place=a.place,updatetime=getdate() from #t4_2 a left join 档案_文件表 b on a.编号=b.code where b.id is not null end else if (select count() from #t4_2 where 在库数=0 and 编号 is null)>0 begin if object_id('tempdb..#t4_3') is not null Begin drop table #t4_3 end select bcode+'-'+RIGHT('000'+convert(varchar(10),isnull((select max(convert(int,right(code,3))) from 档案_文件表 where left(code,6)=left(a.place,6)),0)+row_number() over (order by id )),3) 文件编号,流水号+'|'+信息+'|'+委托单位 文件名称,* into #t4_3 from #t4_2 a ,(select * from split_table('1,2,3,4',',') ) b where a.在库数=0 and 编号 is null insert 档案_文件表 select 文件编号,文件名称,'抽样单' filetype ,place,getdate() createtime ,getdate() updatetime,opr createtor,upper(opr_code) createcode from #t4_3 insert 档案_流转记录表 select 文件编号,'文件',isnull(b.place,'初次入库'),'文件入库',a.place,getdate(),opr_code,opr,localcode,localtor from #t4_3 a left join 档案_文件表 b on a.编号=b.code delete #t4 insert #t4 select 类型,文件编号,opr,opr_code,place,localcode,localtor,委托单位,信息,流水号,档案盒编号,姓名,档案盒位置,部门,操作内容 from #t4_3 end else begin update a set 操作内容='不能重复入库',档案盒编号=isnull(b.place,''),档案盒位置=isnull(c.place,'') from #t4_2 a left join 档案_文件表 b on a.编号=b.code left join 档案_档案盒表 c on b.place=c.code where 在库数>0 and 编号 is null delete #t4 insert #t4 select 类型,code,opr,opr_code,place,localcode,localtor,委托单位,信息,流水号,档案盒编号,姓名,档案盒位置,部门,操作内容 from #t4_2 end end else begin update a set 操作内容='入库位置不正确',档案盒编号=isnull(b.place,''),档案盒位置=isnull(c.place,'') from #t4 a left join 档案_文件表 b on a.code=b.code left join 档案_档案盒表 c on b.place=c.code end end else begin update #t4 set 操作内容=case 类型 When '工号' then 姓名+'登陆' when '档案盒' then '切换至档案盒'+code when '文件' then '文件入库' else '未确定的操作类型' end
这个需要你自己看一下咯,记得格式化一下去看。
你这是什么 存储过程?,一大串的看都看不清