PL/SQL: nested table with id's and create view - oracle

i've created tables, linked through the nested table inside one of them:
CREATE TABLE A (
NAME VARCHAR(150) ,
ID INTEGER PRIMARY KEY
) ;
CREATE TYPE A_LIST IS TABLE OF integer;
CREATE TABLE B (
NAME VARCHAR(150),
ID INTEGER PRIMARY KEY,
LIST A_LIST
) NESTED TABLE LIST STORE AS LIST_TABLE;
now I need to CREATE VIEW to show data from table B with nested data from A. Its something like
CREATE OR REPLACE VIEW ff OF B, A AS
SELECT N.name, N.LIST
CAST( MULTISET (
....
)AS TYPE_FILM_LIST)
FROM B N;
but i'm stuck in that dots(

If you want to join the tables B and A based on nested ID's - here an example.
Some test data
insert into b
values ('x',1, A_LIST(1,2,3));
insert into a values ('A1',1);
insert into a values ('A2',2);
insert into a values ('A3',3);
The main step is the join of the table B with the nested table from b, table(b.list) The nested ID is returned as COLUMN_VALUE ; rest is simple join
select b.id, b.name, a.name a_name, a.id a_id from b, table(b.list) ba, a
where ba.column_value = a.id;
result
ID SUBSTR(B.NAME,1,5) A_NAME A_ID
---------- ------------------ ------ ----------
1 x A1 1
1 x A2 2
1 x A3 3

create type record_for_a is object( a varchar2(150), id number);
create type l_record_for_a is table of record_for_a;
select table_b.name, table_b.id,
cast( multiset (select record_for_a(aa.name,aa.id) from a aa, table(table_b.list) bb where aa.id = bb.column_value) as l_record_for_a)
neste_col_a
from
b table_b;

Related

In Oracle SQL Developer, I am unable to create table due to duplicate columns into same table

I am unable to create table due to duplicate columns into same table.
There is duplicate column name called load_Tstamp. How to rename the column or any other alternative?
Create table New_County_code as (
Select a.*,b.LOAD_TSTMP as Load_time
from (
Select CONCAT( b.STATE_FIPS_CODE,a.ZIP_CNTY_CDE) AS "Final_County_Code",
a.*,
b.*
from mdmstggeo.T_USPS_DETAIL_RECORD#ODSDEV a
left join (
select *
from DSSCORP.T_STATE#ds31
where STATE_FIPS_CODE is not null) b
on ST_CPTL_BLDG_ZIP_CDE = DTL_ZIP_CDE
WHERE ds_batch_gid=1661
order by STATE_FIPS_CODE
)
)
Every column name in your target table must be unique, so use column aliases wherever needed to ensure that
SQL> create table t1 as
2 select a.*, b.*
3 from scott.dept a,
4 scott.dept b;
select a.*, b.*
*
ERROR at line 2:
ORA-00957: duplicate column name
SQL>
SQL> create table t1 as
2 select a.*, b.deptno c1, b.dname c2, b.loc c3
3 from scott.dept a,
4 scott.dept b;
Table created.
SQL>
SQL> desc t1
Name Null? Type
----------------------------------------------------------------------- -------- --------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
C1 NUMBER(2)
C2 VARCHAR2(14)
C3 VARCHAR2(13)

Oracle - Replace null values for pivot columns with join from another table

