• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

分享一个自动生成单据的存储过程

mysql 搞代码 4年前 (2022-01-09) 12次浏览 已收录 0个评论

近期接到一个需求需要将一个大的单据拆分成多个小单据,这几个小单据主信息要相同,除了单号和ID外其他都要一样,于是 我考虑了借用游标和循环来进行拆分数据,并最终实现了该方法,通过了测试,代码如下: IF OBJECT_ID(N’tempdb..#tmpOrgList’, ‘U’) IS NO

近期接到一个需求需要将一个大的单据拆分成多个小单据,这几个小单据主信息要相同,除了单号和ID外其他都要一样,于是

我考虑了借用游标和循环来进行拆分数据,并最终实现了该方法,通过了测试,代码如下:

IF OBJECT_ID(N’tempdb..#tmpOrgList’, ‘U’) IS NOT NULL
drop table #tmpOrgList

—获取所有有客户的分公司的列表
select distinct OrgID into #tmpOrgList from v_WS400TelSurveyCustDetl

–声明新建单据的ID和BillNo
declare @intBillID int=0
declare @chvBillNO nvarchar(20)

—季度首月的第一天
declare @dtLastQtStart datetime
—季度的年和季度
declare @intYearLastQt int
declare @intLastQt int

—自动执行时赋值当前年度和当前季度
set @dtLastQtStart=DATEADD(qq, DATEDiff(qq,1,GETDATE()), 0)
set @intYearLastQt=Year(@dtLastQtStart)
set @intLastQt=DATEPART(QQ,@dtLastQtStart)

—手动执行时重新赋值指定的年度和季度
if @Flage=1
begin
set @intYearLastQt=@intYear
set @intLastQt=@intQt
end

—获取所有的抽查明细列表,去除重复的数据
IF OBJECT_ID(N’tempdb..#tmpSVDetlList’, ‘U’) IS NOT NULL
drop table #tmpSVDetlList

select distinct
[OrgID],[CompanyID],[DistributorID],[CompBranchID],[CustID],[CustChannelID]
,[CustName],isnull([CustContact],”) as Contacts,isnull([MobilePhone],”) as MobilePhone,
isnull([CustTel],”) as ContactPhone,
[CustAd

本文来源gaodai.ma#com搞##代!^码@网3

dr] as ClientAddress,[AreaID],[AreaType],[AreaName]
,[IsShow],GETDATE() as CreateTime,2 as BillerID,GETDATE() as ModifyTime
into #tmpSVDetlList
from v_WS400TelSurveyCustDetl

declare @intOrgSVDetlCount int
declare @intOrgSVDetlPage int
declare @intPageCount int
declare @intPage int=0

—每个分公司明细超过500条就开始拆分
set @intPageCount=500

DECLARE WS400_Cursor Cursor Local For
SELECT OrgID FROM #tmpOrgList
OPEN WS400_Cursor
DECLARE @intOrgID int
FETCH NEXT FROM WS400_Cursor INTO @intOrgID

WHILE @@FETCH_STATUS=0
BEGIN
BEGIN TRY

—判断所属分公司数据量是否超过了500条,超过了则拆分成几条数据来插入
—先生成单个分公司的抽查数据并插入临时表中
IF OBJECT_ID(N’tempdb..#tmpOrgSVDetlList’, ‘U’) IS NOT NULL
drop table #tmpOrgSVDetlList

select
@intBillID as BillID,ROW_NUMBER() over (order by CompanyID asc,CustID asc) as SNO,
[OrgID],[CompanyID],[DistributorID],[CompBranchID],[CustID],[CustChannelID]
,[CustName],[Contacts],[MobilePhone],[ContactPhone],
[ClientAddress],[AreaID],[AreaType],[AreaName]
,[IsShow],GETDATE() as CreateTime,2 as BillerID,GETDATE() as ModifyTime
into #tmpOrgSVDetlList
from #tmpSVDetlList
where OrgID=@intOrgID

—获取当前这个分公司的所有抽查数据的数量
select @intOrgSVDetlCount=COUNT(1) from #tmpOrgSVDetlList
set @intOrgSVDetlPage=@intOrgSVDetlCount/@intPageCount
set @intPage=0
—分批循环插入
while @intPage<=@intOrgSVDetlPage
begin
—-如果明细数量刚好是分页整数倍则最后一次拆分不执行
if @intPage*@intPageCount=@intOrgSVDetlCount
begin
continue
end
—获取ID
exec dbo.prGetIdentityNoTrans ‘WS400TelSurvey’,@intBillID output
—获取BillNO
exec dbo.prGetBillNo 40001,-1,-1,-1,@chvBillNO output
—插入主表
insert into WS400TelSurvey
(
BillID,
BillNO,
OrgID,
SVYear,
SVquarter,
CreateTime,
BillerID,
ModifyTime,
Remark
)
select @intBillID,@chvBillNO,@intOrgID,@intYearLastQt,@intLastQt,GETDATE(),2,GETDATE(),”
–插入明细表
insert into WS400TelSurveyDetl(
BillID,
SNO,
OrgID,
CompanyID,
DistributorID,
CompBranchID,
CustID,
CustChannelID,
CustName,
Contacts,
MobilePhone,
ContactPhone,
ClientAddress,
AreaID,
AreaType,
AreaName,
IsShow,
CreateTime,
BillerID,
ModifyTime
)
select
@intBillID as [BillID],[SNO],
[OrgID],[CompanyID],[DistributorID],[CompBranchID],[CustID],[CustChannelID]
,[CustName],[Contacts],[MobilePhone],[ContactPhone],
[ClientAddress],[AreaID],[AreaType],[AreaName]
,[IsShow],GETDATE() as CreateTime,2 as BillerID,GETDATE() as ModifyTime
from #tmpOrgSVDetlList
where OrgID=@intOrgID and SNO<=(@intPage+1)*@intPageCount
and SNO>@intPageCount*@intPage

set @intPage=@intPage+1
end

IF OBJECT_ID(N’tempdb..#tmpOrgSVDetlList’, ‘U’) IS NOT NULL
drop table #tmpOrgSVDetlList

END TRY
BEGIN CATCH
print ‘error’
END CATCH
FETCH NEXT FROM WS400_Cursor INTO @intOrgID
END

CLOSE WS400_Cursor
DEALLOCATE WS400_Cursor

IF OBJECT_ID(N’tempdb..#tmpOrgList’, ‘U’) IS NOT NULL
drop table #tmpOrgList

IF OBJECT_ID(N’tempdb..#tmpSVDetlList’, ‘U’) IS NOT NULL
drop table #tmpSVDetlList


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:分享一个自动生成单据的存储过程

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址