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. HTML 练习js代码位置在head标签

    方式一: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  2. 全球第一免费开源ERP Odoo仓存功能模块深度应用(一)

    基本功能 库位 库位是一个逻辑存货区,可以是一个物理库区,可以是一个货架.货架上的一个货位.库位可以有子库位 库位有虚拟库位和实际库位,实际库位是实际存放货物的库位,虚拟库位是因复式库存记账而虚构的库 ...

  3. PHP Socket编程(转)

    [PHPsocket编程专题(理论篇)]初步理解TCP/IP.Http.Socket.md [PHPsocket编程专题(实战篇①)]php-socket通信演示 [PHPsocket编程专题(实战篇 ...

  4. 不一样的 SQL Server 日期格式化

    不一样的 SQL Server 日期格式化 Intro 最近统计一些数据,需要按天/按小时/按分钟来统计,涉及到一些日期的格式化,网上看了一些文章大部分都是使用 CONVERT 来转换的,SQL Se ...

  5. 正确的git开发流程

    正确的git开发流程 第一步 在github中创建一个新的仓库,这时候项目是空的,而且只有一个master分支 第二步 第一个开发人员进来了,他在本地创建一个develop分支,并且提交到远程 git ...

  6. MongoDB 文章目录

    基础: MongoDB入门系列(一):基础概念和安装 MongoDB入门系列(二):Insert.Update.Delete.Drop MongoDB入门系列(三):查询(SELECT) MongoD ...

  7. TCP三次握手和四次挥手的全过程

    三次握手: 第一次握手:客户端发送syn包(syn=x)到服务器,并进入SYN_SEND状态,等待服务器确认:第二次握手:服务器收到syn包,必须确认客户的SYN(ack=x+1),同时自己也发送一个 ...

  8. QPainterPath 不规则提示框(二)

    前一篇讲过不规则提示框,但是提示框的方向是固定的,不能达到随意变换方向的效果,本接讲述可以动态变换方向的提示框 先看效果图 图1 图2 图3 图4 如上图1所示,上一篇文章的代码可以达到类似效果 ​本 ...

  9. 基于SpringBoot实现定时任务的设置(常用:定时清理数据库)

    1.构建SpringBoot工程项目 1)创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务. @SpringBootApplication @Ena ...

  10. 初探Parcel

    昨天趁有点时间看了前不久很火的构建工具Parcel,这里说下初步使用的感受,尤其是将其放到实际项目中和Webpack进行比较. 一.前言 首先说下笔者目前的技术栈.最近的前端项目主要以管理后台为主,技 ...