-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbo.zsp_MonthlyRollForwardsPartitionsOnDemand.StoredProcedure.sql
65 lines (62 loc) · 1.66 KB
/
dbo.zsp_MonthlyRollForwardsPartitionsOnDemand.StoredProcedure.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
USE [MonthlyPartitionsTest]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[zsp_MonthlyRollForwardsPartitionsOnDemand]
@TableName nvarchar(128) = N'Test_Monthly'
, @Date date --= null
, @RollForwardsLimit int = 1
as
/*
--单元测试
--重建分区表
--exec zsp_CreateSampleTable
--生成测试数据
--exec zsp_GenerateSampleMonthlyData '2030-10-2', 30
--滚动分区之前
--exec [zsp_TablesPartitionsInfoQuery]
--按需滚动分区并使目标日期参数所在月独占使用分区 [YYYY-02-01, YYYY-03-01)
exec [zsp_MonthlyRollForwardsPartitionsOnDemand] 'Test_Monthly', '2024-07-01'
--滚动分区之后
--exec [zsp_TablesPartitionsInfoQuery]
--查询Sample表分区中数据
exec zsp_SampleMonthlyDataPartitionsInfoQuery
--获取日期所在分区编号
--select dbo.SVF_GetMonthlyPartitionNo('Test_Monthly', '2026-01-01')
--获取日期所在按月独占分区编号 (-1: 该日期无该月独占分区, 0: 该日期已滚动过期, N: 该日期的独占按月分区编号)
--select dbo.SVF_GetMonthlyExclusivePartitionNo('Test_Monthly', '2026-01-01')
*/
begin
set xact_abort on
declare @MonthlyExclusivePartitionNo int = -1
declare @ int = 0
while (@MonthlyExclusivePartitionNo < 0)
begin
begin transaction
select
@MonthlyExclusivePartitionNo
= dbo.[SVF_GetMonthlyExclusivePartitionNo]
(
@TableName
, @Date
)
--select 1/0
if (@MonthlyExclusivePartitionNo < 0)
begin
exec [zsp_MonthlyRollForwardPartitionsOnce]
@TableName
--select 1/0
set @ += 1
end
--select 1/0
commit transaction
--select @@error as error
if (@ >= @RollForwardsLimit)
begin
break
end
end
end
GO