select values from table and create oracle objects - oracle

i want to insert values to oracle Object type by selecting values from other table
And the tables and insert statement looks like this.
CREATE TYPE Test_obj AS OBJECT (
attr1 VARCHAR2(20),
attr2 VARCHAR2(20),
attr3 VARCHAR2(25) );
/
CREATE TABLE resultrow_obj (
resultrow Test_obj ,
RESULTTABLEID NUMBER(20,0),
ROWNUMBER NUMBER(20,0) );
/
INSERT INTO resultrow_obj VALUES (
Test_obj (select col1,col2,col3 from Table2 where rownum<=1),
1,123 );
/

You've got it nearly right:
SQL> INSERT INTO resultrow_obj
2 VALUES((SELECT Test_obj('A', 'B', 'C')
3 FROM dual WHERE rownum <= 1),
4 1, 123);
1 row inserted

Related

Oracle View Object Type Column

Object type
create or replace TYPE "TYPE_FE_FEE_DETAIL" AS OBJECT
(
FE_AMOUNT VARCHAR2(25),
CURR_ID VARCHAR2(5),
PROFILE_TYPE VARCHAR2(1),
ISO_CODE VARCHAR2(25)
);
create or replace TYPE "TYPE_TB_FE_FEE_DETAIL" AS TABLE OF type_fe_fee_detail;
We have a view with two columns where the second column is of OBJECT TYPE and has data like
EPAYPROD_M3.TYPE_TB_FE_FEE_DETAIL(EPAYPROD_M3.TYPE_FE_FEE_DETAIL('10', '1', '1', '818'))
I need to insert these four values into a table which has four separate columns. I am having in trouble doing so.
TYPE_TB_FE_FEE_DETAIL is a nested table. To get the attribute values, unnest it.
You can do this with the table operator:
create or replace TYPE "TYPE_FE_FEE_DETAIL" AS OBJECT
(
FE_AMOUNT VARCHAR2(25),
CURR_ID VARCHAR2(5),
PROFILE_TYPE VARCHAR2(1),
ISO_CODE VARCHAR2(25)
);
/
create or replace TYPE "TYPE_TB_FE_FEE_DETAIL" AS TABLE OF type_fe_fee_detail;
/
with rws as (
select type_tb_fe_fee_detail(type_fe_fee_detail('10', '1', '1', '818')) obj
from dual
)
select t.*
from rws r, table ( r.obj ) t;
FE_AMOUNT CURR_ID PROFILE_TYPE ISO_CODE
10 1 1 818

insert all and inner join in oracle

I would like to insert data in to two tables. Will be one-to-many connection. For this, I have to use Foreign Key, of course.
I think, table1 - ID column is an ideal for this a Primary Key. But I generate it always with a trigger, automatically, every line. SO,
How can I put Table1.ID (auto generated, Primary Key) column in to table2.Fkey column in the same insert query?
INSERT ALL INTO table1 ( --here (before this) generated the table1.id column automatically with a trigger.
table1.food,
table1.drink,
table1.shoe
) VALUES (
'apple',
'water',
'slippers'
)
INTO table2 (
fkey,
color
) VALUES (
table1.id, -- I would like table2.fkey == table1.id this gave me error
'blue'
) SELECT
*
FROM
table1
INNER JOIN table2 ON table1.id = table2.fkey;
The error message:
"00904. 00000 - "%s: invalid identifier""
As suggested by #OldProgrammer, use sequence
INSERT ALL INTO table1 ( --here (before this) generated the table1.id column automatically with a trigger.
table1_id,
table1.food,
table1.drink,
table1.shoe
) VALUES (
<sequecename_table1>.nextval,
'apple',
'water',
'slippers'
)
INTO table2 (
fkey,
color
) VALUES (
<sequecename_table2>.nextval,
<sequecename_table1>.currval, -- returns the current value of a sequence.
'blue'
) SELECT
*
FROM
table1
INNER JOIN table2 ON table1.id = table2.fkey;
Since you're using Oracle DB's 12c version, then might use Identity Column Property. Then easily return the value of first table's (table1) to a local variable by charging of returning clause just after an insert statement for table1, and use inside the next insert statement which is for table2 as stated below :
SQL> create table table1(
2 ID integer generated always as identity primary key,
3 food varchar2(50), drink varchar2(50), shoe varchar2(50)
4 );
SQL> create table table2(
2 fkey integer references table1(ID),
3 color varchar2(50)
4 );
SQL> declare
2 cl_tab table1.id%type;
3 begin
4 insert into table1(food,drink,shoe) values('apple','water','slippers' )
5 returning id into cl_tab;
6 insert into table2 values(cl_tab,'blue');
7 end;
8 /
SQL> select * from table1;
ID FOOD DRINK SHOE
-- ------- ------- -------
1 apple water slippers
SQL> select * from table2;
FKEY COLOR
---- --------------------------------------------------
1 blue
Anytime you issue the above statement for insertions between begin and end, both table1.ID and table2.fkey columns will be populated by the same integer values. By the way do not forget to commit the changes by insertions, if you need these values throughout the DB(i.e.from other sessions also).

