jeudi 12 juillet 2018

sql if statement in function

i have a fuction in visual basic and

i'm sending a and b variables into fuction. It does , a lot of select statements as seen below

SQL CODE

Select xxx where a=@a

union

Select yyy where u=@a

i want to add one more union with if statement. i want to run one part if @b=1 and other part if @b=2

union

if @b=1

select zzz

if @b=2

select ttt

i want to learn, how to use union with if statement. and correct syntax of it.

union

    if @b=1
begin

    select zzz
end

    else if( @b=2)

    select ttt
end

is it correct?

Thanks.

Aucun commentaire:

Enregistrer un commentaire