我想使用这样的代码:
Create or Replace PACKAGE BODY MyPackage
... declarations, other functions ...
Function Parent(param1, param2)
Return Boolean
As
v_Pcs Number Default 0;
Begin
SubProc(paramA, paramB)
v_MyNestedtable.EXTEND(1);
v_MyNestedtable(v_MyNestedtable.Count).P_A := ParamA;
v_MyNestedtable(v_MyNestedtable.Count).P_B := ParamB;
End SupProcedure;
Select Count(*) Into v_Pcs From myTableA Where...;
If v_Pcs> 0 Then
SubProc(param3, param4);
v_Pcs := 0;
End If;
Select Count(*) Into v_Pcs From myTableB Where...;
If v_Pcs> 0 Then
SubProc(param5, param6);
v_Pcs := 0;
End If;
If v_MyNestedtable.Count > 0 Then
For 1..v_MyNestedtable.Count
Loop
dbms_output.Put_line(v_MyNestedtable(i).P_A||', '||
v_MyNestedtable(i).P_B);
End Loop;
Return False;
Else
Return True;
End Parent;
...
End MyPackage;
当 SupProcedure 位于 Parent 函数之外并且它们位于同一级别时,我的代码运行良好。我只从 Parent 函数调用这个 SupProcedure。所以我想通过声明子/嵌套过程使我的代码看起来更好。我怎样才能做到?
与任何其他语法完全相同
PROCEDURE
。但是,您需要将其放在 DECLARE
部分之前的 BEGIN
:
CREATE OR REPLACE PACKAGE BODY MyPackage IS
TYPE MyRecord IS RECORD( P_A NUMBER, P_B NUMBER );
TYPE MyNestedTable IS TABLE OF MyRecord;
FUNCTION Parent(
param1 IN NUMBER,
param2 IN NUMBER
) RETURN BOOLEAN
AS
found BOOLEAN := FALSE;
v_MyNestedtable MyNestedTable := MyNestedTable();
PROCEDURE SubProc(
paramA IN NUMBER,
paramB IN NUMBER
)
IS
BEGIN
v_MyNestedtable.EXTEND(1);
v_MyNestedtable(v_MyNestedtable.Count).P_A := ParamA;
v_MyNestedtable(v_MyNestedtable.Count).P_B := ParamB;
End SubProc;
BEGIN
-- ... do stuff
SubProc(3, 4);
SubProc(5, 6);
FOR i in 1 .. v_MyNestedTable.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(
v_MyNestedTable(i).P_A||', '||v_MyNestedTable(i).P_B
);
found := TRUE;
END LOOP;
RETURN found;
END Parent;
END MyPackage;
/
但是,您可以使用单个查询和游标循环来编写过程:
CREATE OR REPLACE PACKAGE BODY MyPackage IS
FUNCTION Parent(
param1 IN NUMBER,
param2 IN NUMBER
) RETURN BOOLEAN
AS
found BOOLEAN := FALSE;
BEGIN
FOR rw IN (
SELECT 'param3' AS p_a, 'param4' AS p_b
FROM MytableA
WHERE 1 = 1
HAVING COUNT(*) > 0
UNION ALL
SELECT 'param5', 'param6'
FROM MytableB
WHERE 1 = 1
HAVING COUNT(*) > 0
)
LOOP
DBMS_OUTPUT.PUT_LINE(rw.P_A||', '||rw.P_B);
found := TRUE;
END LOOP;
RETURN found;
END Parent;
End MyPackage;
/
(注意:您没有声明参数变量,所以我用文字替换了它们。)