0

I would like to implement a solution where the Table-Valued-Function returns different set of data based on the value of the parameter passed to it.

For Example: something like this:

CREATE FUNCTION test(@param char(6))
RETURNS TABLE
AS
RETURN
(
IF @param IS NULL
    select * from this
ELSE
    select * from that
END
)
Ocelot
  • 1,733
  • 4
  • 29
  • 53

1 Answers1

0

You can re-write the function as:

CREATE FUNCTION test(@param char(6))
RETURNS @retInformation TABLE
(
    -- Columns returned by the function
    ID int PRIMARY KEY NOT NULL  
)
AS
-- Returns the required Id if input parameter is null.
BEGIN    
    IF @param IS NULL
    BEGIN
        INSERT @retInformation 
        select ID from this
    END
    ELSE
    BEGIN
        INSERT @retInformation
        select ID from that
    END;
    RETURN;
END;
Deepshikha
  • 9,896
  • 2
  • 21
  • 21