This is my script for testing my function for anyone who would be interested.
--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;