UDF/case Question

  • After doing nothing but SSIS packages for nearly a year, I must confess that my T-SQL skills have gotten rusty. I'm trying to create a function similar to this:

    CREATE FUNCTION dbo.udfGenericFunction(@Input as varchar(100))

    RETURNS varchar(100)

    AS

    BEGIN

    Declare @Output varchar(100)

    SET @Output = CASE @Input

    WHEN @Input = 'X' THEN SELECT Column1 FROM TABLE_A

    WHEN @Input = 'Y' THEN SELECT Column2 FROM TABLE_A

    ELSE SELECT Column1 + ' ' + Column2 FROM TABLE_A

    END

    RETURN @Output

    END

    Is such a thing possible? If so, what might be the proper syntax. Needless to say, my syntax does not work. Also, we need to stick as close to ANSI-SQL syntax as possible, in case we need to port this over to another db platform. Could someone please help?

  • SELECT @Output =

    CASE

    WHEN @Input = 'X' THEN Column1

    WHEN @Input = 'Y' THEN Column2

    ELSE Column1 + ' ' + Column2

    END

    FROM TABLE_A

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply