PL / SQL在存储过程中包装分层查询

时间:2017-06-16 04:05:48

标签: sql oracle stored-procedures plsql syntax

感谢大家的帮助,我对PL/SQL很新,并且发现语法比T-SQL更具挑战性。我有一个功能PL/SQL cursor,可以将我想要的内容插入到表格中。下一步,我想将它包装在一个存储过程中,这样我就可以传入一个参数值,该值将取代脚本中的'MD01',用户输入任意4个字符串。

问题在于我这样做(简单CREATE OR REPLACE PROCEDURE test AS)即使代码在两秒前工作,我也会遇到一堆错误。我究竟做错了什么?下面发布的代码部分功能完美,但我不知道如何将它正确地包装到PL / SQL中的存储过程中。

  

ORA-00942:表或视图不存在PLS-00364:循环索引变量   'EACH_REC'使用无效ORA-00984:此处不允许列

CREATE OR REPLACE PROCEDURE test IS
DECLARE
   CURSOR c1 IS SELECT * FROM
(
SELECT
    C.FEE_SCHEDULE
,   C.PROC
,   C.MODIFIER
,   C.MODIFIER2
,   C.PROVIDER
,   C.YMDEFF
,   C.YMDEND
,   C.NEXT_SPAN_DATE
,   C.SPAN
,   C.SPAN_FLAG
,   C.RATE
,   TO_DATE(D.YMDTRANS,'YYYYMMDD') AS YMDTRANS
FROM
(
SELECT
    B.FEE_SCHEDULE
,   B.PROC
,   B.MODIFIER
,   B.MODIFIER2
,   PROVIDER
,   TO_DATE(B.YMDEFF,'YYYYMMDD')    AS YMDEFF
,   TO_DATE(B.YMDEND,'YYYYMMDD')    AS YMDEND
,   CASE WHEN RECURSION_LEVEL = 1 THEN NULL ELSE TO_DATE(B.T3,'YYYYMMDD')END        AS NEXT_SPAN_DATE

,   CASE    WHEN    B.YMDEND = '99991231' THEN NULL
            WHEN    B.RANK2 = '1' THEN NULL
            ELSE    TO_DATE(B.T3,'YYYYMMDD') - TO_DATE(B.YMDEND,'YYYYMMDD') END AS SPAN

,   CASE    WHEN    TO_DATE(B.T3,'YYYYMMDD') - TO_DATE(B.YMDEND,'YYYYMMDD') = '1'   THEN 'CORRECT_SPAN'
            WHEN    B.YMDEND = '99991231'                                           THEN 'CORRECT_SPAN'
            WHEN    B.RANK2 = '1'                                                   THEN 'CORRECT_SPAN'
            ELSE 'GAPPED_SPAN' END AS SPAN_FLAG
--, RANK1
--, RECURSION_LEVEL
,   RATE
,   YMDTRANS
FROM
(
SELECT
    A.*
,   CONNECT_BY_ISCYCLE AS T1
,   sys_connect_by_path(YMDEFF,' ') AS T2
,   SUBSTR(sys_connect_by_path(YMDEFF,' '),1,9) AS T3
,   LEVEL AS RECURSION_LEVEL
FROM
(
SELECT
    SUBSTR(FEE_KEY,3,4)     AS FEE_SCHEDULE
,   SUBSTR(FEE_KEY,7,5)     AS PROC
,   SUBSTR(FEE_KEY,19,2)    AS MODIFIER
,   SUBSTR(FEE_KEY,23,2)    AS MODIFIER2
,   SUBSTR(FEE_KEY,29,12)   AS PROVIDER
,   FEE_KEY
,   YMDEFF
,   YMDEND
,   YMDTRANS
,   DENSE_RANK () OVER (PARTITION BY        SUBSTR(FEE_KEY,3,4)
,   SUBSTR(FEE_KEY,7,5)
,   SUBSTR(FEE_KEY,19,2)
,   SUBSTR(FEE_KEY,23,2)
,   SUBSTR(FEE_KEY,29,12)
ORDER BY YMDEFF) AS RANK1

,   DENSE_RANK () OVER (PARTITION BY        SUBSTR(FEE_KEY,3,4)
,   SUBSTR(FEE_KEY,7,5)
,   SUBSTR(FEE_KEY,19,2)
,   SUBSTR(FEE_KEY,23,2)
,   SUBSTR(FEE_KEY,29,12)
ORDER BY YMDEND DESC) AS RANK2
,   RATE/100 AS RATE

FROM AMIOWN.FEE_SCHEDULE
WHERE 1 = 1
AND SUBSTR(FEE_KEY,3,4) = 'MD01'
) A
START WITH  FEE_SCHEDULE IN('MD01')
CONNECT BY  NOCYCLE
    PRIOR   RANK1           =   RANK1 + 1
AND PRIOR   FEE_SCHEDULE    =   SUBSTR(FEE_KEY,3,4)
AND PRIOR   PROC            =   SUBSTR(FEE_KEY,7,5)
AND PRIOR   MODIFIER        =   SUBSTR(FEE_KEY,19,2)
AND PRIOR   MODIFIER2       =   SUBSTR(FEE_KEY,23,2)
AND PRIOR   PROVIDER        =   SUBSTR(FEE_KEY,29,12)
AND LEVEL                   =   2
ORDER BY PROC, YMDEFF, LEVEL
) B
WHERE 1 = 1

AND RECURSION_LEVEL = 2
OR (RECURSION_LEVEL = 1 AND CASE WHEN RECURSION_LEVEL = 1 THEN NULL ELSE TO_DATE(B.T3,'YYYYMMDD')END IS NOT NULL  )
OR  B.YMDEND = '99991231'
OR  B.RANK2 = 1
) C

INNER JOIN
(
SELECT
    SUBSTR(FEE_KEY,3,4) AS FEE_SCHEDULE
,   MAX(YMDTRANS) AS YMDTRANS
FROM AMIOWN.FEE_SCHEDULE
WHERE SUBSTR(FEE_KEY,3,4) = 'MD01'
GROUP BY SUBSTR(FEE_KEY,3,4)
) D ON C.FEE_SCHEDULE = D.FEE_SCHEDULE

WHERE    1 = 1
);

