包含datalength的扩展TYPE_NAME函数

时间:2012-06-11 09:28:25

标签: sql-server sql-server-2008 tsql datalength

我正在考虑创建格式

的函数
FULL_TYPE_NAME(type_id, max_length)

以字符串格式返回数据类型和长度,例如:

FULL_TYPE_NAME (231,-1) 

将返回:

nvarchar(max)

在我这样做之前,我想检查tsql是否已经有这样的功能(我还没有找到)或者是否有某种灵魂有一个我可以使用的现成品。如果没有,那么我会写一个并在此发布。

提前致谢。

4 个答案:

答案 0 :(得分:2)

粗略的开始将是这样的:

CREATE FUNCTION udf_GetDataTypeAsString
    (
      @user_type_id INT ,
      @Length INT
    )
RETURNS VARCHAR(50)
AS 
    BEGIN
        DECLARE @ReturnStr VARCHAR(50)

        IF @Length = -1 
            SELECT  @ReturnStr = UPPER(name) + '(MAX)'
            FROM    sys.types
            WHERE   user_type_id = @user_type_id
        ELSE 
            SELECT  @ReturnStr = UPPER(name) + '(' + CONVERT(VARCHAR, @Length) + ')'
            FROM    sys.types
            WHERE   user_type_id = @user_type_id

        RETURN @ReturnStr

    END
GO

SELECT dbo.udf_GetDataTypeAsString(167, -1)
--#### Returns VARCHAR(MAX)
SELECT dbo.udf_GetDataTypeAsString(231, 24)
--#### Returns NVARCHAR(24)

请注意,这仅适用于char数据类型&只处理长度,如果你想使用精度(小数等),你需要实现更多的逻辑

此外,您可能希望将验证添加到仅允许某些用户类型的-1长度

(为了好奇 - 你为什么要这样做?)

答案 1 :(得分:1)

这是我的功能。感谢HeavenCore的起点

CREATE FUNCTION dbo.full_type_name (
 @User_Type_Id int,
 @Length int) 
RETURNS varchar (50) 
AS
BEGIN
    DECLARE @Returnstr varchar (50) ;

    --Handle invalid values for @Length
    IF (@Length = 0 OR @Length < -1 OR @Length > 8016 OR @Length IS NULL) 
        BEGIN
            SET @Returnstr = NULL;
        END;
    ELSE
        BEGIN
            SELECT @Returnstr = TYPE_NAME (@User_Type_Id) ;
            --unicode characters occupy two bytes
            IF ((@Returnstr = 'nvarchar' OR @Returnstr = 'nchar') AND @Length > 0) 
                BEGIN
                    SET @Length = (@Length / 2);
                END;

           SELECT @Returnstr = @Returnstr + CASE
           WHEN @Returnstr LIKE '%varchar' AND @Length < 0 
              THEN('(' + 'max' + ')') 
           WHEN @Returnstr LIKE '%char' AND @Length >= 0 
              THEN ('(' + CAST(@Length AS varchar + ')') 
           ELSE ''
         END;
        END;
    RETURN @Returnstr;
END;

我致电

Select name, 
  user_type_id, 
  max_length, 
  dbo.full_type_name(ty.user_type_id, ty.max_length) as [full_type_name] 
from sys.types as ty

测试它(仅视觉检查) 任何改进建议都非常赞赏

答案 2 :(得分:0)

这是我的最终功能。我认为它涵盖了一切,但随意纠正我。我稍后会发布我的测试脚本

CREATE FUNCTION dbo.full_type_name (
 @User_Type_Id int,
 @Length smallint = NULL,
 @Precision tinyint = NULL,
 @Scale tinyint = NULL) 
RETURNS varchar (50) 
AS
BEGIN
  DECLARE
  @Returnstr varchar (50) = NULL,
  @True bit = 1,
  @False bit = 0,
  @Params_Ok bit = 1;

SELECT @Params_Ok = CASE
  --non unicode text
  WHEN ((@User_Type_Id = 165 OR @User_Type_Id = 167 
      OR @User_Type_Id = 173 OR @User_Type_Id = 175) 
     AND ((@Length < -1) OR (@Length = 0) OR (@Length > 8000))) THEN @False
     --unicode text
     WHEN ((@User_Type_Id = 231 OR @User_Type_Id = 239) 
      AND ((@Length < -1) OR (@Length = 0) OR (@Length > 4000))) THEN @False
     --decimal and numeric                          
     WHEN ((@User_Type_Id = 106 OR @User_Type_Id = 108) 
      AND (((@Precision IS NULL) AND (@Scale IS NOT NULL)) 
      OR ((@Precision IS NOT NULL) AND (@Scale IS NULL)) 
      OR (@Precision <=0) OR (@Scale <0) 
      OR (@Precision <= @Scale))) THEN @False
      --float
      WHEN ((@User_Type_Id = 62) AND ((@Precision <= 0) 
        OR (@Precision > 53))) THEN @False
      --time, datetime2 and datetimeoffset
      WHEN ((@User_Type_Id BETWEEN 41 AND 43) 
        AND ((@Precision < 0) OR (@Precision > 7))) 
          THEN @False
      END;
IF(@Params_Ok = @False) 
    BEGIN
        RETURN NULL;
    END;

SELECT @Returnstr = CASE
                      WHEN(@User_Type_Id = 129)THEN 'geometry'
                      WHEN(@User_Type_Id = 130)THEN 'geography'
                      ELSE TYPE_NAME (@User_Type_Id) 
                    END;

--nvarchar and nchar characters occupy two bytes
IF ((@Returnstr = 'nvarchar' OR @Returnstr = 'nchar') AND @Length > 0) 
    BEGIN
        SET @Length = (@Length / 2);
    END;

SELECT @Returnstr = @Returnstr + CASE
  WHEN ((@Returnstr LIKE '%varchar' OR @Returnstr = 'varbinary') 
    AND @Length < 0) 
    THEN('(' + 'max' + ')') 
  WHEN (((@Returnstr LIKE '%char') OR (@Returnstr LIKE '%binary')) 
    AND @Length >= 0) 
    THEN ('(' + CAST(@Length AS varchar) + ')') 
  WHEN ((@Returnstr = 'decimal' OR @Returnstr = 'numeric') 
    AND @Precision IS NOT NULL) 
    THEN ('(' + CAST(@Precision AS varchar) + ',' 
      + CAST(@Scale AS varchar) + ')') 
  WHEN (@Returnstr = 'float' AND @Precision IS NOT NULL) 
    THEN ('(' + CAST(@Precision AS varchar) + ')') 
  --time, datetime2 and datetimeoffset
  WHEN ((@User_Type_Id BETWEEN 41 AND 43) 
    AND (@Precision IS NOT NULL)) 
    THEN ('(' + CAST(@Precision AS varchar) + ')') 
  ELSE ''
 END;
 RETURN @Returnstr;
END;

答案 3 :(得分:0)

这是我的脚本,用于测试我感兴趣的任何人的功能。

--unit test script for dbo.full_type_name
DECLARE
 @Datatypes varchar (max) = '',
 @User_Type_Ids varchar (max) = '',
 @Lengths varchar (max) = '',
 @Precisions varchar (max) = '',
 @Scales varchar (max) = '',
 @Ret_Datatypes varchar (max) = '',
 @Utid_Index int = 0,
 @Length_Index int = 0,
 @Precision_Index int = 0,
 @Scale_Index int = 0,
 @Utid_Substr varchar (40) = '',
 @Length_Substr varchar (40) = '',
 @Precision_Substr varchar (40) = '',
 @Scale_Substr varchar (40) = '',
 @Length smallint = NULL,
 @Precision tinyint = NULL,
 @Scale tinyint = NULL,
 @Count smallint = 0,
 @Ispass bit = 0;

SELECT @Datatypes = COALESCE (@Datatypes + ',', '') + sys.types.name
  FROM sys.types
  WHERE(system_type_id < 242) 
  ORDER BY name;

--remove delimter at start of the string
SELECT @Datatypes = SUBSTRING (@Datatypes, 2, LEN (@Datatypes)) ;

SET @Datatypes = @Datatypes + ',nvarchar(10),nvarchar(max),nchar(20),char(20),
  varchar(max),varchar(10),decimal(10,5),numeric(8,4),float(4),datetime2(2),
  time(4),datetimeoffset(3),varbinary(max)';

SELECT @User_Type_Ids = COALESCE (@User_Type_Ids + ',', '') 
   + CAST(sys.types.user_type_id AS varchar (3)) 
 FROM sys.types
 WHERE(system_type_id < 242) 
 ORDER BY name;
--remove delimter at start of the string
SELECT @User_Type_Ids = SUBSTRING (@User_Type_Ids, 2, LEN (@User_Type_Ids)) ;

SELECT @User_Type_Ids = @User_Type_Ids + ',231,231,239,175,167,167,106,108,62,42,41,43,165';

SELECT @Lengths = '8,null,1,null,3,8,8,10,17,8,null,null,892,16,4,8,null,
  16,17,null,4,4,2,4,8016,256,16,5,8,1,16,null,null,-1';

--now add the remainimg lengths
  SELECT @Lengths = @Lengths + ',20,-1,40,20,-1,10,17,17,8,8,5,10,-1';

WHILE(@Count < 34)  --don't use values from sys.types
  BEGIN
    SELECT @Precisions = @Precisions + ',' + 'null';
    SELECT @Scales = @Scales + ',' + 'null';
    SET @Count = @Count + 1;
  END;

--remove delimter at start of the string
SELECT @Precisions = SUBSTRING (@Precisions, 2, LEN (@Precisions)) ;
--now add the remaining precisions
SELECT @Precisions = @Precisions + ',null,null,null,null,null,null,10,8,4,2,4,3,null';

--remove delimter at start of the string
SELECT @Scales = SUBSTRING (@Scales, 2, LEN (@Scales)) ;
--now add remaining scales
SELECT @Scales = @Scales + ',null,null,null,null,null,null,5,4,null,null,null,null,null';

--Now call our new function for each set of parameters in turn and concatenate the result

--use a while loop to step though comma seperated pseudo arrays
WHILE @User_Type_Ids <> ''
  BEGIN
    --Find index of next commas
    SET @Utid_Index = CHARINDEX (',', @User_Type_Ids) ;
    SET @Length_Index = CHARINDEX (',', @Lengths) ;
    SET @Precision_Index = CHARINDEX (',', @Precisions) ;
    SET @Scale_Index = CHARINDEX (',', @Scales) ;
    IF @Utid_Index > 0
        BEGIN
            -- @utid_substr is substring from start of @CsRoles to character preceding the comma
            SET @Utid_Substr = LEFT (@User_Type_Ids, @Utid_Index - 1) ;
            SET @Length_Substr = LEFT (@Lengths, @Length_Index - 1) ;
            SET @Precision_Substr = LEFT (@Precisions, @Precision_Index - 1) ;
            SET @Scale_Substr = LEFT (@Scales, @Scale_Index - 1) ;
            --now remove this substring and its trailing comma from @CsRoles
            SET @User_Type_Ids = RIGHT (@User_Type_Ids, LEN (@User_Type_Ids) - @Utid_Index) ;
            SET @Lengths = RIGHT (@Lengths, LEN (@Lengths) - @Length_Index) ;
            SET @Precisions = RIGHT (@Precisions, LEN (@Precisions) - @Precision_Index) ;
            SET @Scales = RIGHT (@Scales, LEN (@Scales) - @Scale_Index) ;
        END;
    ELSE
        --last element, has no trailing comma
        BEGIN
            SET @Utid_Substr = @User_Type_Ids;
            SET @Length_Substr = @Lengths;
            SET @Precision_Substr = @Precisions;
            SET @Scale_Substr = @Scales;
            --ensure that we will exit the WHILE loop
            SET @User_Type_Ids = ''; --to ensure break out of while loop
        END;

    IF(@Length_Substr = 'null') 
        BEGIN
            SET @Length = NULL;
        END;
    ELSE
        BEGIN
            SET @Length = CAST(@Length_Substr AS smallint);
        END;
    IF(@Precision_Substr = 'null') 
        BEGIN
            SET @Precision = NULL;
        END;
    ELSE
        BEGIN
            SET @Precision = CAST(@Precision_Substr AS tinyint);
        END;

    IF(@Scale_Substr = 'null') 
        BEGIN
            SET @Scale = NULL;
        END;
    ELSE
        BEGIN
            SET @Scale = CAST(@Scale_Substr AS tinyint);
        END;

    --now call our function
    SET @Ret_Datatypes = @Ret_Datatypes + ',' 
       + ISNULL (jc_utils.jpwfn_0001_full_type_name (CAST(@Utid_Substr AS int), 
       @Length, @Precision, @Scale) , 'NULL') ;

END; --End of WHILE loop

--Now compare the concatenated return values with the expected vlaues
--remove delimter at start of the string
SELECT @Ret_Datatypes = SUBSTRING (@Ret_Datatypes, 2, LEN (@Ret_Datatypes)) ;

IF(@Ret_Datatypes = @Datatypes) 
  BEGIN
    SET @Ispass = 1;
  END;

--Now test for known fail conditions
--all these should return null
--invalid lengths 
IF(@Ispass = 1) 
  BEGIN
    SET @User_Type_Ids = '165,165,165,167,167,167,173,173,173,
      175,175,175,231,231,231,239,239,239,106,106,106,106,
      108,108,108,108,62,62,41,42,43';
    SET @Lengths = '0,8001,-2,0,8001,-2,0,8001,-2,0,8001,-2,0,
      4001,-2,0,4001,-2,-2,80,null,0,-2,80,null,0,0,null,0,0,0';
    SET @Precisions = 'null,null,null,null,null,null,null,null,
      null,null,null,null,null,null,null,null,
      null,null,null,2,0,1,null,2,0,1,0,54,8,8,8';
    SET @Scales = 'null,null,null,null,null,null,null,null,
      null,null,null,null,null,null,null,null,
      null,null,1,null,2,2,1,null,2,2,null,null,0,0,0';

    WHILE @User_Type_Ids <> ''
        BEGIN
            --Find index of next commas
            SET @Utid_Index = CHARINDEX (',', @User_Type_Ids) ;
            SET @Length_Index = CHARINDEX (',', @Lengths) ;
            SET @Precision_Index = CHARINDEX (',', @Precisions) ;
            SET @Scale_Index = CHARINDEX (',', @Scales) ;
            IF @Utid_Index > 0
                BEGIN
                    SET @Utid_Substr = LEFT (@User_Type_Ids, @Utid_Index - 1) ;
                    SET @Length_Substr = LEFT (@Lengths, @Length_Index - 1) ;
                    SET @Precision_Substr = LEFT (@Precisions, @Precision_Index - 1) ;
                    SET @Scale_Substr = LEFT (@Scales, @Scale_Index - 1) ;
                    --now remove this substring and its trailing comma 
                    SET @User_Type_Ids = RIGHT (@User_Type_Ids, LEN (@User_Type_Ids) 
                        - @Utid_Index) ;
                    SET @Lengths = RIGHT (@Lengths, LEN (@Lengths) - @Length_Index) ;
                    SET @Precisions = RIGHT (@Precisions, 
                       LEN (@Precisions) - @Precision_Index) ;
                    SET @Scales = RIGHT (@Scales, LEN (@Scales) - @Scale_Index) ;
                END;
            ELSE
                --last element, has no trailing comma
                BEGIN
                    SET @Utid_Substr = @User_Type_Ids;
                    SET @Length_Substr = @Lengths;
                    SET @Precision_Substr = @Precisions;
                    SET @Scale_Substr = @Scales;
                    --ensure that we will exit the WHILE loop
                    SET @User_Type_Ids = ''; --to ensure break out of while loop
                END;

            IF(@Length_Substr = 'null') 
                BEGIN
                    SET @Length = NULL;
                END;
            ELSE
                BEGIN
                    SET @Length = CAST(@Length_Substr AS smallint);
                END;
            IF(@Precision_Substr = 'null') 
                BEGIN
                    SET @Precision = NULL;
                END;
            ELSE
                BEGIN
                    SET @Precision = CAST(@Precision_Substr AS tinyint);
                END;

            IF(@Scale_Substr = 'null') 
                BEGIN
                    SET @Scale = NULL;
                END;
            ELSE
                BEGIN
                    SET @Scale = CAST(@Scale_Substr AS tinyint);
                END;

            --now call our function
            SET @Ret_Datatypes = dbo.full_type_name (CAST(@Utid_Substr AS int), 
               @Length, @Precision, @Scale) ;
            IF(@Ret_Datatypes IS NOT NULL) 
                BEGIN
                    SET @Ispass = 0;
                    BREAK;
                END;
            ELSE
                BEGIN
                    SET @Ispass = 1;
                END;

        END; --End of WHILE loop
  END;
IF(@Ispass = 1) 
  BEGIN
    PRINT 'pass';
  END;
ELSE
  BEGIN
    PRINT 'fail';
    PRINT @Datatypes;
    PRINT @Ret_Datatypes;
  END;