【问题标题】:How to manipulate data according to cursor values which are also the return value of a stored procedure in Oracle sql?如何根据游标值操作数据,游标值也是Oracle sql中存储过程的返回值?
【发布时间】:2011-07-12 08:55:12
【问题描述】:

我有一个返回游标的 oracle sql 存储过程。 此游标在存储过程主体中获取复杂选择语句的值(在下面的示例中,我使选择语句变得简单)。

然后,我想将光标用于两件事: 1.作为存储过程的返回值 2. 使用它的数据来更新存储过程体中另一个表中的一些值

我不知道怎么做,所以同时我不得不复制(复杂的)select语句并让另一个游标有它的值来更新另一个表。

create or replace procedure sp_GetBuildings(returned_cursor OUT SYS_REFCURSOR,
                                                 timeFrameHrsParam number) is
  v_buildingID Buildings.buildingId%type;

        cursor t_result is
            select customerId
            from (select buildingId from Buildings) b
            inner join Customers c on c.building_id = b.building_id; 
    begin 
        open returned_cursor for
            select customerId
              from (select buildingId from Buildings) b
              inner join Customers c on c.building_id = b.building_id; 
        for t in t_result
            loop
                v_buildingID := t.building_id;                 
                update Buildings set already = 1 where building_id = v_buildingID ;
            end loop;          
        commit;
    end sp_GetBuildings;

你能帮我找到一个解决方案来节省我的 select 语句复制吗?

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    您不能在 Oracle 中复制或克隆游标。游标只是指向结果集的指针,读取游标会沿结果列表沿单个方向移动。但是,您可以使用数组实现非常相似的效果:

    CREATE TYPE nt_number AS TABLE OF NUMBER;
    
    CREATE OR REPLACE PROCEDURE sp_getbuildings(returned_table OUT nt_number,
                                                timeframehrsparam NUMBER) IS
       CURSOR t_result IS
          SELECT   customerid
            FROM        buildings b
                   JOIN customers c
                     ON c.building_id = b.building_id;
       i   NUMBER;
    BEGIN
       OPEN t_result;
    
       FETCH t_result
       BULK COLLECT INTO   returned_table;
    
       CLOSE t_result;
    
       FORALL i IN returned_table.FIRST .. returned_table.LAST
          UPDATE   buildings
             SET   already = 1
           WHERE   building_id = v_buildingid;
    
       COMMIT;
    END sp_getbuildings;
    

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      相关资源
      最近更新 更多