Comparing Multiple Values from one table to Multiple Values in another - plsql

I have a PL/SQL stored function script I am writing and I have run into a problem.
I need to find ALL Applicants who possess ALL the skills needed for a task and display them.
I have a method where I have individually read in Applicant's skills into a VARCHAR2 string and all the Skills required into another. If I could separate these strings into individual words I could compare the two with LIKE '% <data> %'.
How would I go about this (or what is an alternative method)?
CREATE OR REPLACE FUNCTION FUBARR(num IN NUMBER) RETURN VARCHAR IS
string_position VARCHAR2(128);
string_applicant VARCHAR2(128);
string_results VARCHAR2(128);
BEGIN
string_position := '';
string_applicant := '';
FOR SKILLS_row IN (SELECT sname FROM SNEEDED WHERE pnumber = num)
LOOP
string_position := string_position || SKILLS_row.sname || ' ';
END LOOP;
FOR EVERYBODY_row IN (SELECT UNIQUE anumber FROM SPOSSESSED ORDER BY anumber)
LOOP
FOR APPLICANTS_row IN (SELECT sname FROM SPOSSESSED WHERE SPOSSESSED.anumber = EVERYBODY_row.anumber)
LOOP
string_applicant := string_applicant || APPLICANTS_row.sname || ' ';
END LOOP;
--DBMS_OUTPUT.PUT_LINE(EVERYBODY_row.anumber || ' ' || string_applicant);
--IF blaah != LIKE BLAh
IF
string_applicant := '';
END LOOP;
--DBMS_OUTPUT.PUT_LINE(string_position);
--RETURN (string_position);
RETURN('help');
END FUBARR;
/

why not simply select all spossesed - records, where the number of associated skills of the desired num is equal to the plain number of sneeded - skills of that num:
SELECT *
FROM SPOSSESSED sp
WHERE (SELECT COUNT(*) FROM SNEEDED s
WHERE s.pnumber = num) =
(SELECT COUNT(*) FROM SNEEDED s
JOIN SPOSSESSED p ON p.sname = s.sname
WHERE s.pnumber = num and p.anumber = sp.anumber)
or using the ANY construct:
SELECT sp.anumber, COUNT(*)
FROM SPOSSESSED sp
WHERE sp.sname = ANY (SELECT s.sname FROM SNEEDED s WHERE s.pnumber = num)
GROUP BY sp.anumber

Related

SQL Loop Executing after intended