Oracle Pivoting returning Null values for Month/Year

I'm trying to pivoting this table by month/year according to date (field DT):
CREATE TABLE "TEMP"
( "NAME" VARCHAR2(20 BYTE),
"DT" DATE,
"SZ" NUMBER(10,0)
)
Insert into TEMP (NAME,DT,SZ) values ('A',to_date('16/01/15','DD/MM/RR'),'1');
Insert into TEMP (NAME,DT,SZ) values ('B',to_date('16/01/15','DD/MM/RR'),'2');
Insert into TEMP (NAME,DT,SZ) values ('C',to_date('16/01/15','DD/MM/RR'),'3');
Insert into TEMP (NAME,DT,SZ) values ('D',to_date('16/01/15','DD/MM/RR'),'4');
Insert into TEMP (NAME,DT,SZ) values ('A',to_date('10/01/15','DD/MM/RR'),'5');
Insert into TEMP (NAME,DT,SZ) values ('B',to_date('10/01/15','DD/MM/RR'),'6');
Insert into TEMP (NAME,DT,SZ) values ('C',to_date('10/01/15','DD/MM/RR'),'7');
using this query:
SELECT * from
(select name, sz, dt from temp)
pivot (sum(sz) for dt in (to_date('01/2015', 'MM/YYYY') as "dt"))
and I receive this:
NAME dt
-------------------- ----------
D
A
B
C
However, I was excepting to get:
NAME dt
-------------------- ----------
D 4
A 6
B 8
C 10
I've tried many things but none of them seems to work as:
...
pivot (sum(sz) for dt in ('01/2015' as "dt")) --01843. 00000 - "not a valid month"
Do you have a clue of what I'm doing wrong?
Any help is very welcome.
You need to use TO_CHAR and not TO_DATE
WITH pivotdata AS
(
SELECT name,
sz,
To_char(dt, 'MM/YYYY') dt1
FROM temp )
SELECT *
FROM pivotdata pivot (SUM(sz) FOR dt1 IN ('01/2015') );
This will return
NAME '01/2015'
D 4
A 6
B 8
C 10

Comparision in the tree in hierarchical queries

