Oracle存储过程的题

2024-12-30 01:56:40
推荐回答(1个)
回答1:

声明:以下代码没经过严密测试,只是编译通过,books表是登记表,book_kind是分类表,因为不知道详细环境,所以写的麻烦了一点
CREATE OR REPLACE PROCEDURE Pro_Book_Test IS
--二级为空的游标
CURSOR Vc_Book_s IS
SELECT * FROM Books b WHERE b.Sec_Id IS NULL;
--一级为空的游标
CURSOR Vc_Book_f IS
SELECT * FROM Books b WHERE b.Fir_Id IS NULL;
Vr_Book Books%ROWTYPE;
v_Id VARCHAR2(5);
BEGIN
OPEN Vc_Book_s();
LOOP
FETCH Vc_Book_s
INTO Vr_Book;
EXIT WHEN Vc_Book_s%NOTFOUND OR Vc_Book_s%NOTFOUND IS NULL;
--根据三级查出二级
SELECT k.Par_Id
INTO v_Id
FROM Book_Kind k
WHERE k.Kind_Id = Vr_Book.Thi_Id;
--更新到登记表二级上
UPDATE Books b SET b.Sec_Id = v_Id WHERE b.Book_Id = Vr_Book.Book_Id;
COMMIT;
END LOOP;
CLOSE Vc_Book_s;
OPEN Vc_Book_f();
LOOP
FETCH Vc_Book_f
INTO Vr_Book;
EXIT WHEN Vc_Book_f%NOTFOUND OR Vc_Book_f%NOTFOUND IS NULL;
--根据二级查一级
SELECT k.Par_Id
INTO v_Id
FROM Book_Kind k
WHERE k.Kind_Id = Vr_Book.Sec_Id;
--登记表一级上
UPDATE Books b SET b.Fir_Id = v_Id WHERE b.Book_Id = Vr_Book.Book_Id;
COMMIT;
END LOOP;
CLOSE Vc_Book_f;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
Raise_Application_Error(-20000, '错误' || SQLCODE || SQLERRM);
END Pro_Book_Test;