Bulk ref cursor into nested table showing error - oracle

I Created a nested table
I Want to bulk ref cursor (C_get_cards) into the nested table created.
I Want to use nested table as one of the column in table STAT_QUERIES.
How do I combine all the three steps in a single procedure? Please correct me if my approach is wrong.
--Nested Table---
CREATE OR REPLACE TYPE T_card_details AS TABLE OF VARCHAR2(20) -- define type
/
CREATE TYPE Query AS OBJECT ( -- create object
CARD_QUERY T_card_details) -- declare nested table as attribute
/
CREATE TABLE STAT_QUERIES (
OBJECTTYPE VARCHAR2(50),
CATEGORY VARCHAR2(100),
CARD_QUERY T_card_details)
NESTED TABLE CARD_QUERY STORE AS CARD_QUERY_TAB;
select * FROM STAT_QUERIES
-- The cursor bulk collect into T_CARD details
CREATE OR REPLACE PROCEDURE NRMSP_INVENTORYSTATS IS
CURSOR C_get_cards
IS
SELECT
nt.name nt_name
,nd.name nd_name
,ct.name ct_name
,ps.name ps_name
,count(c.cardid)
FROM cardtype ct, card c, node n, nodetype nt, nodedef nd, provisionstatus ps
WHERE ct.name in ('SRA AMP', 'XLA AMP', 'SAM', 'ESAM')
AND ct.cardtypeid = c.card2cardtype
AND c.card2node = n.nodeid
AND n.node2nodetype = nt.nodetypeid
AND n.node2nodedef = nd.nodedefid
AND c.card2provisionstatus = ps.provisionstatusid
GROUP by nt.name, nd.name, ct.name, ps.name
;
TYPE cards_TAB_TYPE IS TABLE OF C_get_cards%ROWTYPE INDEX BY BINARY_INTEGER ;
T_card_details cards_TAB_TYPE ;
BEGIN
OPEN C_get_cards ;
FETCH C_get_cards
BULK COLLECT INTO T_card_details
LIMIT 250
;
END;
/
---INSERT INTO STAT_QUERIES---
INSERT INTO STAT_QUERIES
VALUES('CARD', 'Cards from Other Projects 2014', 'CARD_QUERY');

Related

PLSQL : Insert Result from Cursor into one column of plsql table

The following is my code To create a table object :
TYPE TempObjectsTable IS TABLE OF t_temp_objects%ROWTYPE
INDEX BY BINARY_INTEGER;
nt_scb_temp_objects TempObjectsTable;
The t_temp_objects has the following Columns defined :
Name Null? Type
-------------- ----- -------------
INVC_REF NUMBER
ORDERS NUMBER
ORDER_POS_TYPE NUMBER
RULE_CONDITION VARCHAR2(500)
CHARGE NUMBER
CURRENCY VARCHAR2(10)
TXN_DT DATE
Now, I have a cursor, which returns a lists of Orders, basically numbers.
CURSOR c_orders_frm_grp IS
select a.ordr_id from sa_order a
WHERE a.invc_ref is NULL
I am trying to add these to the plsql table created nt_scb_temp_objects above by using bulk collect. But i want the rest of the columns of nt_scb_temp_objects to filled as null for now, as i will be filling these columns as well in the coming steps.
Currently this is what i am trying.
IF c_orders_frm_grp %ISOPEN THEN
CLOSE c_orders_frm_grp ;
END IF;
OPEN c_orders_frm_grp;
FETCH c_orders_frm_grp BULK COLLECT INTO nt_scb_temp_objects.orders;
CLOSE c_orders_frm_grp;
And this is the error i get : Error(44,74): PLS-00302: component 'ORDERS' must be declared
You do not want that CURSOR and OPEN..FETCH constructs. Simply run a SELECT BULK COLLECT INTO
that collection.
DECLARE
TYPE TempObjectsTable IS TABLE OF t_temp_objects%ROWTYPE
INDEX BY BINARY_INTEGER;
nt_scb_temp_objects TempObjectsTable;
BEGIN
select a.ordr_id as ORDERS,
null as INVC_REF,
null as ORDER_POS_TYPE,
null as RULE_CONDITION,
null as CHARGE,
null as CURRENCY,
null as TXN_DT
BULK COLLECT INTO nt_scb_temp_objects from sa_order a
WHERE a.invc_ref is NULL ;
END;
/
DEMO
Why not use an INSERT INTO ... SELECT, and only specify the single column you want to populate now:
INSERT INTO TempObjectsTable(ORDERS)
SELECT ordr_id
FROM sa_order
WHERE invc_ref IS NULL;
In general you should avoid using cursors, as most regular database operations in SQL are already set based.
Note: If the temp table TempObjectsTable does not already exist, then you will have to create it.

