Saat berurusan dengan Objects
Anda harus ekstra hati-hati saat membuat select
. Ada kesalahan dalam kode Anda yang menyebabkan masalah. Saya juga tidak begitu yakin apakah penugasan langsung yang dilakukan di bawah ini benar-benar diperbolehkan:
rec := PART_TEST(record);
Namun saya mengusulkan 2 solusi di sini. Pertama dengan Pipeline
dan lainnya tanpa itu. Lihat di bawah:
--Persiapan Tabel dan Objek
CREATE TABLE part_table (
part_no NUMBER,
col1 NUMBER
);
INSERT INTO PART_TABLE VALUES(1,11);
INSERT INTO PART_TABLE VALUES(1,33);
INSERT INTO PART_TABLE VALUES(2,22);
SELECT * FROM PART_TABLE;
CREATE OR REPLACE TYPE part_test IS OBJECT (
part_no NUMBER,
col1 NUMBER
);
CREATE OR REPLACE TYPE part_test_t IS TABLE OF part_test;
/
--Fungsi dengan Saluran Pipa
CREATE OR replace FUNCTION part_test_f(search_part IN number)
RETURN part_test_t PIPELINED
AS
rec part_test; --<--Variable of type Object since we want to piperow.
CURSOR cur(part_num number) IS
WITH A AS
( --Make sure you cast your select statement of object type
SELECT part_test(PART_NO,col1) FROM PART_TABLE WHERE PART_NO LIKE part_num
)
SELECT * FROM A;
BEGIN
OPEN cur(search_part) ;
LOOP
Fetch cur into rec; --<-- Note here am not using `Bulk Collect` even though its being a collection since we are `piping` the rows.
exit when cur%NOTFOUND;
pipe row(rec);
END LOOP;
RETURN ;
END;
/
Hasil:
SQL> SELECT * FROM TABLE (PART_TEST_F(1));
PART_NO COL1
---------- ----------
1 11
1 33
--Tanpa Saluran Pipa
CREATE OR REPLACE FUNCTION part_test_f (search_part IN NUMBER)
RETURN part_test_t
AS
rec part_test_t;
CURSOR cur ( part_num NUMBER) IS
WITH a AS
( SELECT part_test( part_no,col1 )
FROM part_table
WHERE part_no LIKE part_num
)
SELECT * FROM a;
BEGIN
OPEN cur(search_part);
LOOP
FETCH cur BULK COLLECT INTO rec;
EXIT WHEN cur%notfound;
END LOOP;
RETURN rec;
END;
/
Hasil:
SQL> Select * from table (part_test_f(1));
PART_NO COL1
---------- ----------
1 11
1 33
Pilih mana yang cocok untuk Anda, tetapi kita semua tahu keuntungan menggunakan Pipeline
fungsi yang paling cocok.