I am attempting to run this code, I know the elements of the Loop are working, and I know the update statement does update. When I run the script with some print statements, it prints the UPDATE information first, then the LOOP information, therefore the update has no information.
PROCEDURE UpdateGridStats(p_grid_name VARCHAR2, p_region_name VARCHAR2) IS
CURSOR c1 IS
SELECT grd.globalid grid_globalid, wp.globalid workpoint_globalid, wp.feature_class_name workpoint_fcname,
tt.work_order_task_type task_type_name
FROM workorderpoint_evw wp, rpt_grid grd, workordertasktype_evw tt
WHERE grd.grid_name = p_grid_name
AND wp.work_order_task_type_globalid = tt.globalid
AND grd.rpt_region = p_region_name
AND sde.st_relation_operators.st_within_f(wp.shape, grd.shape) = 1;
v_count NUMBER := 0;
v_pole_insp_count NUMBER := 0;
v_pole_damage_count NUMBER := 0;
v_cond_damage_count NUMBER := 0;
BEGIN
FOR work_rec IN c1
LOOP
BEGIN
v_count := v_count + 1;
IF work_rec.task_type_name = 'Pole Inspection'
THEN
v_pole_insp_count := v_pole_insp_count + 1;
END IF;
IF work_rec.task_type_name = 'Pole Damage'
THEN
v_pole_damage_count := v_pole_damage_count + 1;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('==> No data found for work record ');
END;
END LOOP;
dbms_output.put_line(v_pole_damage_count || ',' ||v_pole_insp_count);
UPDATE rpt_grid grd SET da_pole_count = v_pole_damage_count, ins_structure_count = v_pole_insp_count
WHERE grd.grid_name = p_grid_name
AND grd.rpt_region = p_region_name;
END UpdateGridStats;
Maybe you just forgot to commit your changes?
Here is a simpler version of your stored procedure.
CREATE OR REPLACE PROCEDURE update_grid_stats(p_grid_name VARCHAR2, p_region_name VARCHAR2)
IS
v_ins_structure_count rpt_grid.ins_structure_count%TYPE;
v_da_pole_count rpt_grid.da_pole_count%TYPE;
BEGIN
UPDATE rpt_grid grd
SET (ins_structure_count, da_pole_count) =
(
SELECT
COUNT(CASE WHEN tt.work_order_task_type = 'Pole Inspection' THEN 1 END),
COUNT(CASE WHEN tt.work_order_task_type = 'Pole Damage' THEN 1 END)
FROM workorderpoint_evw wp
JOIN workordertasktype_evw tt ON wp.work_order_task_type_globalid = tt.globalid
WHERE sde.st_relation_operators.st_within_f(wp.shape, grd.shape) = 1
)
WHERE grd.grid_name = p_grid_name
AND grd.rpt_region = p_region_name
RETURNING ins_structure_count, da_pole_count
INTO v_ins_structure_count, v_da_pole_count;
dbms_output.put_line(
SQL%ROWCOUNT || ' rows got updated. Values: ' ||
'ins_structure_count = ' || v_ins_structure_count ||
', da_pole_count = ' || v_da_pole_count
);
COMMIT;
END update_grid_stats;
The variables and the RETURNING clause are merely needed for the output. If you don't need the output, you can remove them.
if I understand correcatally, the proc works OK, but the print statemnts are not. I beleve the print operations are on a different thread. To overcome this issue, try to replace your pront statment with raiserror function :
declare #mag varchar(max) = 'Hllo world'
raiserror ( #ERR_MSG ,0, 1) with nowait;

CREATE OR REPLACE PROCEDURE proc_video_search

Create a procedure called proc_video_search to search for a video and display the name, copy ID, format, and status of the video’s copies. In addition, the checkout dates and due dates are also displayed for unreturned copies. The damaged copies (Status = 'D') are excluded in your output. Sort your output by the video name (Name) and then the copy ID (CopyID).
$ CREATE OR REPLACE PROCEDURE proc_video_search (
p_VideoName VARCHAR2,
p_FormatName VARCHAR2 DEFAULT NULL) as
v_Count NUMBER;
v_TotalCopies NUMBER; v_Avalb NUMBER;v_FormatName VARCHAR2(100);
v_VideoName VARCHAR2(100); v_CopyID VARCHAR2(100);v_DueDate DATE;
v_Status VARCHAR2(100); v_CheckoutDate DATE;
CURSOR asdf IS
SELECT T_VIDEO.Name, T_COPY.CopyID, Status,T_VIDEO_FORMAT.NAME
FROM T_VIDEO
INNER JOIN T_COPY ON T_VIDEO.VideoID = T_COPY.VideoID
INNER JOIN T_VIDEO_FORMAT ON T_VIDEO_FORMAT.FormatID =
T_VIDEO.FormatID
WHERE Status !='D' AND UPPER(T_VIDEO.Name) like '%' ||
UPPER(p_VideoName) || '%'
OR UPPER(T_VIDEO_FORMAT.NAME)= UPPER(p_FormatName)
ORDER BY T_VIDEO.Name, T_COPY.CopyID;
BEGIN
SELECT COUNT(*)
INTO v_Count
FROM T_VIDEO
WHERE UPPER(T_VIDEO.Name) like '%' || UPPER(p_VideoName) || '%' ;
IF v_count = 0 THEN
DBMS_OUTPUT.PUT_LINE('**** '||v_Count|| ' results found for ' ||
p_VideoName||'. *****');
RETURN;
END IF;
SELECT count(T_COPY.CopyID) INTO v_TotalCopies
FROM T_COPY INNER JOIN T_VIDEO ON T_COPY.VideoID = T_VIDEO.VideoID
INNER JOIN T_VIDEO_FORMA ON T_VIDEO_FORMAT.FormatID =
T_VIDEO.FormatID
WHERE Status !='D' AND UPPER(T_VIDEO.Name) like '%' ||
UPPER(p_VideoName) ||'%'
OR UPPER(T_VIDEO_FORMAT.NAME)=UPPER(p_FormatName);
SELECT count(T_COPY.CopyID)INTO v_Avalb FROM T_COPY
INNER JOIN T_VIDEO ON T_COPY.VideoID = T_VIDEO.VideoID
INNER JOIN T_VIDEO_FORMAT ON T_VIDEO_FORMAT.FormatID =
T_VIDEO.FormatID
WHERE Status ='A' AND UPPER(T_VIDEO.Name) like '%' ||
UPPER(p_VideoName) ||'%'
OR UPPER(T_VIDEO_FORMAT.NAME)=UPPER(p_FormatName);
IF v_TotalCopies >=0 THEN
IF p_FormatName IS NULL THEN
DBMS_OUTPUT.PUT_LINE(v_TotalCopies||' results found for '||
p_VideoName||' . (Available copies:'|| v_Avalb|| ')' );
ELSE
DBMS_OUTPUT.PUT_LINE(v_TotalCopies||' results found for '||
p_VideoName||'('|| p_FormatName||') . (Available copies:'||
v_Avalb|| ')' );
end if;
OPEN asdf;
LOOP
FETCH asdf INTO v_VideoName, v_CopyID, v_Status,
v_FormatName ; exit when asdf%NOTFOUND ;
SELECT COUNT(CheckoutDate)
INTO v_Count FROM T_RENTAL WHERE CopyID = v_CopyID;
IF v_Count = 1 THEN
SELECT CheckoutDate,DueDate
INTO v_CheckoutDate,v_DueDate
FROM T_RENTAL
WHERE CopyID = v_CopyID;
end if;
DBMS_OUTPUT.PUT_LINE(RPAD('-', 53, '-'));
DBMS_OUTPUT.PUT_LINE(RPAD('Name:',30) || RPAD(v_VideoName,15));
DBMS_OUTPUT.PUT_LINE(RPAD('CopyID:',30) || RPAD(v_CopyID,15));
DBMS_OUTPUT.PUT_LINE(RPAD('Format:',30) ||
RPAD(v_FormatName,15));
IF v_Status = 'A' THEN v_Status := 'Available';END IF;
IF v_Status = 'R' THEN v_Status := 'Rented'; END IF;
DBMS_OUTPUT.PUT_LINE(RPAD('Status:',30) || RPAD(v_Status,15));
IF v_Status ='Available' THEN
DBMS_OUTPUT.PUT_LINE(RPAD('CheckoutDate:',30)
||'****************************');
DBMS_OUTPUT.PUT_LINE(RPAD('DueDate:',30)
||'****************************');
ELSE
DBMS_OUTPUT.PUT_LINE(RPAD('CheckoutDate:',30)
||RPAD(TO_CHAR(v_CheckoutDate, 'DD-MON-YYYY'),15));
DBMS_OUTPUT.PUT_LINE(RPAD('DueDate:',30) ||RPAD(TO_CHAR(
v_DueDate, 'DD-MON-YYYY'),15));
END IF; END LOOP; CLOSE asdf; END IF; END proc_video_search ;
EXEC proc_video_search('ANOTHER', 'DVD')`
enter image description here
The problem is with this line of your cursor asdf
OR UPPER(T_VIDEO_FORMAT.NAME)= UPPER(p_FormatName)
Because it says OR, the query can choose to ignore this criteria if it evaluates to false. That is why you are getting results with all the formats; it ignores the filtering.
You have to wrap the OR statement in another AND clause, like so
WHERE Status !='D'
AND UPPER(T_VIDEO.Name) like '%' || UPPER(p_VideoName) || '%'
AND ( /* OR clause here */ )
And then you can handle the case of p_formatName being null or not.

Need help solving Pl/sql issue

I am trying to fill a database with random data, but the primary key needs to still be unique. I can fill the database with random data, but now I am trying to solve the primary key issue.
When running this code I get errors.
set SERVEROUTPUT on
create or replace
PROCEDURE fillDatabase(TableIn IN varchar2, Amount IN NUMBER) IS
columnData varchar2(50);
columnNR number(10);
str varchar2(500);
sqlStatement varchar2(500);
l_ran_time TIMESTAMP;
intlol NUMBER(38);
prmname varchar2(50);
prmtab varchar2(50);
prmkey number(10);
temp number(30);
tempstr varchar2(50);
lolnr number(10);
strq varchar2(50);
BEGIN
lolnr := 1;
select count(*) into columnNR
from user_tab_columns where table_name=TableIn;
FOR counter IN 1..Amount
LOOP
sqlStatement := 'insert into '|| TableIn ||' values (';
FOR counter2 IN 1..columnNR
LOOP
SELECT cols.table_name, cols.column_name into prmtab, prmname
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = TableIn
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;
tempstr := 'select count(*) into temp from '|| prmtab;
dbms_output.put_line('test');
dbms_output.put_line(temp);
EXECUTE IMMEDIATE tempstr;
IF temp = 0
THEN
strq := 'SELECT max(' || prmname || ') into prmkey from '|| prmtab || ' order by '|| prmname;
dbms_output.put_line(strq);
EXECUTE IMMEDIATE strq;
END IF;
select dbms_random.value(0, 20) into intlol from dual;
select dbms_random.string('U', 20) into str from dual;
SELECT SYSDATE + dbms_random.value(0, SYSDATE - SYSDATE+1)
INTO l_ran_time
FROM dual;
Select DATA_TYPE INTO columnData
FROM user_tab_columns
WHERE table_name= TableIn
AND COLUMN_ID = counter2;
dbms_output.put_line(columnData);
CASE
WHEN columnData = 'VARCHAR2' THEN sqlStatement := sqlStatement ||''''|| str ||''', ';
WHEN columnData = 'NUMBER' THEN sqlStatement := sqlStatement || intlol ||', ';
WHEN columnData = 'TIMESTAMP(6)' THEN sqlStatement := sqlStatement ||''''|| l_ran_time ||''', ';
ELSE sqlStatement := sqlStatement || NULL || ', ';
END CASE;
END LOOP;
sqlStatement := SUBSTR(sqlStatement, 0, LENGTH(sqlStatement) -2);
sqlStatement := sqlStatement || ')';
dbms_output.put_line(sqlStatement);
EXECUTE IMMEDIATE sqlStatement;
END LOOP;
END fillDatabase;
Can you guys help me solve this?
use the pattern
str := 'select x from t where...';
execute immediate str into var;
instead of
str := 'select x into var from t where...';
execute immediate str;

How to return the insert statement to execute it by java code and not by stored procedure

I have one stored procedure which has 10 select statements and one insert statement. Insert statement inserts the data in a table which is set by 10 select statement. But I want this insert statement should not be executed in stored procedure itself but I want 10 select statements should send back these 10 selected values to java code and then in java code insert statements should be executed. Following is the stored procedure and Please provide me java code for this.
Java Code:
CallableStatement objStatement= null;
Connection objConnection = getConnection();
try { objStatement = objConnection.prepareCall("{call test_proc(?)}"); objStatement.setDate(1 , new java.sql.Date( sysdate1.getTime())); objStatement.executeUpdate();
} catch (SQLException e) { e.printStackTrace();
log.info("There is some problem in Data Generation : Exception"+e);
procedure:
CREATE OR REPLACE
PROCEDURE "test_proc"(
p_fromdate DATE)
AS
fromdate DATE;
todate DATE;
emp_id NUMBER := 0;
emp_address NUMBER := 0;
emp_dob NUMBER := 0;
emp_doj NUMBER := 0;
emp_msisdn NUMBER := 0;
emp_name NUMBER := 0;
emp_vehicl_number NUMBER := 0;
emp_vehicl_type NUMBER := 0;
emp_middle_name NUMBER := 0;
emp_last_name NUMBER := 0;
BEGIN
SELECT id INTO emp_id FROM employee ;
SELECT address INTO emp_address FROM employee ;
SELECT dob INTO emp_dob FROM employee ;
SELECT doj INTO emp_doj FROM employee ;
SELECT msisdn INTO emp_msisdn FROM employee ;
SELECT name INTO emp_name FROM employee ;
SELECT vehicle_number INTO emp_vehicl_number FROM employee ;
SELECT vehicle_type INTO emp_vehicl_type FROM employee ;
SELECT middlename INTO emp_middle_name FROM employee ;
SELECT lastNAme INTO emp_last_name FROM employee ;
INSERT
INTO test
(
idofEmp,
Empaddress,
Empdob,
Empdoj,
Empmsisdn,
Empname,
Empvehicle_number,
Empvehicle_type,
Empmiddlename,
EmplastNAme
)
VALUES
(
emp_id,
emp_address,
emp_dob,
emp_doj,
emp_msisdn,
emp_name,
emp_vehicl_number,
emp_vehicl_type,
emp_middle_name,
emp_last_name
);
END;
Please ignore datatypes:
I just want the last insert statement should be executed by java code and not by procedure.I dont want to keep insert statement in stored procedure. please sugggest
There's no need to execute 10 separate SELECT statements to retrieve ten separate fields from EMPLOYEE; you can do this in a single statement:
SELECT ID, ADDRESS, DOB, DOJ,
MSISDN, NAME, VEHICLE_NUMBER,
VEHICLE_TYPE, MIDDLENAME, LASTNAME
INTO emp_id, emp_address, emp_dob, emp_doj,
emp_msisdn, emp_name, emp_vehicl_number,
emp_vehicl_type, emp_middle_name, emp_last_name
FROM EMPLOYEE;
However, because there isn't a WHERE clause to limit the number of rows returned, both your original 10 statements and the above statement will fail with a TOO_MANY_ROWS exception if there is more than one row in the EMPLOYEE table.
As far as "returning" an INSERT statement, one way to do this would be to return the text of the INSERT statement so that it could be executed from Java. Something like:
CREATE OR REPLACE FUNCTION test_func(p_fromdate DATE)
RETURN VARCHAR2
AS
fromdate DATE;
todate DATE;
emp_id NUMBER := 0;
emp_address NUMBER := 0;
emp_dob NUMBER := 0;
emp_doj NUMBER := 0;
emp_msisdn NUMBER := 0;
emp_name NUMBER := 0;
emp_vehicl_number NUMBER := 0;
emp_vehicl_type NUMBER := 0;
emp_middle_name NUMBER := 0;
emp_last_name NUMBER := 0;
strInsert_stmt VARCHAR2(2000);
BEGIN
SELECT id, address, dob, doj,
msisdn, name, vehicle_number,
vehicle_type, middlename, lastname
INTO emp_id, emp_address, emp_dob, emp_doj,
emp_msisdn, emp_name, emp_vehicl_number,
emp_vehicl_type, emp_middle_name, emp_last_name
FROM employee;
strInsert_stmt := 'INSERT INTO test ' ||
'(idofEmp, Empaddress, Empdob, Empdoj, ' ||
'Empmsisdn, Empname, Empvehicle_number, ' ||
'Empvehicle_type, Empmiddlename, EmplastNAme) ' ||
'VALUES (' ||
emp_id || ',' ||
emp_address || ',' ||
emp_dob || ',' ||
emp_doj || ',' ||
emp_msisdn || ',' ||
emp_name || ',' ||
emp_vehicl_number || ',' ||
emp_vehicl_type || ',' ||
emp_middle_name || ',' ||
emp_last_name || ')';
RETURN strInsert_stmt;
END TEST_PROC;
One final bit of advice - in Oracle you shouldn't create objects with lower-case or mixed-case names, as you did by putting "test_proc" in double-quotes. Doing so will cause problems because most people will not expect objects to be named in anything other than the default (upper) case, and because any future use of these objects will require that the name be contained in double-quotes. For example, I believe that to invoke a procedure name "test_proc" (in lower case) from Java you'd need to send the name enclosed in double-quotes, as in:
objConnection.prepareCall("{call \"test_proc\"(?)}");
And if you have to add an upper-case schema name to the procedure in order to have the name resolved properly it gets even uglier:
objConnection.prepareCall("{call SOME_SCHEMA.\"test_proc\"(?)}");
In my experience it's best to create objects without putting their names in double-quotes - the names will be stored using upper-case, but you can still refer to them in lower or mixed case because Oracle will convert mixed-case or lower-case names to upper-case if the mixed-case or lower-case name is not in double quotes.
Edit
If you prefer to use a procedure instead of a function, try:
CREATE OR REPLACE FUNCTION test_func(p_fromdate IN DATE,
p_insert_stmt OUT VARCHAR2)
RETURN VARCHAR2
AS
fromdate DATE;
todate DATE;
emp_id NUMBER := 0;
emp_address NUMBER := 0;
emp_dob NUMBER := 0;
emp_doj NUMBER := 0;
emp_msisdn NUMBER := 0;
emp_name NUMBER := 0;
emp_vehicl_number NUMBER := 0;
emp_vehicl_type NUMBER := 0;
emp_middle_name NUMBER := 0;
emp_last_name NUMBER := 0;
BEGIN
SELECT id, address, dob, doj,
msisdn, name, vehicle_number,
vehicle_type, middlename, lastname
INTO emp_id, emp_address, emp_dob, emp_doj,
emp_msisdn, emp_name, emp_vehicl_number,
emp_vehicl_type, emp_middle_name, emp_last_name
FROM employee;
p_insert_stmt := 'INSERT INTO test ' ||
'(idofEmp, Empaddress, Empdob, Empdoj, ' ||
'Empmsisdn, Empname, Empvehicle_number, ' ||
'Empvehicle_type, Empmiddlename, EmplastNAme) ' ||
'VALUES (' ||
emp_id || ',' ||
emp_address || ',' ||
emp_dob || ',' ||
emp_doj || ',' ||
emp_msisdn || ',' ||
emp_name || ',' ||
emp_vehicl_number || ',' ||
emp_vehicl_type || ',' ||
emp_middle_name || ',' ||
emp_last_name || ')';
END TEST_PROC;

Get Installation Sequence of Oracle Objects

Ok, I have a complex recursion problem. I want to get a dependecy installation sequence of all of my objcts (all_objects table) in my Oracle 11g database.
First I have created a view holding all dependencies
create or replace
view REALLY_ALL_DEPENDENCIES as
select *
from ALL_DEPENDENCIES
union
select owner, index_name, 'INDEX', table_owner, table_name, table_type, null, null
from all_indexes
union
select p.owner, p.table_name, 'TABLE', f.owner, f.table_name, 'TABLE', null, null
from all_constraints p
join all_constraints f
on F.R_CONSTRAINT_NAME = P.CONSTRAINT_NAME
and F.CONSTRAINT_TYPE = 'R'
and p.constraint_type='P'
;
/
EDIT
I have tried do concate all dependencies by using this function:
create
or replace
function dependency(
i_name varchar2
,i_type varchar2
,i_owner varchar2
,i_level number := 0
,i_token clob := ' ') return clob
is
l_token clob := i_token;
l_exist number := 0;
begin
select count(*) into l_exist
from all_objects
where object_name = i_name
and object_type = i_type
and owner = i_owner;
if l_exist > 0 then
l_token := l_token || ';' || i_level || ';' ||
i_name || ':' || i_type || ':' || i_owner;
else
-- if not exist function recursion is finished
return l_token;
end if;
for tupl in (
select distinct
referenced_name
,referenced_type
,referenced_owner
from REALLY_ALL_DEPENDENCIES
where name = i_name
and type = i_type
and owner = i_owner
)
loop
-- if cyclic dependency stop and shout!
if i_token like '%' || tupl.referenced_name || ':' || tupl.referenced_type || ':' || tupl.referenced_owner || '%' then
select count(*) into l_exist
from REALLY_ALL_DEPENDENCIES
where name = tupl.referenced_name
and type = tupl.referenced_type
and owner = tupl.referenced_owner;
if l_exist > 0 then
return '!!!CYCLIC!!! (' || i_level || ';' || tupl.referenced_name || ':' || tupl.referenced_type || ':' || tupl.referenced_owner || '):' || l_token;
end if;
end if;
-- go into recursion
l_token := dependency(
tupl.referenced_name
,tupl.referenced_type
,i_owner /* I just want my own sources */
,i_level +1
,l_token);
end loop;
-- no cyclic condition and loop is finished
return l_token;
end;
/
And I can query through
select
object_name
,object_type
,owner
,to_char(dependency(object_name, object_type, owner)) as dependecy
from all_objects
where owner = 'SYSTEM'
;
Ok, maybe it is something like "cheating" but you can not do cyclic dependencies at creation time. So at least as a human beeing I am only able to create one object after another :-) And this sequence should be "reverse engineer able".
Now I am more interested in a solution than before ;-) And it is still about the tricky part ... "How can I select all soures from a schema orderd by its installation sequence (dependent objects list prior the using object)"?
It is just some kind of sorting problem, insn't it?
Usually you "cheat" by creating the objects in a particular order. For example, you might make sequences first (they have zero dependencies). Then you might do tables. After that, package specs, then package bodies, and so on.
Keep in mind that it is possible to have cyclic dependencies between packages, so there are cases where it will be impossible to satisfy all dependencies at creation anyway.
What's the business case here? Is there a real "problem" or just an exercise?
EDIT
The export tool we use exports objects in the following order:
Database Links
Sequences
Types
Tables
Views
Primary Keys
Indexes
Foreign Keys
Constraints
Triggers
Materialized Views
Materialized View Logs
Package Specs
Package Bodies
Procedures
Functions
At the end, we run the dbms_utility.compile_schema procedure to make sure everything is valid and no dependencies are missed. If you use other object types than these, I'm not sure where they'd go in this sequence.
Ok, I had some time to look at the job again and I want to share the results. Maybe anotherone comes across this thread searching for a solution. First of all I did the SQLs as SYS but I think you can do it in every schema using public synonyms.
The Procedure "exec obj_install_seq.make_install('SCOTT');" makes a clob containing a sql+ compatible sql file, assuming your sources are called "object_name.object_type.sql". Just spool it out.
Cheers
Chris
create global temporary table DEPENDENCIES on commit delete rows as
select * from ALL_DEPENDENCIES where 1=2 ;
/
create global temporary table install_seq(
idx number
,seq number
,iter number
,owner varchar2(30)
,name varchar2(30)
,type varchar2(30)
) on commit delete rows;
/
create global temporary table loop_chk(
iter number
,lvl number
,owner varchar2(30)
,name varchar2(30)
,type varchar2(30)
) on commit delete rows;
/
create or replace package obj_install_seq is
procedure make_install(i_schema varchar2 := 'SYSTEM');
end;
/
create or replace package body obj_install_seq is
subtype install_seq_t is install_seq%rowtype;
type dependency_list_t is table of DEPENDENCIES%rowtype;
procedure set_list_data(i_schema varchar2 := user)
is
l_owner varchar2(30) := i_schema;
begin
-- collect all dependencies
insert into DEPENDENCIES
select *
from (select *
from ALL_DEPENDENCIES
where owner = l_owner
and referenced_owner = l_owner
union
select owner, index_name, 'INDEX', table_owner, table_name, table_type, null, null
from all_indexes
where owner = l_owner
and table_owner = l_owner
union
select p.owner, p.table_name, 'TABLE', f.owner, f.table_name, 'TABLE', null, null
from all_constraints p
join all_constraints f
on F.R_CONSTRAINT_NAME = P.CONSTRAINT_NAME
and F.CONSTRAINT_TYPE = 'R'
and p.constraint_type='P'
and p.owner = f.owner
where p.owner = l_owner
) all_dep_tab;
-- collect all objects
insert into install_seq
select rownum, null,null, owner, object_name, object_type
from (select distinct owner, object_name, object_type, created
from all_objects
where owner = l_owner
order by created) objs;
end;
function is_referencing(
i_owner varchar2
,i_name varchar2
,i_type varchar2
,i_iter number
,i_level number := 0
) return boolean
is
l_cnt number;
begin
select count(*) into l_cnt
from loop_chk
where name = i_name
and owner = i_owner
and type = i_type
and iter = i_iter
and lvl < i_level;
insert into loop_chk values(i_iter,i_level,i_owner,i_name,i_type);
if l_cnt > 0 then
return true;
else
return false;
end if;
end;
procedure set_seq(
i_owner varchar2
,i_name varchar2
,i_type varchar2
,i_iter number
,i_level number := 0)
is
-- l_dep all_dependencies%rowtype;
l_idx number;
l_level number := i_level +1;
l_dep_list dependency_list_t;
l_cnt number;
begin
-- check for dependend source
begin
select * bulk collect into l_dep_list
from dependencies
where name = i_name
and owner = i_owner
and type = i_type;
if l_dep_list.count <= 0 then
-- recursion finished
return;
end if;
end;
for i in 1..l_dep_list.count loop
if is_referencing(
l_dep_list(i).referenced_owner
,l_dep_list(i).referenced_name
,l_dep_list(i).referenced_type
,i_iter
,i_level
) then
-- cyclic dependecy
update install_seq
set seq = 999
,iter = i_iter
where name = l_dep_list(i).referenced_name
and owner = l_dep_list(i).referenced_owner
and type = l_dep_list(i).referenced_type;
else
--chek if sequence is earlier
select count(*) into l_cnt
from install_seq
where name = l_dep_list(i).referenced_name
and owner = l_dep_list(i).referenced_owner
and type = l_dep_list(i).referenced_type
and seq > l_level *-1;
-- set sequence
if l_cnt > 0 then
update install_seq
set seq = l_level *-1
,iter = i_iter
where name = l_dep_list(i).referenced_name
and owner = l_dep_list(i).referenced_owner
and type = l_dep_list(i).referenced_type;
end if;
-- go recusrion
set_seq(
l_dep_list(i).referenced_owner
,l_dep_list(i).referenced_name
,l_dep_list(i).referenced_type
,i_iter + (i-1)
,l_level
);
end if;
end loop;
end;
function get_next_idx return number
is
l_idx number;
begin
select min(idx) into l_idx
from install_seq
where seq is null;
return l_idx;
end;
procedure make_install(i_schema varchar2 := 'SYSTEM')
is
l_obj install_seq_t;
l_idx number;
l_iter number := 0;
l_install_clob clob := chr(10);
begin
set_list_data(i_schema);
l_idx := get_next_idx;
while l_idx is not null loop
l_iter := l_iter +1;
select * into l_obj from install_seq where idx = l_idx;
update install_seq set iter = l_iter where idx = l_idx;
update install_seq set seq = 0 where idx = l_idx;
set_seq(l_obj.owner,l_obj.name,l_obj.type,l_iter);
l_idx := get_next_idx;
end loop;
for tupl in ( select * from install_seq order by seq, iter, idx ) loop
l_install_clob := l_install_clob || '#' ||
replace(tupl.name,' ' ,'') || '.' ||
replace(tupl.type,' ' ,'') || '.sql' ||
chr(10);
end loop;
l_install_clob := l_install_clob ||
'exec dbms_utility.compile_schema(''' || upper(i_schema) || ''');';
-- do with the install file what you want
DBMS_OUTPUT.PUT_LINE(dbms_lob.substr(l_install_clob,4000));
end;
end;
/

Resources