sql [CreateTableFromFieldListIncludingTypes]从字段列表创建表动态地包括类型,比例和精度#table #create #dyna

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql [CreateTableFromFieldListIncludingTypes]从字段列表创建表动态地包括类型,比例和精度#table #create #dyna相关的知识,希望对你有一定的参考价值。

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]

以上是关于sql [CreateTableFromFieldListIncludingTypes]从字段列表创建表动态地包括类型,比例和精度#table #create #dyna的主要内容,如果未能解决你的问题,请参考以下文章

sql [sql技巧]一些sql技巧#sql

sql 2008 r2 在sql 2008上兼容么

SQL基础

有大神知道,sql server 中如何批量执行sql语句吗?

pl sql developer怎么执行sql

pl sql developer怎么执行sql