如何使用IF和ELSE語句來定義要根據「深度INTEGER」運行多少存儲過程? (例如,如果用戶使用1所述的深度周長,僅第一部分將被加工的)在DB2 Store過程中使用IF語句
--#SET TERMINATOR/
CREATE PROCEDURE STOREP2(IN str_in VARCHAR(1), IN depth INTEGER)
LANGUAGE SQL MODIFIES SQL DATA
DYNAMIC RESULT SETS 1
CRSUBJ: BEGIN
DECLARE C1 CURSOR WITH RETURN FOR
SELECT T1.COLUMN1, T1.COLUMN2 FROM TABLE1 AS T1 WHERE T1.COLUMN1=str_in
UNION
SELECT T2.COLUMN1, T2.COLUMN2 FROM TABLE1 AS T1, TABLE1 AS T2 WHERE T1.COLUMN1=str_in AND T2.COLUMN1=T1.COLUMN2
UNION
SELECT T3.COLUMN1, T3.COLUMN2 FROM TABLE1 AS T1, TABLE1 AS T2, TABLE1 AS T3 WHERE T1.COLUMN1=str_in AND T2.COLUMN1=T1.COLUMN2 AND T3.COLUMN1=T2.COLUMN2
UNION
SELECT T4.COLUMN1, T4.COLUMN2 FROM TABLE1 AS T1, TABLE1 AS T2, TABLE1 AS T3, TABLE1 AS T4 WHERE T1.COLUMN1=str_in AND T2.COLUMN1=T1.COLUMN2 AND T3.COLUMN1=T2.COLUMN2 AND T4.COLUMN1=T3.COLUMN2;
OPEN C1;
RETURN;
END CRSUBJ/
謝謝:)
這看起來像(可能)用於遞歸設置。你在瀏覽樹狀結構嗎?您使用的是哪種版本的DB2?最近的大多數版本都支持遞歸CTE,這將爲您提供多種方法來控制深度。 – 2012-04-20 15:51:33