CAS子句在FROM子句中

时间:2012-05-31 20:38:10

标签: sql-server tsql case

是否可以在t-sql查询中的FROM子句中使用CASE? 例如:

SELECT *
FROM CASE
    WHEN column_ = 'firstColumn'
        THEN table_ t, firstTable fT
    ELSE table_ t, secondTable sT

2 个答案:

答案 0 :(得分:3)

SELECT  *
FROM    mytable t
CROSS APPLY
        (
        SELECT  *
        FROM    firstTable ft
        WHERE   t.column = 'firstColumn'
                AND ft.id = t.joincolumn
        UNION ALL
        SELECT  *
        FROM    secondTable st
        WHERE   (t.column <> 'firstColumn' OR t.column IS NULL)
                AND st.id = t.joincolumn
        ) q

答案 1 :(得分:1)

感谢大家的意见。我实际上结束了类似于Quassnoi建议的事情:

SELECT *
FROM table_ t, firstTable fT

UNION

SELECT *
FROM table_ t, secondTable sT

我不知道我在想什么。我应该早点想出来。