begin

 --通过销货单与明细,生成安装项目及明细,及判断明细是否拆分生成多条
--delete from sazxm
--delete from ssbazrw
--获取未生成项目的销货单号
select rowid = identity(int,1,1),a.id xmid
into #sazxmlist
from sales a where a.id not in (select id from sazxm) and
convert(char(20),a.ddjrq,112) >= convert(char(20),'2018-12-01',112)
--根据销货单 单据日期 生成 当天安装项目,公司安装的直接到待安装状态
insert into sazxm(id,scode,idxhdh,sazxmdb,emazlx,idxsht,idxsdd,idhtqykh,emsfwsby,emsfwsaz,iddep,
idowner,emshzt,dfhrq,dbjfzje,emlife,idzdkh,swlgs,dsfzdsc,created,createdby,djrq,bqbqr,idazgcs,
idfpr,dfpsj,sshdz,idxsjh,contacts,sdh,idtsfpr)
select xhd.id,replace(convert(char(20),xhd.ddjrq,112)+right(xhd.id,3),' ',''),xhd.id,'2F01',
'f8d6171e3fcdaed39f0919a9ec5f51ae',xhd.idxmh,xhd.idso,xhd.idkh,ht.emcarry,ht.install,xhd.iddep,xhd.idowner,
'2cdd6816b789e9dc5312a21e37232b46',xhd.dxhrq,xhd.dbmoney,ht.emlife,xhd.idkh,xhd.sfyfs,1,getdate(),
'',convert(date,xhd.ddjrq),0,ht.idfwgcs,'',
getdate(),xhd.sshdz,xhd.idxsjh,xhd.contacts,xhd.slxdh,'9eda31d9ea905fa4df78f3d8b9b642c0'
from sales xhd left join [contract] ht on ht.id = xhd.idxmh left join org_employee emp
on emp.id = ht.idfwgcs where xhd.id not in (select id from sazxm) and
convert(char(20),xhd.ddjrq,112) >= convert(char(20),'2018-12-01',112)
and xhd.sname in ('','','','') and ht.install = '9eda31d9ea905fa4df78f3d8b9b642c0'
--根据销货单 单据日期 生成 当天安装项目,客户安装的直接到待客服审核状态
insert into sazxm(id,scode,idxhdh,sazxmdb,emazlx,idxsht,idxsdd,idhtqykh,emsfwsby,emsfwsaz,iddep,
idowner,emshzt,dfhrq,dbjfzje,emlife,idzdkh,swlgs,dsfzdsc,created,createdby,djrq,bqbqr,
sshdz,idxsjh,contacts,sdh)
select xhd.id,replace(convert(char(20),xhd.ddjrq,112)+right(xhd.id,3),' ',''),xhd.id,'2F01',
'f8d6171e3fcdaed39f0919a9ec5f51ae',xhd.idxmh,xhd.idso,xhd.idkh,ht.emcarry,ht.install,xhd.iddep,xhd.idowner,
'c03cc4622209d4adfa082a96e73f8221',xhd.dxhrq,xhd.dbmoney,ht.emlife,xhd.idkh,xhd.sfyfs,1,getdate(),
'',convert(date,xhd.ddjrq),0,xhd.sshdz,xhd.idxsjh,xhd.contacts,xhd.slxdh
from sales xhd left join [contract] ht on ht.id = xhd.idxmh left join org_employee emp
on emp.id = ht.idfwgcs where xhd.id not in (select id from sazxm) and
convert(char(20),xhd.ddjrq,112) >= convert(char(20),'2018-12-01',112)
and xhd.sname in ('','','','') and ht.install = 'f908b77150a27c74415912c1c3a13656'
--无合同的 销货单 单据日期 生成 当天安装项目
insert into sazxm(id,scode,idxhdh,sazxmdb,emazlx,idxsdd,idhtqykh,iddep,
idowner,emshzt,dfhrq,dbjfzje,idzdkh,swlgs,dsfzdsc,created,createdby,djrq,bqbqr,
sshdz,idxsjh,contacts,sdh)
select xhd.id,replace(convert(char(20),xhd.ddjrq,112)+right(xhd.id,3),' ',''),xhd.id,'2F01',
'f8d6171e3fcdaed39f0919a9ec5f51ae',xhd.idso,xhd.idkh,xhd.iddep,xhd.idowner,
'20658d2abc23904dd1d9c0db20f04d71',xhd.dxhrq,xhd.dbmoney,xhd.idkh,xhd.sfyfs,1,getdate(),
'',convert(date,xhd.ddjrq),0,xhd.sshdz,xhd.idxsjh,xhd.contacts,xhd.slxdh
from sales xhd where xhd.id not in (select id from sazxm) and
convert(char(20),xhd.ddjrq,112) >= convert(char(20),'2018-12-01',112)
and xhd.sname in ('','','','') and xhd.idxmh is null --创建临时表,表中有3列:行号,id,数量,是否展开数量
select rowid = identity(int,1,1),mx.id,mx.dbnumber,mx.sfscazrw
into #amxtemplist
from #sazxmlist a join salemx mx on a.xmid = mx.idxhdh --DROP TABLE #amxtemplist
--select * from #amxtemplist
--声明变量:@numrows 当前行号,@maxnumrows 最大行号,@id 当前id,@maxdbsl 最大数量,@dbsl 初始数量,@sfcf 是否展开数量
declare @numrows int,@maxnumrows int,@id char(100),@maxdbsl int ,@dbsl int,@sfcf int select @numrows = MIN(RowID),@maxnumrows=MAX(RowID)
FROM #amxtemplist
print @numrows print @maxnumrows while @numrows <= @maxnumrows
begin
select @id = id,@maxdbsl = dbnumber
from #amxtemplist where rowid=@numrows select @id = (select id from salemx where id = @id)
set @maxdbsl = (select dbnumber from sales xhd join salemx xhmx on xhd.id = xhmx.idxhdh where xhmx.id = @id)
set @sfcf = (select sfscazrw from sales xhd join salemx xhmx on xhd.id = xhmx.idxhdh where xhmx.id = @id)
set @dbsl = 1
if (@dbsl = @maxdbsl)
begin
--生成单个设备安装任务
insert into ssbazrw (id,idazxm,sazxmdb,syspos,idcpxx,idxhmxbh,swlmc,idpp,szjys,
sptys,sdw,iddep,idowner,idhtqykh,idzdkh,dbsl,sph,scw,sfqdb,idfqdh,idxhddh,sxhddb,
dbprice,dbmoney,swlgg,emazlx,created,createdby,szk,rwscode,emzt,idazgcs,idfpr,
dfpsj,idazbm,idht,emsfyxlh,ddjrq,bzdscsj)
select xhmx.id,xhmx.idxhdh,left(xhmx.idxhdh,4),xhmx.sxh,xhmx.idcp,
xhmx.id,xhmx.sname,xhmx.idpp,xhmx.szjys,xhmx.sptys,xhmx.sdw,xhmx.iddep,
xhmx.idowner,xhmx.idkh,xhmx.idkh,xhmx.dbnumber,xhmx.sph,xhmx.scw,xhmx.sfqdb,
xhmx.idfqdh,xhmx.idxhdh,left(xhmx.idxhdh,4),xhmx.dbprice,xhmx.dbmoney,xhmx.sgg,
'f8d6171e3fcdaed39f0919a9ec5f51ae',getdate(),'',
xhmx.discount,xhd.scode+'-'+xhmx.sxh,'7c78c2e5fb906560a1ff52ea68a50b6d',xm.idazgcs,
xm.idfpr,xm.dfpsj,emp.iddep,xm.idxsht,'f3406c8577eb9b62f9e5cf8a1895e8c2',xhd.ddjrq,1
from sales xhd join salemx xhmx on xhd.id = xhmx.idxhdh join sazxm xm on xm.id = xhd.id
left join org_employee emp on emp.id = xm.idazgcs
where xhmx.id not in (select id from ssbazrw)
and xhd.id in (select id from sazxm) and xhmx.dbnumber = 1 and xhmx.sfscazrw = 1
end
else
begin
--数量大于1时,创建多条设备安装任务
while (@dbsl <= @maxdbsl)
begin
if(@sfcf = 0)
begin
--生成无需展开的安装任务
insert into ssbazrw (id,idazxm,sazxmdb,syspos,idcpxx,idxhmxbh,swlmc,idpp,szjys,
sptys,sdw,iddep,idowner,idhtqykh,idzdkh,dbsl,sph,scw,sfqdb,idfqdh,idxhddh,sxhddb,
dbprice,dbmoney,swlgg,emazlx,created,createdby,szk,rwscode,emzt,idazgcs,idfpr,
dfpsj,idazbm,idht,emsfyxlh,ddjrq,bzdscsj)
select xhmx.id,xhmx.idxhdh,left(xhmx.idxhdh,4),xhmx.sxh,xhmx.idcp,
xhmx.id,xhmx.sname,xhmx.idpp,xhmx.szjys,xhmx.sptys,xhmx.sdw,xhmx.iddep,
xhmx.idowner,xhmx.idkh,xhmx.idkh,xhmx.dbnumber,xhmx.sph,xhmx.scw,xhmx.sfqdb,
xhmx.idfqdh,xhmx.idxhdh,left(xhmx.idxhdh,4),xhmx.dbprice,xhmx.dbmoney,xhmx.sgg,
'f8d6171e3fcdaed39f0919a9ec5f51ae',getdate(),'',
xhmx.discount,xhd.scode+'-'+xhmx.sxh,'7c78c2e5fb906560a1ff52ea68a50b6d',xm.idazgcs,
xm.idfpr,xm.dfpsj,emp.iddep,xm.idxsht,'403fd62411e6c38ee1e7999ff53db78e',xhd.ddjrq,1
from sales xhd join salemx xhmx on xhd.id = xhmx.idxhdh join sazxm xm on xm.id = xhd.id
left join org_employee emp on emp.id = xm.idazgcs
where xhmx.id not in (select id from ssbazrw)
and xhd.id in (select id from sazxm) and xhmx.sfscazrw = 0
break
end else begin
insert into ssbazrw (id,idazxm,sazxmdb,syspos,idcpxx,idxhmxbh,swlmc,idpp,szjys,sptys,
sdw,iddep,idowner,idhtqykh,idzdkh,dbsl,sph,scw,sfqdb,idfqdh,idxhddh,sxhddb,dbprice,dbmoney,
swlgg,emazlx,created,createdby,szk,rwscode,emzt,idazgcs,idfpr,dfpsj,idazbm,idht,emsfyxlh,ddjrq,bzdscsj)
select xhmx.id+'-'+convert(varchar(100),@dbsl),xhmx.idxhdh,left(xhmx.idxhdh,4),xhmx.sxh,
xhmx.idcp,xhmx.id,xhmx.sname,xhmx.idpp,xhmx.szjys,xhmx.sptys,xhmx.sdw,xhmx.iddep,
xhmx.idowner,xhmx.idkh,xhmx.idkh,1,xhmx.sph,xhmx.scw,xhmx.sfqdb,idfqdh,xhmx.idxhdh,
left(xhmx.idxhdh,4),xhmx.dbprice,nullif(xhmx.discount,0) / nullif(xhmx.dbprice,0) * 1,xhmx.sgg,
'f8d6171e3fcdaed39f0919a9ec5f51ae',getdate(),'',xhmx.discount,
xhd.scode+'-'+xhmx.sxh+'-'+convert(varchar(100),@dbsl),'7c78c2e5fb906560a1ff52ea68a50b6d',
xm.idazgcs,xm.idfpr,xm.dfpsj,emp.iddep,xm.idxsht,'f3406c8577eb9b62f9e5cf8a1895e8c2',xhd.ddjrq,1
from sales xhd join salemx xhmx on xhd.id = xhmx.idxhdh join sazxm xm on xm.id = xhd.id
left join org_employee emp on emp.id = xm.idazgcs
where xhmx.id+'-'+convert(varchar(100),@dbsl) not in (select id from ssbazrw) and xhmx.id = @id
and xhd.id in (select id from sazxm) and xhmx.sfscazrw = 1
set @dbsl = @dbsl + 1
if @dbsl > @maxdbsl begin break end
end
end
end
set @numrows = @numrows + 1
if @numrows > @maxnumrows begin break end
end
DROP TABLE #sazxmlist
DROP TABLE #amxtemplist --安装任务明细总条数
update xm set xm.dbmxts = mx.tl from sazxm xm join
(select COUNT(*) tl,rw.idazxm id from ssbazrw rw group by rw.idazxm) mx
on mx.id = xm.id --生成搬运任务
insert into sbyrw(id,scode,idazxm,idxsht,idhtqykh,idzdkh,idowner,iddep,idxhdh,dbmoney,idywy,idywbm,bzdscsj)
select xhd.id,'BY'+replace(convert(char(20),xhd.ddjrq,112)+right(xhd.id,3),' ',''),xhd.id,
xhd.idxmh,xhd.idkh,xhd.idkh,ht.idfwgcs,emp.iddep,xhd.id,xhd.dbmoney,ht.idowner,ht.iddep,1
from sales xhd join [contract] ht on ht.id = xhd.idxmh left join org_employee emp
on emp.id = ht.idfwgcs join sazxm xm on xm.id = xhd.id where xhd.id not in (select id from sbyrw) and
--datediff(dd,xhd.ddjrq,getdate()) = 0 and
ht.emcarry = 'bbe251eb0f1d867ed89eea05523a72f4'
end

