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 .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...
随机推荐
- 有源点最短路径--Dijkstra算法
问题描述:一个带权有向图G与源点v,求从源点v到G中其他顶点的最短路径,并限定各边权值大于0 它的思想在于,对顶点集划分为两组,第一组为已经求出的最短路径的集合(S),期初只有一个顶点,此后每求出一个 ...
- css实现3D切换功能
Demo asdasdasd <!DOCTYPE html> <html lang="en"> <head> <title>De ...
- Odoo Tech World 2018(上海)互联网开源技术大会通告
会议概述 点击进入活动报名通道 高成本的软件开发,耗时的系统安装,繁琐的操作培训… 这一系列问题都是企业数字化管理的痛点, "软件"成为发展数企业数字化转型的瓶颈, 无论是小厂家或 ...
- 一张图看懂 SQL 的各种 join 用法
下图展示了 LEFT JOIN.RIGHT JOIN.INNER JOIN.OUTER JOIN 相关的 7 种用法. 具体分解如下: 1.INNER JOIN(内连接) 2.LEFT J ...
- postgresql 使用pg_restore时显示role "root" does not exist的解决办法
在docker里恢复bakcup格式的数据库,结果提示role "root" does not exist 解决方法: 切换用户: su - postgres 然后再次运行命令: ...
- 20190429 - 如何访问 macOS 的 httpd、mysql 等服务
问题:在局域网,通过其它机器访问 macOS 上的服务时,提示拒绝. 解决:将 macOS 防火墙 80.3306 端口打开. macOS 内置防火墙,也许开启了防火墙.Icefloor 是一个老牌的 ...
- 如何取消-"插入耳机自动显示提示框"
首先我们打开控制面板->1,你可以直接搜索控制面板打开 2,你可以右击我的电脑->点击属性->左上角打开控制面板
- chrome谷歌浏览器开发者工具-网络带宽控制
有时候我们想在本地测试一下图片预加载loading的加载情况,如下图: 可无奈一般网络带宽都比较好,基本上看不到效果,图片一下子就加载出来了, 可能这个时候有些小伙伴想到的办法是用定时器延迟加载 其实 ...
- 解决关于:Oracle数据库 插入数据中文乱码 显示问号???
问题: oracle数据库,通过接口插入的中文数据乱码,中文变成了问号??? 解决方案: 计算机=>属性=>高级系统设置=>环境变量=>新建 变量名:NLS_LANG 值:SI ...
- BaiduSpeechDemo【百度语音SDK集成】(基于v3.0.8.1)
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 上一篇集成的是V3.0.7.3版本的SDK<BaiduSpeechDemo[百度语音SDK集成](基于v3.0.7.3)> ...