oracle, adding a new line to a nested table

I have these three object
create or replace
type type_client
( num int ,
username varchar(30),
balance int,
ta table_achat,
ref_admin ref type_admin,
member function get_prix_achat_total return int );
create or replace
type table_achat as table of achat ;
create or replace
type achat as object ( num_item int , qte int
);
create table table_client OF type_client ;
suppose in an entry of table_client .. we have a nested table like this :
(num_item,qte) : (1 , 5),(2 , 3)
what I want is the nested table be like this (for example):
(num_item,qte) : (1 , 5),(2 , 3)(3 , 44)
What I mean is, how to add a new line to an already created nested table while keeping existing entries? ..
We can use the MULTISET UNION operator to create a new set from two sets. In your case one of those sets is your existing set and the second set is the set of new entries.
Here is a demo based on a simplified version of your set-up:
declare
nt table_achat;
begin
nt := table_achat(achat(1 , 5),achat(2 , 3));
dbms_output.put_line(nt.count());
nt := nt multiset union table_achat(achat(3 , 44));
dbms_output.put_line(nt.count());
end;
/
Given a table T42 with a column COL_NT which is a nested table of your table_achat type you could insert a new entry in the nested table like this:
insert into the
(select col_nt from t42 where id = 1)
values (achat(3,44));
irrelevant from the question, which i couldn't and didn't try to understand, you can not combine insert + select + values statements as you did before. Perhaps you may prefer among below ones :
insert into the -- if table has one string column
(select ta from table_client where username=user);
OR
insert into the -- if table has two numeric columns
values (3,44);

What is the purpose of "RETURN AS VALUE" in NESTED TABLES (Oracle 9i)