I've a pivot table output and now I want to check for the values from the pivot columns and replace the values if null from another column from another table.
Invoice_No
Column
value
111
A
One
111
B
Two
111
C
Three
111
E
Five
(SELECT Invoice_No, new_value, Column_Name FROM table_name)
PIVOT(max(new_value)
FOR Column_Name IN ('A','B','C','D','E'))
This returned the following table
Invoice_No
'A'
'B'
'C'
'D'
'E'
111
One
Two
Three
null
Five
Now, I want to replace the null value from column D with a value from another table that matches the Invoice_no.
with temp as
(SELECT Invoice_No, new_value, Column_Name FROM table_name)
PIVOT(max(new_value)
FOR Column_Name IN ('A','B','C','D','E'))
select nvl(temp.D,bckup.D)
from
(select A,B,C,D,E from Backup_table) bckup
join
temp
on
temp.Invoice_No = bckup = Invoice_No
Now, I'm getting the error saying D Column does not exist.
Pivot will rename your column as 'D' not D only. So, You need a simple update in your query as -
WITH temp AS(SELECT *
FROM(SELECT Invoice_No, new_value, Column_Name
FROM table_name)
PIVOT(max(new_value) FOR Column_Name IN ('A' AS A,'B' AS B,'C' AS C,'D' AS D,'E' AS E)
)
SELECT NVL(temp.D,bckup.D)
FROM(SELECT A,B,C,D,E
FROM Backup_table) bckup
JOIN temp ON temp.Invoice_No = bckup.Invoice_No

query to get all tables in a materialized view

Good afternoon friends,
a query is there any way (select * from) to visualize which tables form a materialized view?
ex:
CREATE MATERIALIZED VIEW table_vm
REFRESH COMPLETE ON COMMIT
as
SELECT * FROM table1;
UNION ALL
SELECT * FROM table2;
I would like to output something like this:
view name | table name
table_m | Table 1
table_m | table 2
tabla_m | table 3
....
....
Thank you so much,
I would appreciate any information.
You can use the view DBA_DEPENDENCIES to view any dependencies for an object compiled into the database. Querying that view with the name of your materialized view should list all of the tables as well as any other dependencies the materialized view relies on. REFERENCED_OWNER and REFERENCED_NAME will hold the values of the tables being used by the materialized view.
SELECT *
FROM dba_dependencies
WHERE owner = 'OWNER_OF_MV' AND name = 'TABLE_MV';
One option would be
SQL> create table t1 ( c1 number, c2 varchar2(1) ) ;
Table created.
SQL> create table t2 ( c1 number , c3 varchar2(1) ) ;
Table created.
SQL> create table t3 ( c1 number , c3 varchar2(1) ) ;
Table created.
SQL> create materialized view mv1 refresh complete on demand as
2 select a.c1 , b.c3 as c2, c.c3
3 from t1 a inner join t2 b on a.c1 = b.c1
4 left join t3 c on a.c1 = c.c1 ;
Materialized view created.
SQL> select name as mv, listagg(referenced_name || ' - ' || referenced_type , '|' )
within group ( order by referenced_name ) as list_dep
from dba_dependencies where name='MV1' and name != referenced_name
group by name
MV LIST_DEP
------------------------------ --------------------------------------------------
MV1 T1 - TABLE|T2 - TABLE|T3 - TABLE

Inserting selected data from table in one schema into another table in a different schema

I have a table A in Schema1 and table B in Schema2.
The tables have different columns.
Table A:
ID1 Name Code
-------------------------------
1 Skyler A0
2 Amanda A1
3 Rachel B0
4 Harvey C0
5 Louis B1
Table B:
ID Names Enterprise Modified_Date
------------------------------------------------------
1 Amanda 1 2018.08.10
2 Skyler 1 2018.08.11
As depicted, Schema1.A.Name = Schema2.B.Names
I want to insert the values "Rachel,Harvey and Louis" from A.Name into B.Names.
For b.ID, i have a sequence in place. Enterprise column is always 1 and modified date can e sysdate.
How can i achieve this in PL/SQL?
use insert Statement with select statement
insert into tabB (names,Enterprise,Modified_Date )
select Name,1,sysdate from tabA where Name in ('Rachel','Harvey','Louis');
You can do this by using below query.
insert into tableB (names,Enterprise,Modified_Date )
select Name,1,sysdate from tableA where Name not in (select distinct(Name) from tableB);

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).

Resources