1 begin
  2
  3 --通过销货单与明细,生成安装项目及明细,及判断明细是否拆分生成多条
  4 --delete from sazxm
  5 --delete from ssbazrw
  6 --获取未生成项目的销货单号
  7 select rowid = identity(int,1,1),a.id xmid
  8 into #sazxmlist
  9 from sales a where a.id not in (select id from sazxm) and
 10 convert(char(20),a.ddjrq,112) >= convert(char(20),'2018-12-01',112)
 11 --根据销货单 单据日期 生成 当天安装项目,公司安装的直接到待安装状态
 12 insert into sazxm(id,scode,idxhdh,sazxmdb,emazlx,idxsht,idxsdd,idhtqykh,emsfwsby,emsfwsaz,iddep,
 13 idowner,emshzt,dfhrq,dbjfzje,emlife,idzdkh,swlgs,dsfzdsc,created,createdby,djrq,bqbqr,idazgcs,
 14 idfpr,dfpsj,sshdz,idxsjh,contacts,sdh,idtsfpr)
 15 select xhd.id,replace(convert(char(20),xhd.ddjrq,112)+right(xhd.id,3),' ',''),xhd.id,'2F01',
 16 'f8d6171e3fcdaed39f0919a9ec5f51ae',xhd.idxmh,xhd.idso,xhd.idkh,ht.emcarry,ht.install,xhd.iddep,xhd.idowner,
 17 '2cdd6816b789e9dc5312a21e37232b46',xhd.dxhrq,xhd.dbmoney,ht.emlife,xhd.idkh,xhd.sfyfs,1,getdate(),
 18 '12979218489686204728368524150460',convert(date,xhd.ddjrq),0,ht.idfwgcs,'12979218489686204728368524150460',
 19 getdate(),xhd.sshdz,xhd.idxsjh,xhd.contacts,xhd.slxdh,'9eda31d9ea905fa4df78f3d8b9b642c0'
 20 from sales xhd left join [contract] ht on ht.id = xhd.idxmh left join org_employee emp
 21 on emp.id = ht.idfwgcs where xhd.id not in (select id from sazxm) and
 22 convert(char(20),xhd.ddjrq,112) >= convert(char(20),'2018-12-01',112)
 23 and xhd.sname in ('2301','2303','2388','2399') and ht.install = '9eda31d9ea905fa4df78f3d8b9b642c0'
 24 --根据销货单 单据日期 生成 当天安装项目,客户安装的直接到待客服审核状态
 25 insert into sazxm(id,scode,idxhdh,sazxmdb,emazlx,idxsht,idxsdd,idhtqykh,emsfwsby,emsfwsaz,iddep,
 26 idowner,emshzt,dfhrq,dbjfzje,emlife,idzdkh,swlgs,dsfzdsc,created,createdby,djrq,bqbqr,
 27 sshdz,idxsjh,contacts,sdh)
 28 select xhd.id,replace(convert(char(20),xhd.ddjrq,112)+right(xhd.id,3),' ',''),xhd.id,'2F01',
 29 'f8d6171e3fcdaed39f0919a9ec5f51ae',xhd.idxmh,xhd.idso,xhd.idkh,ht.emcarry,ht.install,xhd.iddep,xhd.idowner,
 30 'c03cc4622209d4adfa082a96e73f8221',xhd.dxhrq,xhd.dbmoney,ht.emlife,xhd.idkh,xhd.sfyfs,1,getdate(),
 31 '12979218489686204728368524150460',convert(date,xhd.ddjrq),0,xhd.sshdz,xhd.idxsjh,xhd.contacts,xhd.slxdh
 32 from sales xhd left join [contract] ht on ht.id = xhd.idxmh left join org_employee emp
 33 on emp.id = ht.idfwgcs where xhd.id not in (select id from sazxm) and
 34 convert(char(20),xhd.ddjrq,112) >= convert(char(20),'2018-12-01',112)
 35 and xhd.sname in ('2301','2303','2388','2399') and ht.install = 'f908b77150a27c74415912c1c3a13656'
 36 --无合同的 销货单 单据日期 生成 当天安装项目
 37 insert into sazxm(id,scode,idxhdh,sazxmdb,emazlx,idxsdd,idhtqykh,iddep,
 38 idowner,emshzt,dfhrq,dbjfzje,idzdkh,swlgs,dsfzdsc,created,createdby,djrq,bqbqr,
 39 sshdz,idxsjh,contacts,sdh)
 40 select xhd.id,replace(convert(char(20),xhd.ddjrq,112)+right(xhd.id,3),' ',''),xhd.id,'2F01',
 41 'f8d6171e3fcdaed39f0919a9ec5f51ae',xhd.idso,xhd.idkh,xhd.iddep,xhd.idowner,
 42 '20658d2abc23904dd1d9c0db20f04d71',xhd.dxhrq,xhd.dbmoney,xhd.idkh,xhd.sfyfs,1,getdate(),
 43 '12979218489686204728368524150460',convert(date,xhd.ddjrq),0,xhd.sshdz,xhd.idxsjh,xhd.contacts,xhd.slxdh
 44 from sales xhd where xhd.id not in (select id from sazxm) and
 45 convert(char(20),xhd.ddjrq,112) >= convert(char(20),'2018-12-01',112)
 46 and xhd.sname in ('2301','2303','2388','2399') and xhd.idxmh is null
 47
 48 --创建临时表,表中有3列:行号,id,数量,是否展开数量
 49 select rowid = identity(int,1,1),mx.id,mx.dbnumber,mx.sfscazrw
 50 into #amxtemplist
 51 from #sazxmlist a join salemx mx on a.xmid = mx.idxhdh
 52
 53 --DROP TABLE #amxtemplist
 54 --select * from #amxtemplist
 55 --声明变量:@numrows 当前行号,@maxnumrows 最大行号,@id 当前id,@maxdbsl 最大数量,@dbsl 初始数量,@sfcf 是否展开数量
 56 declare @numrows int,@maxnumrows int,@id char(100),@maxdbsl int ,@dbsl int,@sfcf int
 57
 58 select @numrows = MIN(RowID),@maxnumrows=MAX(RowID)
 59 FROM #amxtemplist
 60 print @numrows print @maxnumrows
 61
 62 while @numrows <= @maxnumrows
 63 begin
 64 select @id = id,@maxdbsl = dbnumber
 65 from #amxtemplist where rowid=@numrows
 66
 67 select @id = (select id from salemx where id = @id)
 68 set @maxdbsl = (select dbnumber from sales xhd join salemx xhmx on xhd.id = xhmx.idxhdh where xhmx.id = @id)
 69 set @sfcf = (select sfscazrw from sales xhd join salemx xhmx on xhd.id = xhmx.idxhdh where xhmx.id = @id)
 70 set @dbsl = 1
 71 if (@dbsl = @maxdbsl)
 72 begin
 73 --生成单个设备安装任务
 74 insert into ssbazrw (id,idazxm,sazxmdb,syspos,idcpxx,idxhmxbh,swlmc,idpp,szjys,
 75 sptys,sdw,iddep,idowner,idhtqykh,idzdkh,dbsl,sph,scw,sfqdb,idfqdh,idxhddh,sxhddb,
 76 dbprice,dbmoney,swlgg,emazlx,created,createdby,szk,rwscode,emzt,idazgcs,idfpr,
 77 dfpsj,idazbm,idht,emsfyxlh,ddjrq,bzdscsj)
 78 select xhmx.id,xhmx.idxhdh,left(xhmx.idxhdh,4),xhmx.sxh,xhmx.idcp,
 79 xhmx.id,xhmx.sname,xhmx.idpp,xhmx.szjys,xhmx.sptys,xhmx.sdw,xhmx.iddep,
 80 xhmx.idowner,xhmx.idkh,xhmx.idkh,xhmx.dbnumber,xhmx.sph,xhmx.scw,xhmx.sfqdb,
 81 xhmx.idfqdh,xhmx.idxhdh,left(xhmx.idxhdh,4),xhmx.dbprice,xhmx.dbmoney,xhmx.sgg,
 82 'f8d6171e3fcdaed39f0919a9ec5f51ae',getdate(),'12979218489686204728368524150460',
 83 xhmx.discount,xhd.scode+'-'+xhmx.sxh,'7c78c2e5fb906560a1ff52ea68a50b6d',xm.idazgcs,
 84 xm.idfpr,xm.dfpsj,emp.iddep,xm.idxsht,'f3406c8577eb9b62f9e5cf8a1895e8c2',xhd.ddjrq,1
 85 from sales xhd join salemx xhmx on xhd.id = xhmx.idxhdh join sazxm xm on xm.id = xhd.id
 86 left join org_employee emp on emp.id = xm.idazgcs
 87 where xhmx.id not in (select id from ssbazrw)
 88 and xhd.id in (select id from sazxm) and xhmx.dbnumber = 1 and xhmx.sfscazrw = 1
 89 end
 90 else
 91 begin
 92 --数量大于1时,创建多条设备安装任务
 93 while (@dbsl <= @maxdbsl)
 94 begin
 95 if(@sfcf = 0)
 96 begin
 97 --生成无需展开的安装任务
 98 insert into ssbazrw (id,idazxm,sazxmdb,syspos,idcpxx,idxhmxbh,swlmc,idpp,szjys,
 99 sptys,sdw,iddep,idowner,idhtqykh,idzdkh,dbsl,sph,scw,sfqdb,idfqdh,idxhddh,sxhddb,
100 dbprice,dbmoney,swlgg,emazlx,created,createdby,szk,rwscode,emzt,idazgcs,idfpr,
101 dfpsj,idazbm,idht,emsfyxlh,ddjrq,bzdscsj)
102 select xhmx.id,xhmx.idxhdh,left(xhmx.idxhdh,4),xhmx.sxh,xhmx.idcp,
103 xhmx.id,xhmx.sname,xhmx.idpp,xhmx.szjys,xhmx.sptys,xhmx.sdw,xhmx.iddep,
104 xhmx.idowner,xhmx.idkh,xhmx.idkh,xhmx.dbnumber,xhmx.sph,xhmx.scw,xhmx.sfqdb,
105 xhmx.idfqdh,xhmx.idxhdh,left(xhmx.idxhdh,4),xhmx.dbprice,xhmx.dbmoney,xhmx.sgg,
106 'f8d6171e3fcdaed39f0919a9ec5f51ae',getdate(),'12979218489686204728368524150460',
107 xhmx.discount,xhd.scode+'-'+xhmx.sxh,'7c78c2e5fb906560a1ff52ea68a50b6d',xm.idazgcs,
108 xm.idfpr,xm.dfpsj,emp.iddep,xm.idxsht,'403fd62411e6c38ee1e7999ff53db78e',xhd.ddjrq,1
109 from sales xhd join salemx xhmx on xhd.id = xhmx.idxhdh join sazxm xm on xm.id = xhd.id
110 left join org_employee emp on emp.id = xm.idazgcs
111 where xhmx.id not in (select id from ssbazrw)
112 and xhd.id in (select id from sazxm) and xhmx.sfscazrw = 0
113 break
114 end else begin
115 insert into ssbazrw (id,idazxm,sazxmdb,syspos,idcpxx,idxhmxbh,swlmc,idpp,szjys,sptys,
116 sdw,iddep,idowner,idhtqykh,idzdkh,dbsl,sph,scw,sfqdb,idfqdh,idxhddh,sxhddb,dbprice,dbmoney,
117 swlgg,emazlx,created,createdby,szk,rwscode,emzt,idazgcs,idfpr,dfpsj,idazbm,idht,emsfyxlh,ddjrq,bzdscsj)
118 select xhmx.id+'-'+convert(varchar(100),@dbsl),xhmx.idxhdh,left(xhmx.idxhdh,4),xhmx.sxh,
119 xhmx.idcp,xhmx.id,xhmx.sname,xhmx.idpp,xhmx.szjys,xhmx.sptys,xhmx.sdw,xhmx.iddep,
120 xhmx.idowner,xhmx.idkh,xhmx.idkh,1,xhmx.sph,xhmx.scw,xhmx.sfqdb,idfqdh,xhmx.idxhdh,
121 left(xhmx.idxhdh,4),xhmx.dbprice,nullif(xhmx.discount,0) / nullif(xhmx.dbprice,0) * 1,xhmx.sgg,
122 'f8d6171e3fcdaed39f0919a9ec5f51ae',getdate(),'12979218489686204728368524150460',xhmx.discount,
123 xhd.scode+'-'+xhmx.sxh+'-'+convert(varchar(100),@dbsl),'7c78c2e5fb906560a1ff52ea68a50b6d',
124 xm.idazgcs,xm.idfpr,xm.dfpsj,emp.iddep,xm.idxsht,'f3406c8577eb9b62f9e5cf8a1895e8c2',xhd.ddjrq,1
125 from sales xhd join salemx xhmx on xhd.id = xhmx.idxhdh join sazxm xm on xm.id = xhd.id
126 left join org_employee emp on emp.id = xm.idazgcs
127 where xhmx.id+'-'+convert(varchar(100),@dbsl) not in (select id from ssbazrw) and xhmx.id = @id
128 and xhd.id in (select id from sazxm) and xhmx.sfscazrw = 1
129 set @dbsl = @dbsl + 1
130 if @dbsl > @maxdbsl begin break end
131 end
132 end
133 end
134 set @numrows = @numrows + 1
135 if @numrows > @maxnumrows begin break end
136 end
137 DROP TABLE #sazxmlist
138 DROP TABLE #amxtemplist
139
140 --安装任务明细总条数
141 update xm set xm.dbmxts = mx.tl from sazxm xm join
142 (select COUNT(*) tl,rw.idazxm id from ssbazrw rw group by rw.idazxm) mx
143 on mx.id = xm.id
144
145 --生成搬运任务
146 insert into sbyrw(id,scode,idazxm,idxsht,idhtqykh,idzdkh,idowner,iddep,idxhdh,dbmoney,idywy,idywbm,bzdscsj)
147 select xhd.id,'BY'+replace(convert(char(20),xhd.ddjrq,112)+right(xhd.id,3),' ',''),xhd.id,
148 xhd.idxmh,xhd.idkh,xhd.idkh,ht.idfwgcs,emp.iddep,xhd.id,xhd.dbmoney,ht.idowner,ht.iddep,1
149 from sales xhd join [contract] ht on ht.id = xhd.idxmh left join org_employee emp
150 on emp.id = ht.idfwgcs join sazxm xm on xm.id = xhd.id where xhd.id not in (select id from sbyrw) and
151 --datediff(dd,xhd.ddjrq,getdate()) = 0 and
152 ht.emcarry = 'bbe251eb0f1d867ed89eea05523a72f4'
153 end
154  

