我想知道是否可以在sql代码的CASE语句中使用INSERT INTO语句.
这是我正在尝试做的粗略伪代码:
SELECT (CASE (SELECT SomeValue FROM SoMetable) WHEN NULL THEN INSERT INTO OtherTable VALUES (1,2,3) (SELECT NewlyInsertedValue FROM OtherTable) ELSE (SELECT SomeOtherValue FROM WeirdTable) END),Column1,Column2 FROM BigTable
解决方法
您需要完成IF … THEN语句.大致相似的东西(不确定db2的语法):
SELECT @SomeValue = SomeValue FROM SoMetable IF @SomeValue IS NULL INSERT INTO OtherTable VALUES (1,3) SELECT NewlyInsertedValue FROM OtherTable; ELSE INSERT INTO OtherTable VALUES (1,3) SELECT SomeOtherValue FROM WeirdTable; END IF;