Is there a specific case, when i should use RETURN AS VALUE?
Normally i use only NESTED TABLE xxx STORE AS xxx
For example:
CREATE OR REPLACE TYPE address_t AS OBJECT (
ADDID NUMBER(10,0),
STREET VARCHAR2(40),
ZIP VARCHAR2(5),
CITY VARCHAR2(40)
)
/
CREATE OR REPLACE TYPE addresses_nt AS TABLE OF address_t
/
CREATE OR REPLACE TYPE invoicepos_t AS OBJECT (
ARTID NUMBER(10,0),
AMOUNT NUMBER(10,0)
)
/
CREATE OR REPLACE TYPE invoicepos_nt AS TABLE OF invoicepos_t
/
CREATE OR REPLACE TYPE customer_t AS OBJECT (
CUSID NUMBER(10,0),
FIRSTNAME VARCHAR2(30),
LASTNAME VARCHAR2(30),
ADDRESSES addresses_nt
)
/
CREATE OR REPLACE TYPE invoice_t AS OBJECT (
INVOICEID NUMBER(10,0),
CUSTOMER REF customer_t,
ADDID NUMBER(10,0),
POSITIONS invoicepos_nt
)
/
CREATE TABLE customer OF customer_t
NESTED TABLE ADDRESSES STORE AS all_adresses RETURN AS VALUE
/
CREATE TABLE invoices OF invoice_t
NESTED TABLE POSITIONS STORE AS all_invoicepos RETURN AS VALUE
/
As far as I can tell, the only difference is that LOCATORs are a bit faster than VALUEs. But that doesn't make sense and I'm hoping somebody will prove me wrong; there's almost never a "fast=true" switch.
According to the SQL Language Reference:
RETURN [AS] Specify what Oracle Database returns as the result of a query.
VALUE returns a copy of the nested table itself.
LOCATOR returns a collection locator to the copy of the nested table.
The locator is scoped to the session and cannot be used across sessions. Unlike a LOB locator, the collection locator cannot be used to modify the collection instance.
This implies that LOCATORs are read-only. But on 11gR2 a LOCATOR can still be modified.
The Object Relational Developer's Guide also discusses LOCATORs, but does not mention any downsides to using them.
Sample Schema
CREATE OR REPLACE TYPE invoicepos_t AS OBJECT (
ARTID NUMBER(10,0),
AMOUNT NUMBER(10,0)
)
/
CREATE OR REPLACE TYPE invoicepos_nt AS TABLE OF invoicepos_t
/
create table invoices_val
(
INVOICEID NUMBER,
POSITIONS invoicepos_nt
)
NESTED TABLE POSITIONS STORE AS all_invoicepos_val RETURN AS VALUE
/
create table invoices_loc
(
INVOICEID NUMBER,
POSITIONS invoicepos_nt
)
NESTED TABLE POSITIONS STORE AS all_invoicepos_loc RETURN AS locator
/
insert into invoices_val values(1, invoicepos_nt(invoicepos_t(1,1)));
insert into invoices_loc values(1, invoicepos_nt(invoicepos_t(1,1)));
insert into invoices_def values(1, invoicepos_nt(invoicepos_t(1,1)));
commit;
Compare performance and funcionality
--Value: 1.0 seconds
declare
v_positions invoicepos_nt;
begin
for i in 1 .. 10000 loop
select positions
into v_positions
from invoices_val;
end loop;
v_positions.extend;
v_positions(2) := invoicepos_t(3,3);
update invoices_val set positions = v_positions;
end;
/
--Locator: 0.8 seconds
declare
v_positions invoicepos_nt;
begin
for i in 1 .. 10000 loop
select positions
into v_positions
from invoices_loc;
end loop;
v_positions.extend;
v_positions(2) := invoicepos_t(3,3);
update invoices_loc set positions = v_positions;
end;
/

SELECT from a bulk collection

Is it possible to select from a bulk collection?
Something along these lines:
DECLARE
CURSOR customer_cur IS
SELECT CustomerId,
CustomerName
FROM Customers
WHERE CustomerAreaCode = '576';
TYPE customer_table IS TABLE OF customer_cur%ROWTYPE;
my_customers customer_table;
BEGIN
OPEN customer_cur;
FETCH customer_cur
BULK COLLECT INTO my_customers;
-- This is what I would like to do
SELECT CustomerName
FROM my_customers
WHERE CustomerId IN (1, 2, 3);
END;
I don't seem to be able to select from the my_customers table.
Yes, you can. Declare yourself schema-level types as follows:
create or replace rec_customer_cur
as
object (
customerid integer, -- change to the actual type of customers.customerid
customername varchar2(100) -- change to the actual type of customers.customername
);
/
create or replace type customer_table
as
table of rec_customer_cur;
/
Then, in your PLSQL code, you can declare
CURSOR customer_cur IS
SELECT new rec_customer_cur(CustomerId, CustomerName)
FROM Customers
WHERE CustomerAreaCode = '576';
... and then use ...
SELECT CustomerName
INTO whatever
FROM table(my_customers)
WHERE CustomerId IN (1, 2, 3);
This is because schema-level types can be used in SQL context.
If you want to also display the dataset returned by the select, then just use a REF CURSOR as an OUT parameter.
The SELECT ...FROM TABLE is a SQL statement, which needs a STATIC TABLE NAME, as a database object. It throws an error since the collection name is not actually a database table as an object.
To return the dataset, use SYS_REFCURSOR as OUT parameter.
open cur as select....

Deleting specific record from nested table Oracle DB

