Last active
July 18, 2019 06:43
-
-
Save emilefraser/88e16948f7741c5af318896163dbfe74 to your computer and use it in GitHub Desktop.
[CreateTableFromFieldListIncludingTypes] Creating table from Field List Dynamically including type, scale and precision #table #create #dynamic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
-- Numeric DataTypes | |
CASE WHEN [f].[DataType] IN ('bigint', 'int', 'smallint', 'tinyint', 'bit', 'decimal', 'numeric', 'money', 'smallmoney', 'float', 'real') | |
THEN CASE WHEN [f].[DataType] IN ('decimal', 'numeric') | |
THEN '(' + CAST(f.[Precision] AS VARCHAR(5)) + ',' + CAST(f.[Scale] AS VARCHAR(5)) + ')' | |
WHEN [f].[DataType] IN ('float', 'real') | |
THEN '' | |
--THEN '(' + CAST(f.[Precision] AS VARCHAR(5)) + ')' | |
WHEN [f].[DataType] IN ('bigint', 'int', 'smallint', 'tinyint', 'bit', 'money', 'smallmoney') | |
THEN '' | |
ELSE '' | |
END | |
-- Date/Time DataTypes | |
WHEN [f].[DataType] IN ('datetime', 'datetime2', 'smalldatetime', 'date', 'time', 'datetimeoffset', 'timestamp') | |
THEN CASE WHEN [f].[DataType] IN ('datetime2', 'datetimeoffset', 'time') | |
THEN '(' + CAST(f.[Scale] AS VARCHAR(5)) + ')' | |
WHEN [f].[DataType] IN ('datetime', 'smalldatetime', 'date', 'time') | |
THEN '' | |
ELSE '' | |
END | |
-- char string types | |
WHEN [f].[DataType] IN ('char', 'nchar', 'varchar', 'nvarchar', 'varbinary', 'binary', 'text', 'ntext') | |
THEN CASE WHEN [f].[DataType] IN ('varchar', 'char', 'varbinary', 'binary') | |
THEN '(' + CASE WHEN f.[MaxLength] = -1 | |
THEN 'MAX' | |
ELSE CAST(f.[MaxLength] AS VARCHAR(5)) | |
END + ')' | |
WHEN [f].[DataType] IN ('nvarchar', 'nchar') | |
THEN '(' + CASE WHEN f.[MaxLength] = -1 | |
THEN 'MAX' | |
ELSE CAST(f.[MaxLength] / 2 AS VARCHAR(5)) | |
END + ')' | |
WHEN [f].[DataType] IN ('binary', 'text', 'ntext') | |
THEN '' | |
ELSE '' | |
END | |
-- spatial | |
WHEN [f].[DataType] IN ('geography', 'geometry', 'hierarchyid') | |
THEN CASE WHEN [f].[DataType] IN ('geography', 'geometry', 'hierarchyid') | |
THEN '' | |
ELSE '' | |
END | |
-- other types | |
WHEN [f].[DataType] IN ('sql_variant', 'uniqueidentifier', 'xml', 'image', 'sysname') | |
THEN CASE WHEN [f].[DataType] IN ('sql_variant', 'uniqueidentifier', 'xml', 'image', 'sysname') | |
THEN '' | |
ELSE '' | |
END | |
END | |
/* | |
-- Just keeping this for now for possible FUTURE | |
CASE WHEN c.collation_name IS NOT NULL AND c.system_type_id = c.user_type_id | |
THEN ' COLLATE ' + c.collation_name | |
ELSE '' | |
END + | |
CASE WHEN c.is_nullable = 1 | |
THEN ' NULL' | |
ELSE ' NOT NULL' | |
END + | |
CASE WHEN c.default_object_id != 0 | |
THEN ' CONSTRAINT [' + OBJECT_NAME(c.default_object_id) + ']' + | |
' DEFAULT ' + OBJECT_DEFINITION(c.default_object_id) | |
ELSE '' | |
END + | |
CASE WHEN cc.[object_id] IS NOT NULL | |
THEN ' CONSTRAINT [' + cc.name + '] CHECK ' + cc.[definition] | |
ELSE '' | |
END + | |
CASE WHEN c.is_identity = 1 | |
THEN ' IDENTITY(' + CAST(IDENTITYPROPERTY(c.[object_id], 'SeedValue') AS VARCHAR(5)) + ',' + | |
CAST(IDENTITYPROPERTY(c.[object_id], 'IncrementValue') AS VARCHAR(5)) + ')' | |
ELSE '' | |
END | |
*/ | |
+ ',' + CHAR(13) + CHAR(10) | |
FROM | |
[dbo].[Fields] AS [f] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment