sqlsql-serverpartitioningdynamic-sqlsql-agent-job

How to Partition a Table by Month ("Both" YEAR & MONTH) and create monthly partitions automatically?


I'm trying to Partition a Table by both Year and Month. The Column through which I'll partition is a datetime type column with an ISO Format ('20150110', 20150202', etc).

For example, I have sales data for 2010, 2011, 2012. I'd Like the data to be partitioned by year and each year be partitioned by month as well. (2010/01, 2010/02, ... 2010/12, 2011/01, ... 2015/01...)

E.X:

Sales2010Jan, Sales2010Feb, Sales2011Jan, Sales2011Feb, Sales2012Dec, etc.

My Question is: is it even possible? If it is, how an I automate the process using SSIS?


Solution

  • SSIS is an ETL (extract, transform, load). This is not what you want to do. You just need to create DDL statements dynamically .

    I work with quarter below but it works as well with 1, 2 or X months if you want.

    If you want to partition the table, you first need to create the file, filegroups and partionned table and set the partitionning manually

    Creation of N+1 partitions for 2015 Q1 (before, Q1 and after Q2) on a table with an int identity PK and a datetime2 partitioned column. Update it to add months, make it monthly or whatever you need...

    Build the query used to add extra file groups after the right boundary and split the last partition

    the output of this query is a list of SQL queries that must be run in order.

    Execute the dynamic SQL

    Automation

    If you want to run it monthly and make sure the next 12 months are always created, use this Set @endDate = DATEADD(MONTH, 12, getdate())

    Finally

    Link

    Create job = 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

    While loop = https://dba.stackexchange.com/questions/57933/can-exec-work-with-while-loop-of-cursor