标签:
IF OBJECT_ID(N‘[dbo].[ufn_GetDisplayDataTypeName]‘, ‘FN‘) IS NOT NULL BEGIN DROP FUNCTION [dbo].[ufn_GetDisplayDataTypeName]; END GO --================================== -- 功能: 获取字段列显示的数据类型名称 -- 说明: 支持用户定义类型,可以运行于SQL Server 2005+ -- 创建: yyyy-MM-dd hh:mm-hh:mm XXX 创建内容描述 -- 修改: yyyy-MM-dd hh:mm-hh:mm XXX 修改内容描述 --================================== CREATE FUNCTION [dbo].[ufn_GetDisplayDataTypeName] ( @intUserTypeID AS INT -- 用户类型ID ,@sntMaxLength AS SMALLINT -- 最大字节长度 ,@tntPrecision AS TINYINT -- 精度 ,@tntScale AS TINYINT -- 小数位 ,@bitIsUpperDisplay AS BIT = 0 -- 是否大写显示(默认小写显示) ) RETURNS NVARCHAR(270) AS BEGIN SET @bitIsUpperDisplay = ISNULL(@bitIsUpperDisplay, 0); DECLARE @nvcDataTypeName AS NVARCHAR(128) ,@nvcBaseDataTypeName AS NVARCHAR(128) ,@nvcSuffixDisplayName AS NVARCHAR(14) SELECT @nvcDataTypeName = N‘‘ ,@nvcBaseDataTypeName = N‘‘ ,@nvcSuffixDisplayName = N‘‘ SELECT @nvcDataTypeName = [name] ,@nvcBaseDataTypeName = (CASE WHEN (@nvcDataTypeName = N‘sysname‘ OR [is_user_defined] = 1) THEN TYPE_NAME([system_type_id]) ELSE @nvcDataTypeName END) FROM [sys].[types] WHERE [user_type_id] = @intUserTypeID; SET @nvcDataTypeName = (CASE WHEN @nvcDataTypeName <> @nvcBaseDataTypeName THEN @nvcDataTypeName + N‘:‘ ELSE N‘‘ END); SET @nvcBaseDataTypeName = CASE @bitIsUpperDisplay WHEN 1 THEN UPPER(@nvcBaseDataTypeName) ELSE @nvcBaseDataTypeName END; SET @nvcSuffixDisplayName = (CASE WHEN @nvcBaseDataTypeName = N‘char‘ THEN QUOTENAME(RTRIM(CAST(@sntMaxLength AS VARCHAR(4))), N‘)‘) WHEN @nvcBaseDataTypeName = N‘varchar‘ THEN (CASE WHEN @sntMaxLength = -1 THEN N‘(MAX)‘ ELSE QUOTENAME(RTRIM(CAST(@sntMaxLength AS VARCHAR(4))), N‘)‘) END) WHEN @nvcBaseDataTypeName = N‘nchar‘ THEN QUOTENAME(RTRIM(CAST(@sntMaxLength / 2 AS VARCHAR(4))), N‘)‘) WHEN @nvcBaseDataTypeName = N‘nvarchar‘ THEN (CASE WHEN @sntMaxLength = -1 THEN N‘(MAX)‘ ELSE QUOTENAME(RTRIM(CAST(@sntMaxLength / 2 AS VARCHAR(4))), N‘)‘) END) WHEN @nvcBaseDataTypeName = N‘binary‘ THEN QUOTENAME(RTRIM(CAST(@sntMaxLength AS VARCHAR(4))), N‘)‘) WHEN @nvcBaseDataTypeName = N‘varbinary‘ THEN (CASE WHEN @sntMaxLength = -1 THEN N‘(MAX)‘ ELSE QUOTENAME(RTRIM(CAST(@sntMaxLength / 2 AS VARCHAR(4))), N‘)‘) END) WHEN @nvcBaseDataTypeName IN (N‘numeric‘, N‘decimal‘) THEN QUOTENAME(RTRIM(CAST(@tntPrecision AS VARCHAR(4))) + N‘,‘ + RTRIM(CAST(@tntScale AS VARCHAR(4))), N‘)‘) WHEN @nvcBaseDataTypeName IN (N‘time‘, N‘datetime2‘, N‘datetimeoffset‘) THEN QUOTENAME(RTRIM(CAST(@tntScale AS VARCHAR(4))), N‘)‘) ELSE N‘‘ END); RETURN @nvcDataTypeName + @nvcBaseDataTypeName + @nvcSuffixDisplayName; END GO
CREATE TYPE [dbo].[fd_udt_digest] FROM [NVARCHAR](255) NULL GO CREATE TYPE [dbo].[fd_udt_id] FROM [CHAR](15) NOT NULL GO CREATE TYPE [dbo].[fd_udt_username] FROM [NVARCHAR](20) NULL GO CREATE TYPE [dbo].[UFemail] FROM [VARCHAR](100) NULL GO CREATE TYPE [dbo].[UFFlag] FROM [CHAR](1) NULL GO CREATE TYPE [dbo].[UFhyperlink] FROM [VARCHAR](100) NULL GO CREATE TYPE [dbo].[UFMedia] FROM [IMAGE] NULL GO CREATE TYPE [dbo].[UFreference] FROM [VARCHAR](30) NULL GO CREATE TYPE [dbo].[UFtext] FROM [NTEXT] NULL GO CREATE TYPE [dbo].[UFUID] FROM [UNIQUEIDENTIFIER] NULL GO CREATE TYPE [dbo].[userdecimal] FROM [DECIMAL](28, 6) NOT NULL GO CREATE TYPE [dbo].[udtProduct] AS TABLE ( ProductID INT NOT NULL ,UnitPrice DECIMAL(9, 2) NOT NULL ,Quantity INT NOT NULL PRIMARY KEY ([ProductID] ASC) ) GO CREATE TABLE [dbo].[ScaleDataTypeTable]( [ColDecimal] [NUMERIC](18, 2) NULL, [ColNumeric] [NUMERIC](9, 2) NULL, [ColFloat] [FLOAT] NULL, [ColReal] [REAL] NULL, [ColTime] [TIME](5) NOT NULL, [ColDateTime2] [DATETIME2](3) NULL, [ColDateTimeOffset] [DATETIMEOFFSET](5) NULL, [ColSqlVariant] [SQL_VARIANT] NULL, [ColMoney] [MONEY] NULL, [ColSmallMoney] [SMALLMONEY] NULL, [Col1] [dbo].[fd_udt_digest] NULL, [Col2] [dbo].[fd_udt_id] NULL, [Col3] [dbo].[fd_udt_username] NULL, [Col4] [dbo].[UFemail] NULL, [Col5] [dbo].[UFFlag] NULL, [Col6] [dbo].[UFhyperlink] NULL, [Col7] [dbo].[UFMedia] NULL, [Col8] [dbo].[UFreference] NULL, [Col9] [dbo].[UFtext] NULL, [Col10] [dbo].[UFUID] NULL, [Col11] [dbo].[userdecimal] NULL, [Col12] HIERARCHYID NULL, [Col13] GEOMETRY NULL, [Col14] GEOGRAPHY NOT NULL, [Col15] CHAR(10) NOT NULL, [Col16] VARCHAR(25) NOT NULL, [Col16Max] VARCHAR(MAX) NOT NULL, [Col17] NCHAR(16) NOT NULL, [Col18] NVARCHAR(32) NOT NULL, [Col18Max] NVARCHAR(MAX) NOT NULL, [ColBigint] BIGINT NOT NULL, [ColInt] INT NOT NULL, [ColSmallint] INT NOT NULL, [ColTinyint] TINYINT NOT NULL, [ColBit] BIT NOT NULL, [Col19] DECIMAL(9,7) NOT NULL, [Col20] MONEY NOT NULL, [Col21] SMALLMONEY NOT NULL, [Col22] TIMESTAMP NOT NULL, [Col24] UNIQUEIDENTIFIER NOT NULL, [Col25] IMAGE NOT NULL, [Col26] TEXT NOT NULL, [Col27] NTEXT NOT NULL, [Col28] BINARY(8) NOT NULL, [Col29] VARBINARY(8) NOT NULL, [Col29Max] VARBINARY(MAX) NOT NULL, [ColMxml] XML NULL, [Col30] DATE NOT NULL, [Col31] DATETIME NOT NULL, [Col32] SMALLDATETIME NOT NULL ) ON [PRIMARY]; GO CREATE TABLE [dbo].[UpdateDataTable] ( UpdateDataTableId INT NOT NULL, [Col23] ROWVERSION NOT NULL, [Colname] sysname NOT NULL ) ON [PRIMARY]; GO
基本数据类型小写展示的效果的T-SQL代码和效果截图如下:
SELECT T2.[name] AS [table_name] ,[T1].[name] AS [column_name] ,TYPE_NAME([T1].[user_type_id]) AS [column_datatype] ,[dbo].[ufn_GetDisplayDataTypeName]([T1].[user_type_id], [T1].[max_length], [T1].[precision], [T1].[scale], 0) AS [column_display_datatype] ,[T1].[max_length] ,[T1].[precision] ,[T1].[scale] FROM [sys].[all_columns] AS T1 INNER JOIN [sys].[all_objects] AS T2 ON [T1].[object_id] = [T2].[object_id] WHERE [T2].[name] IN (N‘ScaleDataTypeTable‘, N‘UpdateDataTable‘) --AND EXISTS (SELECT 1 FROM [sys].[types] WHERE ([name] = N‘sysname‘ OR [is_user_defined] = 1) AND [user_type_id] = [T1].[user_type_id]); GO
SELECT T2.[name] AS [table_name] ,[T1].[name] AS [column_name] ,TYPE_NAME([T1].[user_type_id]) AS [column_datatype] ,[dbo].[ufn_GetDisplayDataTypeName]([T1].[user_type_id], [T1].[max_length], [T1].[precision], [T1].[scale], 1) AS [column_display_datatype] ,[T1].[max_length] ,[T1].[precision] ,[T1].[scale] FROM [sys].[all_columns] AS T1 INNER JOIN [sys].[all_objects] AS T2 ON [T1].[object_id] = [T2].[object_id] WHERE [T2].[name] IN (N‘ScaleDataTypeTable‘, N‘UpdateDataTable‘) --AND EXISTS (SELECT 1 FROM [sys].[types] WHERE ([name] = N‘sysname‘ OR [is_user_defined] = 1) AND [user_type_id] = [T1].[user_type_id]); GO
标签:
原文地址:http://www.cnblogs.com/dzy863/p/5891548.html