i NUMBER:= 0;
BEGIN
  FOR each_rec IN c1 LOOP
INSERT INTO SCHEMA.FEE_SCHEDULE_GAPS_DETAIL
(
    FEE_SCHEDULE
,   PROC
,   MODIFIER
,   MODIFIER2
,   PROVIDER
,   YMDEFF
,   YMDEND
,   NEXT_SPAN_DATE
,   SPAN
,   SPAN_FLAG
,   RATE
,   YMDTRANS
)
VALUES
(   each_rec.FEE_SCHEDULE
,   each_rec.PROC
,   each_rec.MODIFIER
,   each_rec.MODIFIER2
,   each_rec.PROVIDER
,   each_rec.YMDEFF
,   each_rec.YMDEND
,   each_rec.NEXT_SPAN_DATE
,   each_rec.SPAN
,   each_rec.SPAN_FLAG
,   each_rec.RATE
,   each_rec.YMDTRANS
);
i:= i+1;

  END LOOP;
END;
/

2 个答案:

答案 0 :(得分:1)

Boneist是正确的: “作为authid definer(默认)的过程不会查看角色来确定是否有必要的权限 - 相反,授权需要是直接的 - 即。而不是在schema1.table1上的grant select到some_role;授予some_role对于schema2,它必须是schema1.table1上的grant select to schema2“

我联系了我们的Oracle DBA,他们很快就授予我直接访问权限和完全正确编译的存储过程。非常感谢,很高兴我并不疯狂。

答案 1 :(得分:0)

匿名阻止:

DECLARE
  <declarations>
BEGIN
  <actions>
END;

可以这种方式转换为存储过程:

CREATE OR REPLACE PROCEDURE test IS
  <declarations>
BEGIN
  <actions>
END;