I have a requirement to pull the data in the below fashion
Compare_Result Association_Type_name Association_Role
Type in Dev,but not Prod ProcessingSite Null
Role in Dev,but not Prod ProcessingSite MoodedActivity
Role in Dev,but not Prod Specimen ProductRelationship
Requirement: I need to compare the development and production. if any association_type or association_role are not exists in production then i need to return a message like "the Type Object found in Development,but not Production"
Could any one please give me an idea.
sample data
create table nodes
(
node_id number(5),
parent_node_id number(5),
object_id number(5)
);
begin
insert into nodes values(4001,7001,2001);
insert into nodes values(4002,4001,2005);
insert into nodes values(4003,4002,2002);
insert into nodes values(4004,4003,2004);
insert into nodes values(4005,4004,2003);
insert into nodes values(4006,4004,2006);
insert into nodes values(4007,4004,2007);
insert into nodes values(4008,4003,2008);
insert into nodes values(4009,4008,2011);
insert into nodes values(4010,4008,2013);
insert into nodes values(4011,4008,2014);
insert into nodes values(4012,4003,2009);
insert into nodes values(4013,4012,2015);
insert into nodes values(4014,4012,2017);
insert into nodes values(4015,4001,2020);
insert into nodes values(4016,4015,2012);
insert into nodes values(4017,4016,2016);
insert into nodes values(4018,4017,2024);
insert into nodes values(4019,4017,2023);
insert into nodes values(4020,4016,2029);
insert into nodes values(4021,4020,2022);
insert into nodes values(4022,4020,2021);
insert into nodes values(4023,4020,2019);
insert into nodes values(4024,4016,2010);
insert into nodes values(4025,4024,2018);
end;
/
create table objects
(
object_id number(5),
object_type_id number(5),
name varchar2(40)
);
begin
insert into objects values ( 2001,5001,'iad_Dictionary_area');
insert into objects values ( 2002,5003,'iad_Association_Dictionary');
insert into objects values ( 2003,5005,'TreatingSite');
insert into objects values ( 2004,5004,'ProcessingSite');
insert into objects values ( 2005,5002,'Development');
insert into objects values ( 2006,5005,'MoodedActivity');
insert into objects values ( 2007,5005,'MaterialName');
insert into objects values ( 2008,5004,'PerformedClass');
insert into objects values ( 2009,5004,'Specimen');
insert into objects values ( 2010,5004,'Specimen');
insert into objects values ( 2011,5005,'RegulatoryAssessment');
insert into objects values ( 2012,5003,'iad_Association_Dictionary');
insert into objects values ( 2013,5005,'Submission');
insert into objects values ( 2014,5005,'Mooded');
insert into objects values ( 2015,5005,'class13th');
insert into objects values ( 2016,5004,'ProcessingSite');
insert into objects values ( 2017,5005,'ProductRelationship');
insert into objects values ( 2018,5005,'class13th');
insert into objects values ( 2019,5005,'Distributor');
insert into objects values ( 2020,5006,'Production');
insert into objects values ( 2021,5005,'Assessor');
insert into objects values ( 2022,5005,'StudyInvestigator');
insert into objects values ( 2023,5005,'MaterialName');
insert into objects values ( 2024,5005,'TreatingSite');
insert into objects values ( 2029,5004,'BiologicEntityGroup');
end;
/
create table object_types
(
object_type_id number(5),
name varchar2(35)
);
begin
insert into object_types values (5001,'Dictionary_area');
insert into object_types values (5002,'Development');
insert into object_types values (5003,'Association_Dictionary');
insert into object_types values (5004,'Association_Type');
insert into object_types values (5005,'Association_Role');
insert into object_types values (5006,'Production');
end;
/
example
let us assume i have below structure
Dictionary_area
development
Association_Dictionary
ProcessingSite(type1)
TreatingSite(role1)
MoodedActivity(role2)
MaterialName(role3)
PerformedClass(type2)
RegulatoryAssessment(role1)
Submission(role2)
Mooded(role3)
Specimen(type3)
class13th(role1)
ProductRelationship(role2)
production
Association_Dictionary
ProcessingSite(type1)
TreatingSite(role1)
MaterialName(role2)
BiologicEntityGroup(type2)
StudyInvestigator(role1)
Assessor(role2)
Distributor(role3)
Specimen (type3)
class13th(role1)
ProcessingSite is an association_type and it is having 3 association_roles. we can observe same association_type in the production, but MoodedActivity(association_role) is not available. in this case we need to return "Type Object found in Development,but not Production".
Tables information
nodes: this table will be having nodes and parent nodes information. for example if development object is having some node like 100 and its parent node is 99.
objects: objects information.
object_types: object_type information. for example ProcessingSite object_type is "Association_Type".
i tried in the below fashion
WITH childs AS
(SELECT n.object_id, n.node_id, n.parent_node_id, ot.NAME, "path",
rootnode
FROM (SELECT object_id, node_id, parent_node_id,
SYS_CONNECT_BY_PATH (object_id, '/') "path",
CONNECT_BY_ROOT (object_id) rootnode
FROM nodes
START WITH object_id = 2001
CONNECT BY PRIOR node_id = parent_node_id) n
JOIN
objects o ON n.object_id = o.object_id
JOIN object_types ot ON o.object_type_id = ot.object_type_id
),
devobj AS
(SELECT n.object_id, n.NAME
FROM (SELECT *
FROM childs) n
WHERE n.NAME = 'Development'),
prodobj AS
(SELECT n.object_id, n.NAME
FROM (SELECT *
FROM childs) n
WHERE n.NAME = 'Production'),
dev_associ_type_obj AS
(SELECT *
FROM (SELECT object_id, NAME
FROM (SELECT *
FROM childs)
START WITH object_id = (SELECT object_id
FROM devobj)
CONNECT BY PRIOR node_id = parent_node_id) t
WHERE t.NAME = 'Association_Type'),
dev_associ_roles_obj AS
(SELECT object_id, NAME, CONNECT_BY_ROOT (object_id) rootnode
FROM (SELECT *
FROM childs)
START WITH object_id IN (SELECT object_id
FROM dev_associ_type_obj)
CONNECT BY PRIOR node_id = parent_node_id),
prod_associ_type_obj AS
(SELECT *
FROM (SELECT object_id, NAME
FROM (SELECT *
FROM childs)
START WITH object_id = (SELECT object_id
FROM devobj)
CONNECT BY PRIOR node_id = parent_node_id) t
WHERE t.NAME = 'Association_Type'),
prod_associ_roles_obj AS
(SELECT object_id, NAME, CONNECT_BY_ROOT (object_id) rootnode
FROM (SELECT *
FROM childs)
START WITH object_id IN (SELECT object_id
FROM prod_associ_type_obj)
CONNECT BY PRIOR node_id = parent_node_id)
SELECT *
FROM prod_associ_roles_obj
MINUS
SELECT *
FROM dev_associ_roles_obj

