sql Server 创建临时表 嵌套循环 添加数据
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 创建临时表 嵌套循环 添加数据的更多相关文章
- sql server创建临时表的两种写法和删除临时表
--创建.删除临时表 --第一种方式 create table #tmp(name varchar(255),id int) --第二种方式 select count(id) as storyNum ...
- SQL Server通过创建临时表遍历更新数据
前言: 前段时间新项目上线为了赶进度很多模块的功能都没有经过详细的测试导致了生成环境中的数据和实际数据对不上,因此需要自己手写一个数据库脚本来更新下之前的数据.(线上数据库用是SQL Server20 ...
- sql Server中临时表与数据表的区别
sql server 中临时表与数据表的区别 1.如何判断临时表和数据表已生成 --如何判断临时表是否已创建--- if exists(select * from tempdb..sysobjects ...
- SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学、字符串、日期时间)函数[转]
SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学.字符串.日期时间)函数 --创建表格 create table aa ( UserName varchar(50 ...
- SQL Server 创建索引方法
转自 <SQL Server 创建索引的 5 种方法> 地址:https://www.cnblogs.com/JiangLe/p/4007091.html 前期准备: create tab ...
- 【转】SQL Server 创建约束图解 唯一 主键-界面操作
SQL Server 创建约束图解 唯一 主键-界面操作 SQLServer中有五种约束,Primary Key约束.Foreign Key约束.Unique约束.Default约束和Check约束, ...
- SQL Server创建链接服务器
1.通过sql语句创建链接服务器,数据是sql server的 EXEC sp_addlinkedserver @server='test', --链接服务器别名,自定义 @srvproduct='' ...
- SQL Server 创建表
SQL Server 创建表 我们在上一节中完成了数据库的创建,在本节,我们要往这个新的数据库中加入点数据,要想将数据添加到数据库,我们就必须在数据库中添加一个表,接下来来看看具体的操作. 我们的数据 ...
- SQL Server创建索引(转)
什么是索引 拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...
随机推荐
- C# 插入超链接到PDF文档(3种情况)
超链接可以实现不同元素之间的连接,用户可以通过点击被链接的元素来激活这些链接.具有高效.快捷.准确的特点.本文中,将分享通过C#编程在PDF文档中插入超链接的方法.内容包含以下要点: 插入网页链接 插 ...
- Css3 笔记 动画 和定位属性
transform 变形属性属性:translate 平移,rotate 旋转, scale 缩放,skew 倾斜 ◆ translate :指定对象的2D平移第一个参数对应X轴,第二参数对应Y轴:如 ...
- 微信小程序后端开发(Java语言)笔记
前言: 因为是第一次真正接触后端开发,从编码到部署服务器到上线,所以做个笔记,也供和我一样的开发小白一些参考. 一.前期工作:开发环境与工具: 1. 编程语言:Java #笔者还没学PHP,只想 ...
- pytest进阶之fixture
前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unit ...
- shell脚本获取进程ID并杀死的实现及问题解析
经常需要杀死某个进程,操作了几次之后,对一个熟练的码农来说,就要制作自己的工具了.有些工具虽然很小,但是却能节省一大部分的时间. 输入某个进程的ID并杀死的方法.这种事情,一般是先搜索再进行优化,这种 ...
- [intellij IDEA]导入eclipse项目
1.因为最近eclipse在更新代码时经常卡死,就想将eclipse的项目迁移到idea.特意写下自己的经验,给迁移时遇到困难的朋友一些帮助 File -> new ->project f ...
- 通过数据分析告诉你北京Python开发的现状
相信各位同学多多少少在拉钩上投过简历,今天突然想了解一下北京Python开发的薪资水平.招聘要求.福利待遇以及公司地理位置.既然要分析那必然是现有数据样本.本文通过爬虫和数据分析为大家展示一下北京Py ...
- unity中ScriptableObject在assetbundle中的加载
转载请标明出处:http://www.cnblogs.com/zblade/ 以前都是写一些个人的调研博客,从今天开始,也写一些个人在开发中遇到的一些可以分享的趟坑博客,为后续的开发人员提供一些绵薄之 ...
- 总结C语言字符检测函数:isalnum、isalpha...
前言:最近一直在刷leetcode的题,用到isalnum函数,用man手册查找了一下,总共有13个相关函数如下: #include <ctype.h> int isalnum(int c ...
- [翻译] GCC 内联汇编 HOWTO
目录 GCC 内联汇编 HOWTO 原文链接与说明 1. 简介 1.1 版权许可 1.2 反馈校正 1.3 致谢 2. 概览 3. GCC 汇编语法 4. 基本内联 5. 扩展汇编 5.1 汇编程序模 ...