SQL if select statement returns no rows then perform alternative select statement

前端 未结 3 555
忘掉有多难
忘掉有多难 2021-02-07 07:27

Basically, what syntex would allow me to achieve the title statement?

If (select statement 1) returns 0 rows THEN (select statement 2) else (select statement 3)
         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 08:11

    Here you go...

    IF ((select count(*) from table1)= 0)
    BEGIN
    Select * from table2
    END
    ELSE
    BEGIN
    SELECT * from table3
    END
    

提交回复
热议问题