插入内的条件

时间:2012-08-20 10:23:25

标签: sql

是否可以像IF中那样在INSERT内执行此类操作;

INSERT INTO @TABLE (ID, NAME, ADDRESS)
VALUES(
    IF CONDITION (SELECT DATA)
    ELSE (SELECT DATA)
)

1 个答案:

答案 0 :(得分:4)

排序......

INSERT INTO @TABLE (ID, NAME, ADDRESS)
SELECT 
CASE condition WHEN result then id else id2 end,
CASE condition WHEN result then name else name2 end,
...

UNION

INSERT INTO @TABLE (ID, NAME, ADDRESS)
SELECT data FROM source WHERE condition
UNION
SELECT data FROM othersource WHERE NOT(condition)