2009-12-11 10 views
18

Domanda: Ho una funzione SQL che restituisce un elenco di fileSQL unione condizionale

ora dovrei aderire un ulteriore elenco a quella lista con un'unione, ma solo se l'utente è admin.

È possibile? Qualcosa di simile:

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 
) 

risposta

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

Stavo per suggerire l'uso di una tabella temporanea, ma questa è la vera risposta. – cjk

+4

@Quassnoi: lascia un po 'per noi, eh? :) –

+4

'@OMG Ponies': hai avuto l'intera settimana! – Quassnoi