MD3.f_GetColumnDataType.sql 768 Bytes
Newer Older
dwentzel's avatar
dwentzel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'MD3.f_GetColumnDataType'))
   DROP FUNCTION MD3.f_GetColumnDataType
GO

CREATE FUNCTION MD3.f_GetColumnDataType (
    @SchemaName SYSNAME
   ,@TableName SYSNAME
   ,@ColumnName SYSNAME
   )
    RETURNS SYSNAME
    WITH RETURNS NULL ON NULL INPUT
AS

BEGIN

    DECLARE @ColumnDataType SYSNAME

    SELECT @ColumnDataType = TYPE_NAME(c.system_type_id)
    FROM sys.objects o
    JOIN sys.columns c 
      ON o.object_id = c.object_id
    JOIN sys.types t 
      ON c.system_type_id = t.system_type_id
    WHERE o.name = @TableName 
      AND c.name = @ColumnName 
      AND o.schema_id = schema_id(@SchemaName)
      AND o.type = 'U'

    RETURN @ColumnDataType

END
GO