为每行循环并选择结果

问题描述 投票:0回答:1

我想遍历查询结果,然后为每个项目将ID插入temp_table。我已编写查询:

    BEGIN
    FOR item IN
    (SELECT ID FROM INV_SRV1 WHERE SRV_CATEGORY_ID IN 
                (
                    SELECT id
                    FROM   inv_srv_category1
                    START WITH parent_category_id = 1
                    CONNECT BY PRIOR id = parent_category_id
                ) OR SRV_CATEGORY_ID = 1)
    LOOP
INSERT INTO TEMP_TABLE (ID)
    (SELECT s.id
        FROM   inv_srv1 s
        START WITH s.parent_srv_id = item.id
        CONNECT BY PRIOR item.id = s.parent_srv_id);
    END LOOP;
    END;

预期结果(来自TEMP_TABLE的SELECT ID):

+----+
| ID |
+----+
| 1  |
+----+
| 2  |
+----+
| 3  |
+----+
| 4  |
+----+
| 5  |
+----+
| 6  |
+----+
| 7  |
+----+
| 8  |
+----+
| 9  |
+----+
| 10 |
+----+
| 11 |
+----+
| 12 |
+----+
| 13 |
+----+
| 14 |
+----+
| 15 |
+----+
| 16 |
+----+
| 17 |
+----+
| 18 |
+----+
| 19 |
+----+

但是它给了我错误:

Error report -
ORA-06550: line 11, column 2:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:

   ( - + case mod new not null <an identifier>
   <a double-quoted delimited-identifier> <a bind variable>
   continue avg count current exists max min prior sql stddev
   sum variance execute forall merge time timestamp interval
   date <a string literal with character set specification>
   <a number> <a single-quoted SQL string> pipe
   <an alternatively-quoted string literal with character set specification>
   <an alternat
ORA-06550: line 14, column 47:
PLS-00103: Encountered the symbol ")" when expecting one of the following:

   . ( * @ % & - + ; / at for mod remainder rem
   <an exponent (**)> and or group having intersect minus order
   start union where connect || multiset
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

任何想法我该如何运行?

表格:

CREATE TABLE TEMP_TABLE
(
ID NUMBER
)

CREATE TABLE INV_SRV_CATEGORY1
(
    ID number,
    name nvarchar2(255),
    PARENT_CATEGORY_ID number
)

insert into inv_srv_category1(id, name, parent_category_id)
select 1, 'deveti', null from dual
union all select 2, 'deseti', 1 from dual
union all select 3, 'enajsti', 1 from dual
union all select 4, 'dvanajsti', 1 from dual
union all select 5, 'trinajsti', 1 from dual

union all select 6, 'deseti', 2 from SYS.dual
union all select 7, 'enajsti', 2 from dual
union all select 8, 'dvanajsti', 6 from dual
union all select 9, 'trinajsti', 4 from dual

union all select 10, 'deseti', 9 from SYS.dual
union all select 11, 'enajsti', 9 from dual
union all select 12, 'dvanajsti', 11 from dual
union all select 13, 'trinajsti', 12 from dual


CREATE TABLE INV_SRV1
(
    ID number,
    name nvarchar2(255),
    SRV_CATEGORY_ID number,
    PARENT_SRV_ID number
)

insert into inv_srv1(id, name, srv_category_id, parent_srv_id)
select 1, 'under root 1', 1, null from dual
union all select 2, 'under root 1', 1, null from dual
union all select 3, 'under root 1', 1, null from dual
union all select 4, 'under root 1', 1, null from dual

union all select 5, 'under srv 4', null, 4 from dual
union all select 6, 'under srv 4', null, 4 from dual
union all select 7, 'under srv 4', null, 4 from dual

union all select 8, 'under srv 7', null, 7 from dual
union all select 9, 'under srv 7', null, 7 from dual
union all select 10, 'under srv 7', null, 7 from dual

union all select 11, 'under cat 7', 7, null from dual
union all select 12, 'under cat 7', 7, null from dual

union all select 13, 'under srv 11', null, 11 from dual
union all select 14, 'under srv 11', null, 11 from dual

union all select 15, 'under srv 11', 5, null from dual
union all select 16, 'under srv 11', 5, null from dual

union all select 17, 'under cat 13', 13, null from dual
union all select 18, 'under cat 13', 13, null from dual
union all select 19, 'under srv 18', null, 18 from dual
oracle plsql
1个回答
1
投票

这是how(意思是:这样就不会失败)。但是,它返回您想要的吗?您应该知道。

SQL> set serveroutput on
SQL> begin
  2     for item
  3        in (select id
  4              from inv_srv1
  5             where    srv_category_id in (    select id
  6                                                from inv_srv_category1
  7                                          start with parent_category_id = 1
  8                                          connect by prior id =
  9                                                        parent_category_id)
 10                   or srv_category_id = 1)
 11     loop
 12       for chi in (select s.id
 13                   from inv_srv1 s
 14                   start with s.parent_srv_id = item.id
 15                   connect by nocycle prior item.id = s.parent_srv_id
 16                  )
 17       loop
 18          dbms_output.put_line(item.id ||' - '|| chi.id);
 19       end loop;
 20     end loop;
 21  end;
 22  /
4 - 5
4 - 6
4 - 7
11 - 13
11 - 14
18 - 19

PL/SQL procedure successfully completed.

SQL>
© www.soinside.com 2019 - 2024. All rights reserved.