0

我正在尝试在 DB2 存储过程中找到条件逻辑的示例......实际上是一个语法示例。

具体来说 - 我需要根据另一个查询的结果做两件事之一。所以程序在逻辑上是这样的

SELECT x FROM y
IF x = 'a'
    UPDATE table1
else
    UPDATE table2

谁能提供一个例子?谢谢

4

1 回答 1

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
@
于 2013-09-17T00:28:42.187 回答