oracle 游标,与循环运行图连接

发布于 2024-11-09 16:26:29 字数 1359 浏览 0 评论 0原文

我想使用游标通过 Oracle 的 Connect By Prior 循环浏览管理报告链。然后顺序颠倒过来,因此 VP 位于顶部,而我开始的记录位于底部 (A.Corpid)。

顶部有一个游标 C1,它按照特定条件检索 corpid

从该表返回的每个规范名称(假设它有 6 行)我想存储在定义的 TIER1、TIER2、TIER3 变量中。

不知怎的,我不知道如何将单词“TIER”与行号结合起来,

DECLARE 
cursor c1 is
select distinct corpid, cn from Mytable where Country ='ITA';
master varchar2(50);
Tier1 varchar2(50);
Tier2 varchar2(50);
Tier3 varchar2(50);
Tier4 varchar2(50);
Tier5 varchar2(50);
Tier6 varchar2(50);
Tier7 varchar2(50);
Tier8 varchar2(50);
Begin
for rec in c1
loop
dbms_output.put_line(rec.cn);

   DECLARE 
   Cursor C2 is

    SELECT CN  FROM Mytable A CONNECT BY PRIOR A.reportsto=A.corpid 
    START WITH A.corpid=rec.corpid
    order by rownum desc;

        Begin
        open C2;
        for i in 1..8 loop
            fetch C2 into master;
            dbms_output.put_line(master);
              'Tier'||to_char(i)) :=master ;  
            end loop;
            end;

     if TIER1 is null then
    TIER1:='';
    end if;
*/ remmed out until the variable assignments work */
-- update mytable set VP_TIER1=TIER1 where corpid=rec.corpid; 
end loop;
end;

Oracle 抱怨“||”

(ORA-06550:第 33 行,第 31 列: PLS-00103:遇到符号“|”当预期出现以下情况之一时: := 。 (@%; )


我也尝试过连接,但这也不起作用

ORA-06550:第 33 行,第 26 列: PLS-00306:调用“CONCAT”时参数数量或类型错误

I want to use a cursor to loop through a management reporting chain using the Connect By Prior from Oracle. The order is then reversed so the VP is at the top and the record i started with is at the bottom (A.Corpid).

there's a cursor C1 on top that retrieves corpid following certain criteria

Each Canonical Name returned from that table (say f.i it has 6 rows) i want to store in defined TIER1,TIER2, TIER3 variables.

Somehow i can't figure out how to combine the word 'TIER' with the row number i

DECLARE 
cursor c1 is
select distinct corpid, cn from Mytable where Country ='ITA';
master varchar2(50);
Tier1 varchar2(50);
Tier2 varchar2(50);
Tier3 varchar2(50);
Tier4 varchar2(50);
Tier5 varchar2(50);
Tier6 varchar2(50);
Tier7 varchar2(50);
Tier8 varchar2(50);
Begin
for rec in c1
loop
dbms_output.put_line(rec.cn);

   DECLARE 
   Cursor C2 is

    SELECT CN  FROM Mytable A CONNECT BY PRIOR A.reportsto=A.corpid 
    START WITH A.corpid=rec.corpid
    order by rownum desc;

        Begin
        open C2;
        for i in 1..8 loop
            fetch C2 into master;
            dbms_output.put_line(master);
              'Tier'||to_char(i)) :=master ;  
            end loop;
            end;

     if TIER1 is null then
    TIER1:='';
    end if;
*/ remmed out until the variable assignments work */
-- update mytable set VP_TIER1=TIER1 where corpid=rec.corpid; 
end loop;
end;

Oracle complains about the '||'

(ORA-06550: line 33, column 31:
PLS-00103: Encountered the symbol "|" when expecting one of the following:
:= . ( @ % ;
)

I've tried as well to concat but that didn't work either

ORA-06550: line 33, column 26:
PLS-00306: wrong number or types of arguments in call to 'CONCAT'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2024-11-16 16:26:29

您的文字和变量的串联不会像您希望的那样计算为变量 TIER1 。尝试使用数组代替:

DECLARE  
    cursor c1
    is 
        select distinct corpid, cn
        from Mytable where Country ='ITA'; 
        master varchar2(50);
        TYPE Tier_arr_t IS TABLE OF VARCHAR2(50) INDEX BY PLS_INTEGER;
        Tier_arr TIER_ARR_T;  
Begin 
    for rec in c1 
    loop 
        dbms_output.put_line(rec.cn);

        DECLARE
            Cursor C2
            is
                SELECT CN 
                FROM Mytable A
                CONNECT BY PRIOR A.reportsto=A.corpid      
                START WITH A.corpid=rec.corpid     
                order by rownum desc;
        Begin
             open C2;

             for i in 1..8
             loop              
                  fetch C2 into master;
                  dbms_output.put_line(master);
                  Tier_arr(i) :=master ;
             end loop;
        end;

        if TIER1 is null
        then
              TIER1:='';
        end if;
        update mytable set VP_TIER1=Tier_arr(1), VP_TIER2=Tier_arr(2) where corpid=rec.corpid;
    end loop;  
end; 

可能还有一种更基于集合的方法来执行此操作,这将是更可取的,但如果这只是一次性需求,那么这应该可行。

Your concatenation of the literal and variable aren't going to evaluate to the variable TIER1 like you are hoping. Try using arrays instead:

DECLARE  
    cursor c1
    is 
        select distinct corpid, cn
        from Mytable where Country ='ITA'; 
        master varchar2(50);
        TYPE Tier_arr_t IS TABLE OF VARCHAR2(50) INDEX BY PLS_INTEGER;
        Tier_arr TIER_ARR_T;  
Begin 
    for rec in c1 
    loop 
        dbms_output.put_line(rec.cn);

        DECLARE
            Cursor C2
            is
                SELECT CN 
                FROM Mytable A
                CONNECT BY PRIOR A.reportsto=A.corpid      
                START WITH A.corpid=rec.corpid     
                order by rownum desc;
        Begin
             open C2;

             for i in 1..8
             loop              
                  fetch C2 into master;
                  dbms_output.put_line(master);
                  Tier_arr(i) :=master ;
             end loop;
        end;

        if TIER1 is null
        then
              TIER1:='';
        end if;
        update mytable set VP_TIER1=Tier_arr(1), VP_TIER2=Tier_arr(2) where corpid=rec.corpid;
    end loop;  
end; 

There is also probably a more set-based approach to doing this which would be much preferred, but this should work if it is just a one-time need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文