I have a script to generate the create table script. You can see it on my answer to this question.
However, now I need to script my partition functions and partition schemes
On this question here, see the answer has examples on how to add a partition to a table and how to remove a partition from a table, and I would like to have a look at the scripts at any stage.
I have found this link: how to find partition function text applied to a table
how to generate the scripts for create partition function and partition schema?
I was working on a script to generate the create partition function
as you can see below, but it only works for my own PF_year
partition function
because it was
tricky to get over the fact that sys.partition_range_values has a sql_variant data type as value.
when I used a case I got the following error:
Msg 206, Level 16, State 2, Line 35
Operand type clash: int is incompatible with date
Then in his great answer Dan Guzman, showed us SQL_VARIANT_PROPERTY some wonderful thing that I was not aware of, now I am a new DBA and person.
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT
RADHE = 'CREATE PARTITION FUNCTION' + space(1) + quotename(spf.name) +
'(' +
COALESCE(baset.name,'data type not found') + ')' + CHAR(13) +
'AS RANGE ' + CASE WHEN CAST(spf.boundary_value_on_right AS int) = 1
THEN 'RIGHT ' ELSE 'LEFT ' END + CHAR(13) +
'FOR VALUES (' +
(SELECT STUFF(
(SELECT
[text()] = N',''' + CAST(
CASE st.system_type_id
WHEN 40 THEN CONVERT (date, sprv.value,126)
--WHEN 56 THEN CONVERT (int, sprv.value)
--ELSE CONVERT (int, sprv.value)
END
AS NVARCHAR) + ''''
FROM sys.partition_range_values sprv
WHERE sprv.function_id=spf.function_id
order by sprv.boundary_id
FOR XML PATH('') ), 1, 1,N'')+ N');')
,st.system_type_id
,spf.name AS [Name],
spf.function_id AS [ID],
CAST(spf.boundary_value_on_right AS int) AS [RangeType],
spf.create_date AS [CreateDate],
spf.fanout AS [NumberOfPartitions]
FROM
sys.partition_functions AS spf
INNER JOIN sys.partition_parameters AS spp
ON spp.function_id=spf.function_id
INNER JOIN sys.types AS st
ON st.system_type_id = st.user_type_id
and spp.system_type_id = st.system_type_id
LEFT OUTER JOIN sys.types AS baset
ON ( baset.user_type_id = spp.system_type_id
and baset.user_type_id = baset.system_type_id)
or ( (baset.system_type_id = spp.system_type_id)
and (baset.user_type_id = spp.user_type_id)
and (baset.is_user_defined = 0)
and (baset.is_assembly_type = 1))
WHERE spf.name = 'PF_YEAR';