我想知道是否有可能在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的语法):
IF...THEN
SELECT @SomeValue = SomeValue FROM SomeTable IF @SomeValue IS NULL INSERT INTO OtherTable VALUES (1, 2, 3) SELECT NewlyInsertedValue FROM OtherTable; ELSE INSERT INTO OtherTable VALUES (1, 2, 3) SELECT SomeOtherValue FROM WeirdTable; END IF;