You are on page 1of 3

Tables

The start of each table creation script is as simple as CREATE TABLE:

2 SELECT @Schema_Build_Text = 'USE [' + @Database_Name + '];' + '

4'+

5 'CREATE TABLE [' + TABLE_DATA.Schema_Name + '].[' + TABLE_DATA.Table_Name + ']('

6 FROM @Tables TABLE_DATA

7 WHERE TABLE_DATA.Schema_Name = @Schema_Name_Current

8 AND TABLE_DATA.Table_Name = @Table_Name_Current

This first section sets the database context to whatever database we are analyzing and then puts
together the CREATE TABLE statement, filtered on the current table and schema we are working
on. Square brackets are used around all object names, which ensures that we are able to manage
any that have spaces in their names.

Columns
Next, we need to list out the columns in this table, in the correct order, with any relevant
attributes defined inline with the table creation. This is a bit more involved as we may have any
number of columns in a table and any number of attributes in any combination. Our TSQL needs
to be robust enough to deal with any combinations that our schema may throw at us. We’ll break
our workflow down as follows:

1. Output the column name.


2. Is the column computed? If so, skip other attributes and go straight to its definition.
3. Print out the data type, including the necessary details:
a. DECIMAL? If so, then include precision and scale.
b. VARCHAR/NVARCHAR/CHAR/NCHAR? If so, then include the column length.
4. If the column is sparse, include that attribute here.
5. Is the column an identity? If so, add that attribute, including the identity seed and
increment.
6. Is the column nullable? If so, specify NULL or NOT NULL here. While SQL Server defaults
columns to NULL when unspecified, we will be thorough and always include one or the
other here.
7. Is there a default constraint on this column? If so, include the constraint name and
definition here.
8. Is the column computed? If so, this is where the definition will go.
After all of these steps, we will have a column list, including important attributes. The TSQL to
generate this script segment is as follows:

1  

2 SELECT

3 @Schema_Build_Text = @Schema_Build_Text + '

4 ' + COLUMN_DATA.Column_Name + ' ' +

5 CASE WHEN COLUMN_DATA.Is_Computed = 1 THEN '' ELSE -- Don't add metadata if a

6 column is computed (just include definition)

7 COLUMN_DATA.Type_Name + -- Basic column metadata

8 CASE WHEN COLUMN_DATA.Type_Name = 'DECIMAL' THEN '(' +

9 CAST(COLUMN_DATA.Column_Precision AS NVARCHAR(MAX)) + ',' + CAST(COLUMN_DATA.Column_Scale AS

10 NVARCHAR(MAX)) + ')' ELSE '' END + -- Column precision (decimal)

11 CASE WHEN COLUMN_DATA.Type_Name IN ('VARCHAR', 'NVARCHAR', 'NCHAR',

12 'CHAR') THEN '(' + CAST(COLUMN_DATA.Column_Length AS NVARCHAR(MAX)) + ')' ELSE '' END + -- Column

13 length (string)

14 CASE WHEN COLUMN_DATA.is_sparse = 1 THEN ' SPARSE' ELSE '' END + -- If a

15 column is sparse, denote that here.

16 CASE WHEN COLUMN_DATA.Is_Identity = 1 THEN ' IDENTITY(' + CAST(Identity_Seed

17 AS NVARCHAR(MAX)) + ',' + CAST(Identity_Increment AS NVARCHAR(MAX)) + ')' ELSE '' END + -- Identity

18 Metadata (optional)

19 CASE WHEN COLUMN_DATA.Is_Nullable = 1 THEN ' NULL' ELSE ' NOT NULL' END +

20 -- NULL/NOT NULL definition

CASE WHEN COLUMN_DATA.Default_Constraint_Name IS NOT NULL THEN '

CONSTRAINT ' + COLUMN_DATA.Default_Constraint_Name + ' DEFAULT ' +

COLUMN_DATA.Default_Constraint_Definition ELSE '' END END + -- Default constraint definition (optional)

CASE WHEN COLUMN_DATA.Is_Computed = 1 THEN 'AS ' +

COLUMN_DATA.Computed_Column_Definition ELSE '' END + ','

FROM @Columns COLUMN_DATA

WHERE COLUMN_DATA.Table_Name = @Table_Name_Current

AND COLUMN_DATA.Schema_Name = @Schema_Name_Current

ORDER BY COLUMN_DATA.Ordinal_Position ASC;

SELECT @Schema_Build_Text = LEFT(@Schema_Build_Text, LEN(@Schema_Build_Text) - 1);


 

The filter ensures we only add columns for the current table we are working on, while the ORDER
BY puts our data in the correct column order, based on column ordinals. The list-building syntax
used here allows us to build the column list in a single statement from a set of columns of any
size. The final SELECT removes the trailing comma from the string, which is left over by the list-
building syntax.

You might also like