I'm having problems deleting specific record from the table (ORACLE DB).
I have a table with a nested table inside of it.
Table structure looks like this: where ML - nested table
Name, City, ML(Brand, Model, ID, Year, Price)
What I need to do is delete specific record with ID of 'L201'.
What I have tried so far:
SELECT B.ID FROM TABLE Dock A, Table(A.ML) B;
This is working giving me all the ID's.
Output:
ID
____
B201
S196
L201
This is not working when trying to delete the record:
DELETE FROM Dock
(SELECT B.ID FROM Dock A, Table(A.ML) B) C
WHERE C.ID = 'L201';
Getting error:
Line 2: SQL command not properly ended;
DELETE FROM TABLE
(SELECT D.ML FROM Dock D) E
WHERE E.ID = 'L201';
Throws an error:
single-row subquery returns more than one row
Maybe this one:
DELETE FROM
(SELECT A.Name, A.City, d.Brand, d.Model, d.ID, d.Year, d.Price
FROM Dock A, TABLE(ML) d)
WHERE ID = 'L201';
Update:
Another trial before we gonna make it more advanced:
DELETE FROM Dock
WHERE ROWID =ANY (SELECT a.ROWID FROM Dock a, TABLE(ML) b WHERE b.ID = 'L201');
Update 2:
If you prefer it more object-oriented, this one should work as well. At least I did not get any error.
CREATE OR REPLACE TYPE ML_TYPE AS OBJECT (
brand VARCHAR2(100),
ID VARCHAR2(20),
MODEL VARCHAR2(20),
YEAR NUMBER,
Price NUMBER,
MAP MEMBER FUNCTION getID RETURN VARCHAR2,
CONSTRUCTOR FUNCTION ML_TYPE(ID IN VARCHAR2) RETURN SELF AS RESULT);
CREATE OR REPLACE TYPE BODY ML_TYPE IS
CONSTRUCTOR FUNCTION ML_TYPE(ID IN VARCHAR2) RETURN SELF AS RESULT IS
-- Constructor to create dummy ML-Object which contains just an ID,
-- used for comparison
BEGIN
SELF.ID := ID;
RETURN;
END ML_TYPE;
MAP MEMBER FUNCTION getID RETURN VARCHAR2 IS
BEGIN
RETURN SELF.ID;
END getID;
END;
/
CREATE OR REPLACE TYPE ML_TABLE_TYPE IS TABLE OF ML_TYPE;
CREATE TABLE Dock (Name VARCHAR2(20), City VARCHAR2(20), ML ML_TABLE_TYPE)
NESTED TABLE ML STORE AS ML_NT;
insert into Dock values ('A', 'NY', ML_TABLE_TYPE(
ML_TYPE('brand1','L301','Model 2',2013, 1000),
ML_TYPE('brand2','L101','Model 3',2013, 1000)));
insert into Dock values ('B', 'NY', ML_TABLE_TYPE(
ML_TYPE('brand3','K301','Model 4',2014, 3000),
ML_TYPE('brand4','K101','Model 5',2014, 3000)));
insert into Dock values ('A', 'NY', ML_TABLE_TYPE(
ML_TYPE('brand5','K301','Model 8',2012, 2000),
ML_TYPE('brand6','L201','Model 9',2012, 2000)));
DELETE FROM Dock WHERE ML_TYPE('L201') MEMBER OF ML;
After reading some literature I think I found the correct way to do this.
By defining a mapping-function for your object-type you can compare two nested tables directly.
Example:
-- creating the custom object type
create or replace type ml_type as object (
brand varchar2(100),
id varchar2(20),
map member function sort_key return varchar2
);
-- creating the object type body and defining the map-function
create or replace type body ml_type as
map member function sort_key return varchar2 is
begin
return self.brand || '|' || self.id;
end;
end;
/
-- creating the nested table of custom type
create or replace type ml_tab as table of ml_type;
-- deleting from your table by comparing the nested-table elements
delete from dock where ml = (select ml from dock a, table(a.ml) b where b.id = 'L201');
In this example the map-functions is just returning the concatenated version of brand and id, but you can define it to what you want/need.

Resources