Pregunta

I've inherited a partitioned database with millions of rows and I've noticed there is a typo in the partition function:

CREATE PARTITION FUNCTION [Partition_by_customer](varchar(10)) AS RANGE RIGHT FOR VALUES (N'DMA', N'DMB', N'DMC', N'DMD', N'DME')

Is there an easy way to remove those unicode converters (as they are causing implicit conversions), everything should be a varchar(10)

Thanks Roger

¿Fue útil?

Solución

The implicit conversion of the Unicode literal to varchar is a one-time operation done when the DDL is executed. The "typo" ('N' national character prefix) is not part of the partition function definition nor used by SQL Server after creation. The Unicode literal is generated by SMO when the function definition scripted.

The actual boundary values are a SQL_VARIANT type and match the partition function data type definition regardless of the specified literal. The catalog view query below shows this:

SELECT
      prv.value AS BoundaryValue
    , SQL_VARIANT_PROPERTY(value, 'BaseType') AS DataType
    , SQL_VARIANT_PROPERTY(value, 'MaxLength') AS Length
FROM sys.partition_functions AS pf
JOIN sys.partition_range_values AS prv ON prv.function_id = pf.function_id
WHERE pf.name = N'Partition_by_customer';

Results:

+---------------+----------+--------+
| BoundaryValue | DataType | Length |
+---------------+----------+--------+
| DMA           | varchar  |     10 |
| DMB           | varchar  |     10 |
| DMC           | varchar  |     10 |
| DMD           | varchar  |     10 |
| DME           | varchar  |     10 |
+---------------+----------+--------+

Otros consejos

I don't believe it's possible to do that. The Microsoft documentation on ALTER PARTITION FUNCTION specifically states to only alter one as a means of combining or splitting the partitions, but anything such as changing the function logic they recommend creating a new partitioned table with the new function and loading it with the data from the old table.

Specifically this part of the documentation under Limitations and Restrictions I believe is relevant to your question:

Only use ALTER PARTITION FUNCTION for splitting one partition into two, or merging two partitions into one. To change the way a table is otherwise partitioned (for example, from 10 partitions to five partitions), exercise any of the following options. Depending on the configuration of your system, these options may vary in resource consumption:

Create a new partitioned table with the necessary partition function. Then, insert the data from the old table into the new table by using an INSERT INTO...SELECT FROM statement.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a dba.stackexchange
scroll top