我正在尝试在DB2存储过程中找到条件逻辑的示例.... 字面上是一个语法示例。
具体来说 - 我需要根据另一个查询的结果做两件事之一。 所以这个程序在逻辑上就是这样的
SELECT x FROM y
IF x = 'a'
UPDATE table1
else
UPDATE table2
任何人都可以提供此示例吗? THX
答案 0 :(得分:1)
这不是太复杂:
CREATE PROCEDURE p1
LANGUAGE SQL
BEGIN
-- Set this to the appropriate data type.
DECLARE varX CHAR(1);
-- Get the value of column 'X' and store it in variable 'varX'.
-- Make sure this query returns only a single row. If you need
-- to iterate over multiple values from the table 'Y' then you
-- will need to use a cursor and use the FETCH ... INTO statement
-- to get each row's value into 'varX'
SELECT x INTO varX FROM y;
-- Do your update as necessary...
IF varX = 'a' THEN
update table1 ... ;
ELSE
update table2 ... ;
END IF;
END
@