I've got a procedure where I need to cache some data, for performance reasons, for downstream operations.
The TYPE definitions work
The BULK COLLECT INTO works
The SELECT does not work
PROCEDURE MYPROC((PARAMS))AS
TYPE REC_TYPE IS RECORD (
COLUMN_1 (TABLEA.COLUMN_A)%TYPE,
COLUMN_2 (TABLEA.COLUMN_B)%TYPE
);
TYPE TAB_TYPE IS TABLE OF REC_TYPE;
TABLE_1 TAB_TYPE;
BEGIN
SELECT COLUMN_A, COLUMN_B
BULK COLLECT INTO TABLE_1
FROM TABLE_A;
SELECT * FROM TABLE_1;
END MYPROC;
Yields:
Error(#,#): PL/SQL: ORA-00942: table or view does not exist
I've also tried wrapping it in a table function like I do with my single-column types elsewhere, but that did not work either
SELECT * FROM TABLE(TABLE_1);
Error(#,#): PL/SQL: ORA-22905: cannot access rows from a non-nested
table item
Your problem is actually a PLS-00642 error, rather than ORA-22905. Essentially you can't use local collection types in SQL statements. The solution therefore, is to define your types at the schema level. When defining types in this way, we cannot use the %TYPE syntax, and instead must explicitly define the column (Getting PLS-00201 error while creating a type in oracle) i.e.
create or replace type rec_type as object (
COLUMN_1 integer,
COLUMN_2 varchar2(128)
);
create or replace type tab_type as table of rec_type;
You then need to explicitly convert the values into the relevant type in order to perform the bulk collect as mentioned here: ORA-00947 Not enough values while declaring type globally.
Your procedure would therefore look something like this:
PROCEDURE MYPROC((PARAMS))AS
TABLE_1 TAB_TYPE;
lCount integer;
BEGIN
SELECT REC_TYPE(COLUMN_A, COLUMN_B)
BULK COLLECT INTO TABLE_1
FROM TABLE_A;
SELECT COUNT(*) INTO lCount FROM TABLE(TABLE_1);
END MYPROC;
I'm trying to return the inserting row into a table of ROWTYPE in PL/SQL but I have the error:
ORA-00936: missing expression.
Table type declaration in package head:
TYPE t_retourResult IS TABLE OF RESULTATS%ROWTYPE;
And error at inserting in the package body:
PROCEDURE EncoderResultats(p_tResultats IN t_resultats, p_tAjoutes OUT t_retourResult, p_tErreurs OUT t_erreur)
IS
ExceptTResultats EXCEPTION;
i INTEGER;
BEGIN
IF(p_tResultats.COUNT = 0) THEN RAISE ExceptTResultats; END IF;
FORALL i IN INDICES OF p_tResultats SAVE EXCEPTIONS
INSERT INTO RESULATS VALUES p_tResultats(i) RETURNING * BULK COLLECT INTO p_tAjoutes;
COMMIT;
EXCEPTION
WHEN ExceptTResultats THEN RAISE_APPLICATION_ERROR(-20006,'Le tableau est vide, aucun resultat à ajouter');
WHEN OTHERS THEN RAISE;
END EncoderResultats;
there is a typo error in
INSERT INTO RESULATS VALUES p_tResultats(i) RETURNING * BULK COLLECT INTO p_tAjoutes;
it is not the table RESULATS but "RESULTATS"
moreover i guess you can't use "RETURNING * " , you have to specify all the column names as reported also here:
PL/SQL How return all attributes in ROW
I'm executing PL/SQL code to display the Currency Code from the Failed Reservation table. Object type and Nested Table collections are used.
When the PL/SQL code is run, the following error is generated. The corresponding line is highlighted in the PL/SQL code section.
Error report:
ORA-06550: line 27, column 11:
PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got NUMBER
ORA-06550: line 27, column 4:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
The code is pasted below:
DDL - Table creation:
CREATE TABLE FAILEDRESERVATION
(
FAILEDRESERVATIONID NUMBER(18,0),
FK_TRANSACTIONID NUMBER(18,0),
DEBITRESERVATIONID NUMBER(18,0),
RESERVATIONTIME DATE,
RESERVATIONAMOUNT NUMBER(18,5),
CURRENCYCODE CHAR(3 BYTE),
AVAILABLEAMOUNT NUMBER(18,5)
);
ALTER TABLE FAILEDRESERVATION
ADD CONSTRAINT "PK_FAILEDRESERVATION" PRIMARY KEY ("FAILEDRESERVATIONID");
Object Type:
CREATE OR REPLACE TYPE TYPE type_failedreservation AS OBJECT
(
FK_TRANSACTIONID NUMBER(18),
DEBITRESERVATIONID NUMBER(18),
RESERVATIONTIME DATE,
RESERVATIONAMOUNT NUMBER(18,5),
CURRENCYCODE CHAR(3),
AVAILABLEAMOUNT NUMBER(18,5)
);
DML:
INSERT INTO FAILEDRESERVATION (FAILEDRESERVATIONID,FK_TRANSACTIONID,DEBITRESERVATIONID,RESERVATIONTIME,RESERVATIONAMOUNT,CURRENCYCODE,AVAILABLEAMOUNT)
VALUES (289,2,1,to_date('07-MAR-16','DD-MON-RR'),20000,'USD',10000);
INSERT INTO FAILEDRESERVATION (FAILEDRESERVATIONID,FK_TRANSACTIONID,DEBITRESERVATIONID,RESERVATIONTIME,RESERVATIONAMOUNT,CURRENCYCODE,AVAILABLEAMOUNT)
VALUES (288,1,1,to_date('01-MAR-16','DD-MON-RR'),10000,'NOK',10000);
Nested Tables:
CREATE OR REPLACE TYPE type_failedreservation_coll as TABLE OF type_failedreservation;
CREATE OR REPLACE TYPE type_dbtrsid_coll AS TABLE OF NUMBER;
PL/SQL Code:
DECLARE
P_FAILEDRESERVATION APPDATA.TYPE_FAILEDRESERVATION_COLL;
vdbtid_coll type_dbtrsid_coll := type_dbtrsid_coll();
BEGIN
SELECT TYPE_FAILEDRESERVATION(fk_transactionid,debitreservationid,reservationtime,reservationamount,currencycode,availableamount)
BULK COLLECT
INTO p_failedreservation
FROM failedreservation;
-- This is line 27
SELECT frs.debitreservationid
INTO vdbtid_coll
FROM TABLE(p_failedreservation) frs;
FOR v_iterate IN vdbtid_coll.FIRST..vdbtid_coll.LAST
LOOP
dbms_output.put_line('The currency code is: '||v_iterate);
END LOOP;
END;
Why is the code generating this error ?
You've declared vdbtid_coll as a collection type, so you need to bulk collect into that too:
SELECT frs.debitreservationid
BULK COLLECT INTO vdbtid_coll
FROM TABLE(p_failedreservation) frs;
With that change:
PL/SQL procedure successfully completed.
The currency code is: 1
The currency code is: 2
That's just giving you the index number in the collection though, so I don't think it's what you really want. You may want:
FOR v_iterate IN vdbtid_coll.FIRST..vdbtid_coll.LAST
LOOP
dbms_output.put_line('The currency code is: '
|| p_failedreservation(v_iterate).currencycode);
END LOOP;
which gets:
PL/SQL procedure successfully completed.
The currency code is: USD
The currency code is: NOK
You don't really need the second select/collection at all though, you can do:
FOR v_iterate IN 1..p_failedreservation.COUNT
LOOP
dbms_output.put_line('The currency code is: '
|| p_failedreservation(v_iterate).currencycode);
END LOOP;
... for the same result. Although I'm not sure what the relevance of the debitreservationid is in that second query, as it is the same value (1) in both rows.
You are trying to select multiple rows into a collection. You need to use BULK COLLECT INTO rather than just INTO.
Change
SELECT frs.debitreservationid
INTO vdbtid_coll
FROM TABLE(p_failedreservation) frs;
To
SELECT frs.debitreservationid
BULK COLLECT INTO vdbtid_coll
FROM TABLE(p_failedreservation) frs;
and you probably want the output to be:
FOR v_iterate IN vdbtid_coll.FIRST..vdbtid_coll.LAST LOOP
dbms_output.put_line('The currency code is: '|| vdbtid_coll(v_iterate) );
END LOOP;
However, you could simplify it all to:
DECLARE
P_FAILEDRESERVATION APPDATA.TYPE_FAILEDRESERVATION_COLL;
BEGIN
SELECT TYPE_FAILEDRESERVATION(
fk_transactionid,
debitreservationid,
reservationtime,
reservationamount,
currencycode,
availableamount
)
BULK COLLECT INTO p_failedreservation
FROM failedreservation;
FOR v_iterate IN p_failedreservation.FIRST .. p_failedreservation.LAST LOOP
dbms_output.put_line(
'The currency code is: '|| p_failedreservation(v_iterate).currencycode
);
END LOOP;
END;
Try casting the type array to its type as follows
SELECT frs.debitreservationid
BULK COLLECT INTO vdbtid_coll
FROM TABLE(CAST(p_failedreservation as APPDATA.TYPE_FAILEDRESERVATION_COLL)) frs;
PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got CHAR
You may get this error in case you write type table in place of type object when
using BULK COLLECT INTO, i faced this error coz i did so.
FOR EXAMPLE:
CREATE OR REPLACE TYPE OBJ_TYPE AS OBJECT (NAME VARCHAR2(20));
/
CREATE OR REPLACE TYPE TBL_TYPE IS TABLE OF OBJ_TYPE;
/
CREATE OR REPLACE FUNCTION FUNC1 RETURN TBL_TYPE AS
TBL_TEXT TBL_TYPE := TBL_TYPE();
BEGIN
SELECT ***OBJ_TYPE*** (NAME) BULK COLLECT INTO ***TBL_TEXT*** FROM <table-name> ;
RETURN ***TBL_TEXT***;
END;
/
Care should be taken when using object type and table type in the function.
I have a regular table with entries and several types declared like that:
create TYPE entity as OBJECT ( attribute1 NUMBER);
create TYPE entity_array as OBJECT of entity;
And now I want using the sql worksheet join declared entity_array object with my table:
DECLARE
entity_array_obj entity_array := entity_array(
entity(11111),
entity(22222)
);
BEGIN
select * from mytable as t1
inner join entity_array_obj as t2 on (t1.attribute1 = t2.attribute1);
END;
And I'm getting compilation error:
PL/SQL: ORA-00933: SQL command not properly ended
Please suggest
create type entity as object ( attribute1 number);
create type entity_array as table of entity;
create table mytable(attribute1 number);
insert into mytable values (11111);
declare
entity_array_obj entity_array := entity_array(
entity(11111),
entity(22222)
);
v_count number;
begin
select count(*)
into v_count
from mytable t1
join table(entity_array_obj) t2
on t1.attribute1 = t2.attribute1;
dbms_output.put_line('Count: '||v_count);
end;
/
Here are the main changes made to the code:
Changed as OBJECT of entity; to as TABLE of entity;. You may not have noticed this error because some IDEs suppress code compilation errors.
Table aliasing in Oracle does not work with "as".
Selects in PL/SQL must always select INTO something.
The table() operator converts the collection into a row source.
Simply trying to get a cursor back for the ids that I specify.
CREATE OR REPLACE PACKAGE some_package AS
TYPE t_cursor IS REF CURSOR;
TYPE t_id_table IS TABLE OF NVARCHAR(38) INDEX BY PLS_INTEGER;
PROCEDURE someentity_select(
p_ids IN t_id_table,
p_results OUT t_cursor);
END;
CREATE OR REPLACE PACKAGE BODY some_package AS
PROCEDURE someentity_select(
p_ids IN t_guid_table,
p_results OUT t_cursor)
IS
BEGIN
OPEN p_results FOR
SELECT *
FROM someschema.someentity
WHERE id IN (SELECT column_value FROM TABLE(p_ids)); - fails here
END;
END;
Note: someschema.someentity.id is a NVARCHAR2(38)
PL/SQL: ORA-00382: expression is of wrong type
PL/SQL: ORA-22905: cannot access rows from a non-nested table item
Where am I going wrong?
In Oracle versions prior to 12.2 you can only SELECT from a collection type that is defined in the database via a CREATE TYPE statement, not an associative array:
CREATE TYPE t_id_table IS TABLE OF NVARCHAR(38);
CREATE OR REPLACE PACKAGE some_package AS
PROCEDURE someentity_select(
p_ids IN t_guid_table,
p_results OUT SYS_REFCURSOR);
END;
CREATE OR REPLACE PACKAGE BODY some_package AS
PROCEDURE someentity_select(
p_ids IN t_guid_table,
p_results OUT SYS_REFCURSOR)
IS
BEGIN
OPEN p_results FOR
SELECT *
FROM someschema.someentity
WHERE id IN (SELECT column_value FROM TABLE(p_ids));
END;
END;
This is an index-by table, which is a PL/SQL type.
You can only use SQL types in the SQL engine of Oracle. Or PL/SQL types, that Oracle can hack around to look like SQL types.
You can have a simple array-like collection and use it as a result. (no index by)
type TGuidList is table of NVarchar(38);
But the best compatibility and stability, you get by declaring it as a global SQL type and use that inside your package:
create type TGuidList is table of NVarchar(38);
Edit: You will not need an NVarChar for a GUID, will you? A good ol' VarChar should do the trick just fine.