转载于:https://www.cnblogs.com/RainHouse/p/9606510.html

sql Server 创建临时表 嵌套循环 添加数据相关推荐

  1. Sql Server 创建临时表

    Sql Server 创建临时表 语法 创建临时表 方法一:create table #临时表名(字段1 约束条件,字段2 约束条件,.....)create table ##临时表名(字段1 约束条 ...

  2. SQL语句创建表并添加数据

    SQL语句创建表并添加数据 创建表 use school 指定要用的数据库 go create table student 创建表 ( ID bigint identity(1,1), 添加ID,id ...

  3. SQL Server创建表和添加列

    撰写时间:2022 年 4 月 27日 SQLServer创建表和添加列 SQL Server创建表: 表用于在数据库中存储数据:表在数据库和模式中唯一命名.每个表包含一个或多个列.每列都有一个相关的 ...

  4. SQL Server创建数据库和数据的增删改查

    SQL Server创建数据库和数据的增删改查 本文是针对数据的增删改查,数据的创建 首先我们使用命令创建sql server数据库 D盘新建一个文件夹DB use master go --创建数据库 ...

  5. SQL Server创建表语句介绍

    SQL Server创建表是最常见也是最常用的操作之一,下面就为您介绍SQL Server创建表的语句写法,供您参考,希望可以让您对SQL Server创建表方面有更深的认识. USE suntest ...

  6. 如何使用SQL Server链接服务器查询Excel数据

    Linked servers allow SQL Server access to data from a remote data source. A remote data source can b ...

  7. CREATE VIEW SQL:通过SQL Server中的视图插入数据

    This is the third article in a series of learning the CREATE VIEW SQL statement. So far, I'd say tha ...

  8. 局域网内两台SQL Server电脑如何实时同步数据

    局域网内两台SQL Server电脑如何实时同步数据 2007-04-10 15:56 在数据库事务处理系统中,数据的同步性问题非常重要,下面我们来建立一个能检测数据同步性的存储过程.         ...

  9. Sql Server函数全解三数据类型转换函数和文本图像函数

    原文:Sql Server函数全解<三>数据类型转换函数和文本图像函数 一:数据类型转换函数 在同时处理不同数据类型的值时,SQL Server一般会自动进行隐士类型转换.对于数据类型相近 ...

最新文章

  1. 关于Java的23种设计模式的有趣见解
  2. Python 的文件IO相关操作说明
  3. 如何在Debian 8上安装Percona XtraDB Cluster for MySQL
  4. 百度seo排名规则_SEO:百度搜索引擎排名规则都有哪些?
  5. 使用L2TPV3桥接---FR-TO-PPP
  6. @class与#import
  7. 洛克菲勒的38封信pdf下载_洛克菲勒写给儿子的38封信:要有竞争的决心
  8. 恶意代码分析实战学习——静态分析
  9. ssm基于Android的签到系统APP(ssm+uinapp+Mysql)
  10. 深入理解8583协议
  11. 2分钟了解全球智慧城市趋势,解码万亿美元大市场的机遇与格局
  12. 地平线摄像头的音频调试至国际平台
  13. 可捕捉的 Microsoft Jet错误
  14. 删除二叉树节点完整c语言程序以及例子,C语言C++实现二叉树构造与查找删除节点.doc...
  15. 百度地图动态修改图标
  16. 软件工程_绘制系统业务流程图
  17. VMware环境部署vFW虚拟防火墙
  18. 【窗体控件】:Button按钮控件
  19. IDEA中的路径问题
  20. 施密特正交化(Schmidt)

热门文章

  1. 什么是你的核心竞争力?
  2. OpenFOAM-v2006重叠网格挖洞问题研究以及overset代码解析(二)PATCH是什么类型的patch?
  3. STM32实战六 PWM加移相正交
  4. 江湖中常说的“网格交易法”到底是什么?
  5. C语言常见问题(11):a label can only be part of a statement and a declaration is not a statement
  6. 《Python 源码剖析》一些理解以及勘误笔记(3)
  7. docker修改系统时间总结
  8. mac重装系统之后删除容器中的其他卷宗内容
  9. 中标麒麟安装node、nginx
  10. ubuntu无法调整分辨率