Question

This question is related to a previous question, Grouping by or iterating through partitions in SQL

I need to support SQL Server 2005, 2008, 2008 R2, and cannot depend on the Enterprise or full version (must work in SQL Server Express).

What I am trying to do is create a user-defined computed column that is essentially either a row_number() or dense_rank() over a partition by clause. This needs to act like an index, in that whenever rows are added to the table, this user-defined column is automatically generated.

I looked over the following Microsoft link that explains how to create a column based on a function, http://msdn.microsoft.com/en-us/library/ms186755.aspx. It doesn't quite get there.

It may not be possible, especially without the full version of SQL Server. Any thoughts?

Was it helpful?

Solution

Main feature of partitioning is splitting single object into multiple relatively independent database objects. Partitioning allows:

  • switch partitions from one table to another immediately, because it becomes a meta data operation, rather than physical copy.
  • lock single partition instead of the whole table, thus giving several processes simultaneous independent access to each partition.
  • spread a single table over multiple db files, storing each partition in its own file
  • compress data and change some other properties for each partition individually.

It cannot be done by any other means.

So I think you are looking to a way to assign an ID to each combination of some key, which consists of multiple columns. There is a number of ways to do it, depending on the result you want to achieve:

  • Create an index on several columns. It may be clustered, it may be on primary key columns. It will aid searches and get you data sorted (in case of clustered index).
  • Create separate table with all your primary key columns and assign each combination a unique key (eg using IDENTITY property). Insert this key to your main table to use as "partition id". Insert values automatically using a trigger or a join.

Please note, that SQL Server can use only one column for partitioning.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top