Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 35736

Creating Complex Test Databases - Generating One Table for each of the 2,397 supported Collations

$
0
0

As a follow up to my post on complex test databases, this article will cover one of the more interesting test databases that I recently created. 

 

GoalCreate a database that includes one table for each supported collation.  Each table contains a single column with the various column level collations supported by SQL Server 2012 (nearly 2,400 different collations supported).

 

The first step here is to determine where we can find the exhaustive list of supported collations.  The answer comes from the built-in table-valued function, fn_helpcollations, that returns the list of supported collations in SQL Server 2012.  Once we have the exhaustive list of supported collations, we need to determine how we will leverage this list to create one table for each collation.  If we were to do this manually, we would write out the following CREATE TABLE statements:

createtableT1         (c1nvarchar(50)collateAlbanian_100_BIN)

createtableT2         (c1nvarchar(50)collateAlbanian_100_BIN2)

createtableT3         (c1nvarchar(50)collateAlbanian_100_CI_AI)

createtableT2395      (c1nvarchar(50)collateYakut_100_CS_AS_KS)

createtableT2396      (c1nvarchar(50)collateYakut_100_CS_AS_KS_WS)

createtableT2397      (c1nvarchar(50)collateYakut_100_CS_AS_WS)

 

The repetition of these statements makes them good candidates for scripting using T-SQL.  By leveraging the ROW_NUMBER function as the table numeric identifier, we're able to put together the following statement:

 

SELECT'create table T'+cast(ROW_NUMBER()over (orderbyname)aschar(10))+'(c1 nvarchar(50) collate '+name+')'
FROM::fn_helpcollations();
 

The output of this statement is exactly the 2,397 CREATE TABLE scripts needed to create our database.  Copy the results and run them in an empty DB to create our desired database.

By leveraging the same language constructs, we could also create different flavors of this same DB. 

Example 1:  1 table with 1024 columns of different collations 
Example 2:  Modify the script to produce one CREATE DATABASE script for each collation

Hope you enjoy,
Sam Lester (MSFT)


Viewing all articles
Browse latest Browse all 35736

Trending Articles