如何根据游标值操作数据,游标值也是Oracle sql中存储过程的返回值?
我有一个返回游标的oracle sql存储过程。 此游标在存储过程体中获取复杂select语句的值(在下面的示例中,我使select语句变得简单) 然后,我想用光标做两件事: 1.将其用作存储过程的返回值 2.使用其数据更新存储过程体中另一个表中的某些值 我找不到该怎么做,所以在此期间,我不得不复制(复杂的)select语句,让另一个游标有它的值来更新另一个表如何根据游标值操作数据,游标值也是Oracle sql中存储过程的返回值?,sql,oracle,Sql,Oracle,我有一个返回游标的oracle sql存储过程。 此游标在存储过程体中获取复杂select语句的值(在下面的示例中,我使select语句变得简单) 然后,我想用光标做两件事: 1.将其用作存储过程的返回值 2.使用其数据更新存储过程体中另一个表中的某些值 我找不到该怎么做,所以在此期间,我不得不复制(复杂的)select语句,让另一个游标有它的值来更新另一个表 create or replace procedure sp_GetBuildings(returned_cursor OUT SYS_
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语句复制?您不能在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;