Stefan Steiger
Stefan Steiger

Reputation: 82186

SQL conditional union

Question: I have an SQL function which returns a list of files

now I should join an additional list to that list with an union, but only if the user is admin.

Is that possible? Something like:

CREATE FUNCTION tfu_CMS_Process(@bIsAdmin bit  )
-- Add the parameters for the function here
RETURNS TABLE
AS
RETURN
 (
 SELECT * FROM TABLE1

 if bIsAdmin

 UNION ALL 

 SELECT * FROM TABLE2

 end if
 )

Upvotes: 17

Views: 9715

Answers (1)

Quassnoi
Quassnoi

Reputation: 425371

SELECT  *
FROM    table1
UNION ALL
SELECT  *
FROM    table2
WHERE   @isAdmin = 1

Upvotes: 35

Related Questions