sql Server 创建临时表 嵌套循环 添加数据的更多相关文章

  1. sql server创建临时表的两种写法和删除临时表

    --创建.删除临时表 --第一种方式 create table #tmp(name varchar(255),id int) --第二种方式 select count(id) as storyNum ...

  2. SQL Server通过创建临时表遍历更新数据

    前言: 前段时间新项目上线为了赶进度很多模块的功能都没有经过详细的测试导致了生成环境中的数据和实际数据对不上,因此需要自己手写一个数据库脚本来更新下之前的数据.(线上数据库用是SQL Server20 ...

  3. sql Server中临时表与数据表的区别

    sql server 中临时表与数据表的区别 1.如何判断临时表和数据表已生成 --如何判断临时表是否已创建--- if exists(select * from tempdb..sysobjects ...

  4. SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学、字符串、日期时间)函数[转]

    SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学.字符串.日期时间)函数   --创建表格 create table aa ( UserName varchar(50 ...

  5. SQL Server 创建索引方法

    转自 <SQL Server 创建索引的 5 种方法> 地址:https://www.cnblogs.com/JiangLe/p/4007091.html 前期准备: create tab ...

  6. 【转】SQL Server 创建约束图解 唯一 主键-界面操作

    SQL Server 创建约束图解 唯一 主键-界面操作 SQLServer中有五种约束,Primary Key约束.Foreign Key约束.Unique约束.Default约束和Check约束, ...

  7. SQL Server创建链接服务器

    1.通过sql语句创建链接服务器,数据是sql server的 EXEC sp_addlinkedserver @server='test', --链接服务器别名,自定义 @srvproduct='' ...

  8. SQL Server 创建表

    SQL Server 创建表 我们在上一节中完成了数据库的创建,在本节,我们要往这个新的数据库中加入点数据,要想将数据添加到数据库,我们就必须在数据库中添加一个表,接下来来看看具体的操作. 我们的数据 ...

  9. SQL Server创建索引(转)

    什么是索引 拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...

随机推荐

  1. Java_基础篇(数组的反转)

    数组反转也是Java的基础. 数组反转要求掌握的是: 1).创建一个数组,在内存中申请一块空间. 2).实例化数组. 3).对数组的了解.如:数组的长度,数组的下标,数组的表示方法. 4).数组的交换 ...

  2. 基于ArcGISServer进行分页矢量查询的方案进阶

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.    背景 在空间查询中,我们对查询结果要求以分页形式进行展示.G ...

  3. linux中添加快捷命令

    例如我们需要ssh的时候,每次都要ssh ninetripod@10.0.0.11 -p 12345,这样显然很麻烦. 我们可以vim ~/.bashrc在里面添加alias ssh_='ssh ni ...

  4. Git使用详细教程(9):git log

    目录 格式化 检索 显示最近提交 Git中使用git log查看提交日志 如果日志很多的话,默认会以分页方式展示 空格可以翻下一页,ctrl+b翻上一页,q退出 格式化 如果想获取每条日志的简要信息, ...

  5. Sql Server 复制数据库

    确实很实用 https://www.cnblogs.com/ggll611928/p/7451558.html

  6. Redis内存模型(2):存储细节

    1. 概述 先看一下执行set hellow world时,所涉及的数据模型: (1)dictEntry:Redis是Key-Value数据库,因此对每个键值对都会有一个dictEntry,里面存储了 ...

  7. SQLServer之删除数据库架构

    删除数据库架构注意事项 要删除的架构不能包含任何对象. 如果架构包含对象,则 DROP 语句将失败. 可以在 sys.schemas 目录视图中查看有关架构的信息. 要求对架构具有 CONTROL 权 ...

  8. 在word中如何美观地插入代码

    打开这个网站 http://www.planetb.ca/syntax-highlight-word 进去后我们看到下面的界面 中间的空白文本框,可以插入代码,下面可以选择代码种类,最后点击Show ...

  9. this指针的初运用

    this一般运用场景: 1.位于函数中,谁调用指向谁 var make = "Mclaren"; var model = "720s" function ful ...

  10. Ansible安装及简单使用备注

    1.安装epel源: rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm 2.安装: yum ...