I have partitioned a table on a column with varchar datatype.
The documentation on truncate table with partitioning uses the partitionids:
Truncate table parttable with (partitions (1,2,5))
I only have a list of the actual values from the partitioned column.
How can I get the partitionids from a list of values so I can use it in the truncate table statement?
Background:
I’m still very new to partitioning and need to validate my plan.
I have a fact table that is partitioned on a column with varchar datatype.
I have an etl process that loads data fully in the beginning of the month.
The data loads after that only contain a subset (the partitioned column) and needs to fully replace the existing data in the fact table.
My plan is:
-
Load the data first to a staging table.
-
Truncate the fact table on the partitioned values in the staging table.
-
Insert the data in the staging table to the fact table.
Am I doing this correctly?
Best Answer
Since you didn't provide a minimal, complete, and verifiable example, I've created one:
That will create a partition scheme, a partition function, and a partitioned table with 30 rows inserted across the three paritions.
The following query determines which values lie on what partitions:
Note in the Common Table Expression (CTE), there is a
WHERE
clause limiting the output to the values (1) and (12) in thei
column of thedbo.pt
table.The output looks like:
As you can see, those values lie on partitions 1 and 3 respectively.
You could extend the query above by joining to a #temp table with the list of values you need the
partition_id
values for.The following code will automatically truncate the partition for the row containing the value
14
: