SSIS 是一个 ETL(提取、转换、加载)。这不是你想做的。
您只需要动态创建 DDL 语句。
我使用下面的季度,但如果你愿意的话,它也适用于 1、2 或 X 个月。
如果要对表进行分区,首先需要创建文件、文件组和分区表并手动设置分区
在具有 int 标识 PK 和 datetime2 分区列的表上为 2015 年第一季度(之前、第一季度和第二季度之后)创建 N+1 个分区。
更新它以添加月份、每月或任何您需要的内容......
-
首先创建N个文件组:
Alter Database [Test] Add Filegroup [Part_Before2015]
Go
Alter Database Test Add Filegroup [Part_201501]
Go
Alter Database Test Add Filegroup [Part_201504]
Go
-
为每个文件组添加一个文件:
Alter Database [Test] Add FILE ( NAME = N'Part_Before2015', FILENAME = N'...\Part_Before2015.ndf' , SIZE = 5120KB , FILEGROWTH = 1024KB ) TO Filegroup [Part_Before2015]
Alter Database [Test] Add FILE ( NAME = N'Part_201501', FILENAME = N'...\Part_201501.ndf' , SIZE = 5120KB , FILEGROWTH = 1024KB ) TO Filegroup [Part_201501]
Alter Database [Test] Add FILE ( NAME = N'Part_201504', FILENAME = N'...\Part_201504.ndf' , SIZE = 5120KB , FILEGROWTH = 1024KB ) TO Filegroup [Part_201504]
-
在 datetime2 类型(或 date 甚至 datetime)上创建分区函数:
Create Partition Function RangePartFunction (datetime2)
as Range Right For Values ('20150101', '20150401')
-
在每个文件组 (N+1) 上使用分区函数创建分区方案:
Create Partition Scheme RangePartScheme as Partition RangePartFunction
To ([Part_Before2015], [Part_201501], [Part_201504])
-
根据其分区方案创建分区表:
Create TABLE [PartitionTable] (id int identity(0, 1) not null, date datetime2 not null, text char(8000))
On RangePartScheme (date) ;
-
在分区列和分区方案上添加聚集索引:
Create Clustered Index IDX_Part On dbo.PartitionTable(date)
On RangePartScheme (date);
-
将 PK 添加到 id 列:
Alter Table dbo.PartitionTable Add COntraint PK_Part Primary Key Nonclustered(id, date);
构建用于在右边界后添加额外文件组并分割最后一个分区的查询
- 回顾分区方案扩展和分区函数拆分
- 审查使用的 DMV
-
查看所有这些以及如何使用它来创建动态 SQL
Declare @currentDate datetime2
Declare @endDate datetime2 = '20160701' -- new end date
Declare @dateAdd int = 3 -- Add 3 month = 1 Quarter
-- Get Current boundaries
Select @currentDate = DATEADD(MONTH, @dateAdd,Cast(MAX(value) as datetime2)) From sys.partition_range_values as r
Inner Join sys.partition_functions as f on r.function_id = f.function_id
Where f.name = 'RangePartFunction'
-- Get all quarters between max and end date
; with d(id, date, name) as (
Select 0, @currentDate, Convert(char(6), @currentDate, 112)
Union All
Select id+1, DATEADD(MONTH, @dateAdd, date), Convert(char(6), DATEADD(MONTH, @dateAdd, date), 112)
From d Where d.date <= @endDate
)
Select * From (
Select id = id*10, query = 'If Not Exists(Select 1 From sys.filegroups Where name = ''Part_'+name+''')
Begin
Print ''Create Filegroup [Part_'+name+']''
Alter Database [Test] Add Filegroup [Part_'+name+']
End
GO'
From d
Union All
Select id*10+1, 'If Not Exists(Select 1 From sys.sysfiles Where name = ''Part_'+name+''')
Begin
Print ''Create File [Part_'+name+'.ndf]''
Alter Database [Test] Add FILE ( NAME = N''Part_'+name+''', FILENAME = N''C:\DB\MSSQL11.MSSQLSERVER\MSSQL\DATA\Part_'+name+'.ndf'' , SIZE = 5120KB , FILEGROWTH = 1024KB ) TO Filegroup [Part_'+name+']
End
GO'
From d
Union All
Select id*10+2, 'Print ''Add Range [Part_'+name+']''
Alter Partition Scheme RangePartScheme Next Used [Part_'+name+']
Go'
From d
Union All
Select id*10+3, 'Print ''Split Function ['+Convert(char(8), date, 112)+']''
Alter Partition Function RangePartFunction() Split Range ('''+Convert(char(8), date, 112)+''');
Go'
From d
) as q order by id
该查询的输出是必须按顺序运行的 SQL 查询的列表。
执行动态SQL
- 可以手动执行(在SSMS中复制粘贴)
- 它可以在 while 循环中执行,也可以使用游标执行,游标将逐行执行输出表的每一行(使用 sp_executesql)
自动化
- 创建执行 SQL 查询的 SQL Server 作业:运行用于创建动态 SQL 的查询,将其输出保存到表变量中,然后使用循环/游标执行每个语句
如果您想每月运行一次并确保始终创建接下来的 12 个月,请使用此Set @endDate = DATEADD(MONTH, 12, getdate())
Finally
Link
创建工作 =https://www.mssqltips.com/sqlservertip/3052/simple-way-to-create-a-sql-server-job-using-tsql/ https://www.mssqltips.com/sqlservertip/3052/simple-way-to-create-a-sql-server-job-using-tsql/
sp_executesql =https://technet.microsoft.com/en-us/library/ms188001%28v=sql.110%29.aspx https://technet.microsoft.com/en-us/library/ms188001%28v=sql.110%29.aspx
while循环=https://dba.stackexchange.com/questions/57933/can-exec-work-with-while-loop-of-cursor https://dba.stackexchange.com/questions/57933/can-exec-work-with-while-loop-of-cursor