Using TYPE in PLSQL for multiple tables - oracle

I am using TYPE to get the rowtype in tables. I want to use it to retrieve the rowtypes of Customer and Supplier (i.e. multiple tables).
Below is the code for Customers, how do I retrieve Customer and Supplier with the same code?
TYPE dept_table_type is table of CUSTOMER%ROWTYPE
INDEX BY PLS_INTEGER;
Thank you

You can use the ROWTYPE of a cursor to do what you're trying to do:
CURSOR MULTI_TABLE_CURSOR IS
SELECT *
FROM CUSTOMER
CROSS JOIN SUPPLIER;
TYPE DEPT_TABLE_TYPE IS TABLE OF MULTI_TABLE_CURSOR%ROWTYPE
INDEX BY PLS_INTEGER;
Note that you don't actually need to use the cursor if you don't have a use for it.
Best of luck.

Related

In Oracle I want to create a "routing interface" which insert into separate tables based on parameter

I need to find a solution to the following problem: there should be a common and single "interface" that I can use in an insert into statement, something like this: insert into INTERFACE (fields) select ...
But there are many tables with the same structure behind the interface which should decide based on list of values (coming in a field) where to put the data. The tables are partitioned by range interval (daily) right now.
I was thinking about having a composite partitioned table which cannot be SELECT-ed to avoid mixing different type of data in a single select query, but creating views on the top of it. In this case the table should be partitioned like this: partition by list FIELD subpartition by range interval. But oracle 12 does not support this.
Any idea how to solve this? (There is a reason why I need a single interface and why I have to store data separately.)
Thank you in advance!
The INSERT ALL syntax can help easily route data to specific tables based on conditions:
create table interface1(a number, b number);
create table interface2(a number, b number);
insert all
when a <= 1 then
into interface1
else
into interface2
select '1' a, 2 b from dual;

Tsql table variable to Oracle PLSql

I am working on TSql to Plsql conversion.
There are several table variables declared and used in tsql, like
DECLARE #table_var table( id_ int ......)
WHILE ...
begin
insert into #table_var select ...
...
select * from #table_var..
I think in oracle global temporary table can be used, but the data will be maximum 10k rows.
How efficient global temporary tables in this case?
Is there any other way other than global temporary table and table types in Oracle to convert similar sql query?
Oracle version: oracle11g or oracle12c
Depending on your size of the data and task that you want to achieve-- you can use
Pl/sql record/table and bulk processing
hold data in to cursor and loop through it
if you want kind of query update -- you can use in-line view
(preserving primary key)

TYPE v_id IS table of table#dblink.column%type;

I have table 'test' in schema A and from schema B I want to run the test query.
so created DBlink to A.test and dblink name 'dbl_test' .
now, I am able to query as
select * from test#dbl_test;
but when I try to use TYPE v_id IS TABLE OF test#dbl_test.id%TYPE; in plsql procedure block with cursor, it is giving errors as must declare and dbl_test is another database and not able to access.
When I query the select statement it is working why not for TYPE?
Put the column before the #:
TYPE v_id IS TABLE OF test.id#dbl_test%TYPE;
Database links are used to link two databases, not schemas.
In your case it is unclear if you really need a database link. Are your two schemas in the same database? If they are, you need only give the relevant rights to schema B and he will be able to reference schema A's datatype. I Think GRANT SELECT ON A.TEST TO B should be sufficient to reference its datatype.
Edit: it is actually possible to reference a remote datatype (I didn't know!) See #jonearles' answer.

OOP concepts in oracle 11.g

i have a question related to Object Orienting programming in oracle database(11.g),generally i have studyed how to create object types or how to create table from existing object,but i am stoped on one topic,namely,suppose that we have created following type
create or replace type mono.Item_type as object(
item_id integer,
part REF mono.Part_type,
quantity integer);
where Part_type is already existed object type.
i know that i can create table from this object like this:
create table tablename of Item_type,for instance
create table item_list of Item_type
what what would be different,if instead of this,we use
create or replace type mono.Item_List as table of Item_type;
here inspite of this,that we use type keyword,we are creating table again and what is different between create table tablename of Item_type and
create or replace type mono.Item_List as table of Item_type;?
When you use create table tablename of Item_type, you're creating a table in the DB.
This table is a structure that can be used to store user's data .
When you use create or replace type mono.Item_List as table you're defining a new Datatype (similar to array).
This Datatype can be used as a type of a column in a DB table or as a type of a variable in plsql code.

Insert into oracle database

Hi I have a database with loads of columns and I want to insert couple of records for testing, now in order to insert something into that database I'd have to write large query .. is it possible to do something like this
INSERT INTO table (SELECT FROM table WHERE id='5') .. I try to insert the row with ID 5 but I think this will create a problem because it will try to duplicate a record, is it possible to change this ID 5 to let say 1000 then I'd be able to insert data without writing complex query and while doing so avoiding replication of data .. tnx
In PL/SQL you can do something like this:
declare
l_rec table%rowtype;
begin
select * into l_rec from table where id='5';
l_rec.id := 1000;
insert into table values l_rec;
end;
If you have a trigger on the table to handle the primary key from a sequence (:NEW.id = seq_sequence.NEXTVAL) then you should be able to do:
INSERT INTO table
(SELECT columns_needed FROM table WHERE whatever)
This will allow you to add in many rows at one (the number being limited by the WHERE clause). You'll need to select the columns that are required by the table to be not null or not having default values. Beware of any unique constraints as well.
Otherwise you'll be looking at PL/SQL or some other form of script to insert multiple rows.
For each column that has no default value or you want to insert the values other than default, you will need to provide the explicit name and value.
You only can use an implicit list (*) if you want to select all columns and insert them as they are.
Since you are changing the PRIMARY KEY, you need to enumerate.
However, you can create a before update trigger and change the value of the PRIMARY KEY in this trigger.
Note that the trigger cannot reference the table itself, so you will need to provide some other way to get the unique number (like a sequence):
CREATE TRIGGER trg_mytable_bi BEFORE INSERT ON mytable FOR EACH ROW
BEGIN
:NEW.id := s_mytable.nextval;
END;
This way you can use the asterisk but it will always replace the value of the PRIMARY KEY.

Resources