How do i turn this tables into an object-oriented table (object-relation) in Oracle

I have this table below in the picture that i want to create as an object-oriented table. I dont want the usual create table with relationships.... I just want to learn how to turn this table into an object-oriented table. Below is a picture of my tables and how are they connected:
CREATE TYPE A_TYPE AS OBJECT(
id INT,
col1 INT
);
/
CREATE TYPE A_REF_TABLE_TYPE AS TABLE OF REF A_TYPE;
/
CREATE TYPE B_TYPE AS OBJECT(
id INT,
col1 INT
);
/
CREATE TYPE B_REF_TABLE_TYPE AS TABLE OF REF B_TYPE;
/
CREATE TYPE C_TYPE AS OBJECT(
id INT,
a_list A_REF_TABLE_TYPE,
b_list B_REF_TABLE_TYPE,
col1 INT
);
/
CREATE TABLE A_TAB OF A_TYPE(
ID PRIMARY KEY
);
CREATE TABLE B_TAB OF B_TYPE(
ID PRIMARY KEY
);
CREATE TABLE C_TAB OF C_TYPE(
ID PRIMARY KEY
)
NESTED TABLE a_list STORE AS c_a_lists
NESTED TABLE b_list STORE AS c_b_lists;
INSERT INTO A_TAB VALUES( A_TYPE( 1, 3 ) );
INSERT INTO A_TAB VALUES( 2, 4 );
INSERT INTO B_TAB VALUES ( B_TYPE( 1, 7 ) );
INSERT INTO B_TAB VALUES ( 2, 2 );
INSERT INTO B_TAB VALUES ( 3, 10 );
INSERT INTO C_TAB VALUES (
1,
A_REF_TABLE_TYPE(
( SELECT REF(a) FROM A_TAB a WHERE ID = 2 ) -- Single value
),
( -- Multiple values
SELECT CAST( COLLECT( REF(b) ) AS B_REF_TABLE_TYPE )
FROM TAB_B b
WHERE ID IN ( 1, 3 )
),
42
);
INSERT INTO C_TAB VALUES (
2,
NULL, -- Unknown
B_REF_TABLE_TYPE(), -- No values
54
);
Output:
SELECT * FROM C_TAB;
ID A_LIST B_LIST COL1
-- ------------------------------- --------------------------------------------- ----
1 A_REF_TABLE_TYPE( A_TYPE(2,4) ) B_REF_TABLE_TYPE( B_TYPE(1,7), B_TYPE(3,10) ) 42
2 (null) B_REF_TABLE_TYPE() 54

Resources