Adding conditionals to PL/SQL cursors - oracle

In the below PL/SQL program I have 2 cursors that are almost similar in functionality. I'm not sure, how I can retain the same functionality just by having one cursor and get rid of the get_all_employes cursor which doesn't have the where clause.
Thanks in advance for your time and help!
CREATE OR REPLACE PROCEDURE EMP_DETAILS(dept_id VARCHAR2) IS
CURSOR get_all_employes IS
SELECT EMP_ID, EMP_NAME, EMP_SAL, EMP_DEPT FROM EMP;
CURSOR get_employee_info IS
SELECT EMP_ID, EMP_NAME, EMP_SAL, EMP_DEPT FROM EMP WHERE EMP_DEPT = dept_id;
BEGIN
IF dept_id IS NULL THEN
FOR X IN get_all_employes;
DBMS_OUTPUT.PUT_LINE(X.EMP_ID || ' ' || X.EMP_NAME || ' ' || X.EMP_SAL || X.EMP_DEPT);
END LOOP;
ELSE
FOR Y IN get_all_employes;
DBMS_OUTPUT.PUT_LINE(Y.EMP_ID || ' ' || Y.EMP_NAME || ' ' || Y.EMP_SAL || Y.EMP_DEPT);
END LOOP;
END IF;
END EMP_DETAILS;
/

Use one cursor and include WHERE EMP_DEPT = dept_id OR dept_id IS NULL:
CREATE PROCEDURE EMP_DETAILS(
dept_id IN EMP.EMP_DEPT%TYPE
)
IS
CURSOR get_employee_info IS
SELECT EMP_ID,
EMP_NAME,
EMP_SAL,
EMP_DEPT
FROM EMP
WHERE EMP_DEPT = dept_id
OR dept_id IS NULL;
BEGIN
FOR Y IN get_employee_info LOOP
DBMS_OUTPUT.PUT_LINE(Y.EMP_ID || ' ' || Y.EMP_NAME || ' ' || Y.EMP_SAL || Y.EMP_DEPT);
END LOOP;
END EMP_DETAILS;
/
So, for the test data:
CREATE TABLE emp ( EMP_ID, EMP_NAME, EMP_SAL, EMP_DEPT ) AS
SELECT 1, 'a', 100, 1 FROM DUAL UNION ALL
SELECT 2, 'b', 200, 1 FROM DUAL UNION ALL
SELECT 3, 'c', 300, 2 FROM DUAL;
Then:
BEGIN
emp_details( NULL );
END;
/
Would output all the rows:
1 a 1001
2 b 2001
3 c 3002
and:
BEGIN
emp_details( 1 );
END;
/
would output only the rows for department 1:
1 a 1001
2 b 2001
db<>fiddle here

Try something like this (fyi your example code does not compile)
create or replace PROCEDURE EMP_DETAILS(dept_id VARCHAR2) IS
TYPE EmpCurTyp IS REF CURSOR;
emp_cv EmpCurTyp;
empRec EMP%ROWTYPE;
BEGIN
IF dept_id IS NULL THEN
open emp_cv for SELECT EMP_ID, EMP_NAME, EMP_SAL, EMP_DEPT FROM EMP;
ELSE
open emp_cv for SELECT EMP_ID, EMP_NAME, EMP_SAL, EMP_DEPT FROM EMP WHERE EMP_DEPT = dept_id;
END IF;
LOOP
FETCH emp_cv INTO empRec;
IF emp_cv%NOTFOUND
THEN
EXIT;
END IF;
DBMS_OUTPUT.PUT_LINE(empREc.EMP_ID || ' ' || empREc.EMP_NAME || ' ' || empREc.EMP_SAL || empREc.EMP_DEPT);
END LOOP;
CLOSE emp_cv;
END EMP_DETAILS;

Related

ORA-00904 and ORA-06512 when I try to MAX all columns

create or replace PROCEDURE COMPARAISON_TEST as
begin
declare
cursor c_tab is
select table_name, column_name , data_type
from all_tab_columns;
v_sql VARCHAR2 (32000);
begin
FOR r_tab in c_tab LOOP
v_sql := 'SELECT ' ||
r_tab.table_name || ' TABLE_NAME, ' ||
r_tab.column_name || ' COLUMN_NAME, ' ||
'(SELECT MAX(' || r_tab.column_name || ') FROM ' || r_tab.table_name || ') VALUE ' ||
'FROM DUAL ';
execute immediate v_sql;
end LOOP;
end;
end COMPARAISON_TEST;
ERRORS
ORA-00904: "DUAL" : invalid identifier
ORA-06512: à "ODS.COMPARAISON_TEST", line 20
ORA-06512: à "ODS.COMPARAISON_TEST", line 20
ORA-06512: à line 2
Problem is that you're doing it in the dark.
Always, before actually executing dynamic SQL, display it on the screen so that you'd see the string to be executed.
This is what you did (I added WHERE clause to a cursor, to shorten the output):
SQL> declare
2
3 cursor c_tab is
4 select table_name, column_name , data_type
5 from all_tab_columns
6 where owner = 'SCOTT' and table_name = 'DEPT';
7
8 v_sql VARCHAR2 (32000);
9
10 begin
11
12 FOR r_tab in c_tab LOOP
13 v_sql := 'SELECT ' ||
14 r_tab.table_name || ' TABLE_NAME, ' ||
15 r_tab.column_name || ' COLUMN_NAME, ' ||
16 '(SELECT MAX(' || r_tab.column_name || ') FROM ' || r_tab.table_name || ') VALUE ' ||
17 'FROM DUAL ';
18 dbms_output.put_line(v_sql);
19 --execute immediate v_sql;
20 end LOOP;
21 end;
22 /
SELECT DEPT TABLE_NAME, DEPTNO COLUMN_NAME, (SELECT MAX(DEPTNO) FROM DEPT) VALUE
FROM DUAL
SELECT DEPT TABLE_NAME, DNAME COLUMN_NAME, (SELECT MAX(DNAME) FROM DEPT) VALUE
FROM DUAL
SELECT DEPT TABLE_NAME, LOC COLUMN_NAME, (SELECT MAX(LOC) FROM DEPT) VALUE FROM
DUAL
PL/SQL procedure successfully completed.
SQL>
See all those invalid strings? They can't be executed. In order to make them valid, include table and column names into single quotes, e.g.
SELECT 'DEPT' TABLE_NAME, 'DEPTNO' COLUMN_NAME, (SELECT MAX(DEPTNO) FROM DEPT) VALUE
FROM DUAL;
This is what you might have wanted:
SQL> set serveroutput on
SQL>
SQL> declare
2 cursor c_tab is
3 select table_name, column_name , data_type
4 from user_tab_columns
5 where table_name = 'DEPT';
6 v_sql VARCHAR2 (32000);
7 v_val varchar2(200);
8 begin
9 FOR r_tab in c_tab LOOP
10 v_sql := 'SELECT MAX(' || r_tab.column_name || ') FROM ' ||
11 r_tab.table_name;
12 execute immediate v_sql into v_val;
13 dbms_output.put_line(r_tab.table_name||'.'||r_tab.column_name||': '|| v_val);
14 end LOOP;
15 end;
16 /
DEPT.DEPTNO: 40
DEPT.DNAME: SALES
DEPT.LOC: NEW YORK
PL/SQL procedure successfully completed.
SQL>
If you want to store result into a table, then you'd do something like this:
SQL> create table maxes
2 (table_name varchar2(30),
3 column_name varchar2(30),
4 max_value varchar2(30)
5 );
Table created.
SQL> declare
2 cursor c_tab is
3 select table_name, column_name , data_type
4 from user_tab_columns
5 where table_name = 'DEPT';
6 v_sql VARCHAR2 (32000);
7 v_val varchar2(200);
8 begin
9 FOR r_tab in c_tab LOOP
10 v_sql := 'SELECT MAX(' || r_tab.column_name || ') FROM ' ||
11 r_tab.table_name;
12 execute immediate v_sql into v_val;
13 insert into maxes (table_name, column_name, max_value)
14 values
15 (r_tab.table_name, r_tab.column_name, v_val);
16 end LOOP;
17 end;
18 /
PL/SQL procedure successfully completed.
SQL> select * From maxes;
TABLE_NAME COLUMN_NAME MAX_VALUE
--------------- --------------- ---------------
DEPT DEPTNO 40
DEPT DNAME SALES
DEPT LOC NEW YORK
SQL>
You should double single quotes that you want to appear within the string.
v_sql := 'SELECT ''' ||
r_tab.table_name || ''' TABLE_NAME, ''' ||
r_tab.column_name || ''' COLUMN_NAME, ' ||
'(SELECT MAX(' || r_tab.column_name || ') FROM ' || r_tab.table_name || ') VALUE ' ||
'FROM DUAL ';

ORA-00904 invalid identifier error in dynamic SQL block

I'm running the below which once executed, an error is reported telling me that EST_ONE_ROW_MB is an invalid identifier.
I've been advised I perhaps need to get the dynamic SQL part running as a stand alone query to begin with as an initial troubleshooting exercise but I'm a bit stumped in terms of how to write a sub-query here that will produce the desired output and eliminate the error.
create or replace procedure JUST_ME is
--variables
l_dblink varchar2(100) := 'DB1';
file_handle UTL_FILE.file_type;
v_ts_name varchar2(30);
v_link_name varchar2(10);
v_csv_name varchar2(100);
EST_ONE_ROW_MB varchar2(100) ;
TOTAL_ROW_COUNT NUMBER;
SPACE_REQUIRED NUMBER;
TOT_OBJECT_SIZE_MB NUMBER;
v_Mv_name varchar2(100);
v_sql1 varchar2(1500);
cur SYS_REFCURSOR;
owner varchar2(100);
table_name varchar2(100);
driver_table varchar2(100);
mandatory_join varchar2(100);
C_TOTAL_ROW_COUNT varchar2(100);
v_total_driver_only varchar2(100);
--
begin
SELECT tablename into v_csv_name
FROM BOB01.BOB_new_table_tracker
WHERE
CREATED_AT = (select MAX(CREATED_AT) from BOB01.BOB_new_table_tracker);
SELECT mv_name into v_Mv_name
FROM BOB01.BOB_new_table_tracker_mv
WHERE
CREATED_AT = (select MAX(CREATED_AT) from BOB01.BOB_new_table_tracker_mv);
select link_name into v_link_name from link_and_mail where mdate = (select max(mdate) from link_and_mail);
select distinct targetschema into v_ts_name from BOB01.MV_BOB_TABLE;
v_sql1 := 'SELECT /*+ monitor parallel (4)*/ a.owner,
a.table_name,
b.driver_table,
b.mandatory_join,
sum(c.sum_bytes) TOT_OBJECT_SIZE_MB,
(TOT_OBJECT_SIZE_MB) / (:C_TOTAL_ROW_COUNT) AS "EST_ONE_ROW_MB",
(EST_ONE_ROW_MB) * (:TOTAL_ROW_COUNT) AS "SPACE_REQUIRED"
FROM dba_tables#DB1 a, '|| v_Mv_name ||' b, MV_PRD_SEG_DATA c
WHERE a.table_name IN ( SELECT table_name
FROM MV_BOB_TABLE
WHERE driver_table IS NOT NULL
AND additional_joins IS NULL
)
AND a.owner IN ( SELECT DISTINCT productionschema FROM MV_BOB_TABLE c )
and a.table_name = b.table_name
and a.table_name = c.segment_name
group by a.owner,a.table_name,b.driver_table,b.mandatory_join
ORDER BY table_name';
file_handle := utl_file.fopen('ESTIMATES_CSV', v_csv_name||'_EST_PROC.csv', 'w', 32767);
--
UTL_FILE.PUT_LINE(file_handle, ' ');
UTL_FILE.PUT_LINE(file_handle, 'The below report shows total row counts in PROD');
UTL_FILE.PUT_LINE(file_handle, ' for unjoined tables in the BOB document:');
UTL_FILE.PUT_LINE(file_handle, ' ');
utl_file.put_line(file_handle, 'OWNER,TABLE_NAME,MANDATORY_JOIN,TOT_OBJECT_SIZE_MB,EST_ONE_ROW_MB,TOTAL_ROW_COUNT,SPACE_REQUIRED');
--main loop
open cur for v_sql1 using TOTAL_ROW_COUNT,C_TOTAL_ROW_COUNT;
loop
fetch cur into OWNER,TABLE_NAME,MANDATORY_JOIN,TOT_OBJECT_SIZE_MB,EST_ONE_ROW_MB,TOTAL_ROW_COUNT,SPACE_REQUIRED;--,EST_ONE_ROW_MB;
exit when cur%NOTFOUND;
execute immediate' select /*+parallel (4)*/ count(*) from '||owner||'.'||table_name || '#' || l_dblink into TOTAL_ROW_COUNT;
execute immediate' select /*+monitor parallel (10)*/ count(*) from ' ||owner||'.'||table_name || '#' || l_dblink||' b '||','||
driver_table || '#' || l_dblink||' a ' ||' where ' ||mandatory_join into TOTAL_ROW_COUNT;
execute immediate' select /*+monitor parallel (10)*/ count(*) from ' ||owner||'.'||table_name || '#' || l_dblink into C_TOTAL_ROW_COUNT;
utl_file.put_line(file_handle,
OWNER || ',' ||
TABLE_NAME || ',' ||
TOT_OBJECT_SIZE_MB || ',' ||
TOTAL_ROW_COUNT || ',' ||
C_TOTAL_ROW_COUNT || ',' ||
round(TOT_OBJECT_SIZE_MB / C_TOTAL_ROW_COUNT,7)|| ',' ||
round(round(TOT_OBJECT_SIZE_MB / C_TOTAL_ROW_COUNT,7) * round(TOTAL_ROW_COUNT,0),0)
);
v_total_driver_only := v_total_driver_only + round(TOT_OBJECT_SIZE_MB / C_TOTAL_ROW_COUNT,7) * round(TOTAL_ROW_COUNT,0);
end loop;
UTL_FILE.PUT_LINE(file_handle, ' ');
utl_file.put_line(file_handle,
'Total Estimated Space Required '|| round(v_total_driver_only,0) ||' MB'
);
utl_file.fclose(file_handle);
end JUST_ME;
to use EST_ONE_ROW_MB on that way is not possible because its' not defied as column.
replace it by (TOT_OBJECT_SIZE_MB) / (:C_TOTAL_ROW_COUNT)
SELECT /*+ monitor parallel (4)*/ a.owner,
a.table_name,
b.driver_table,
b.mandatory_join,
sum(c.sum_bytes) TOT_OBJECT_SIZE_MB, -- will be the same problem
(sum(c.sum_bytes)) / (:C_TOTAL_ROW_COUNT) AS "EST_ONE_ROW_MB",
((sum(c.sum_bytes)) / (:C_TOTAL_ROW_COUNT)) * (:TOTAL_ROW_COUNT) AS "SPACE_REQUIRED"
...
you can do that if you have an Inline View. e.g.
select EST_ONE_ROW_MB * (:TOTAL_ROW_COUNT) AS "SPACE_REQUIRED"
from(
select (TOT_OBJECT_SIZE_MB) / (:C_TOTAL_ROW_COUNT) AS "EST_ONE_ROW_MB"
from ....
)

Dynamic SQL Hangs [duplicate]

Is it possible to search every field of every table for a particular value in Oracle?
There are hundreds of tables with thousands of rows in some tables so I know this could take a very long time to query. But the only thing I know is that a value for the field I would like to query against is 1/22/2008P09RR8.
<
I've tried using this statement below to find an appropriate column based on what I think it should be named but it returned no results.
SELECT * from dba_objects
WHERE object_name like '%DTN%'
There is absolutely no documentation on this database and I have no idea where this field is being pulled from.
Any thoughts?
Quote:
I've tried using this statement below
to find an appropriate column based on
what I think it should be named but it
returned no results.*
SELECT * from dba_objects WHERE
object_name like '%DTN%'
A column isn't an object. If you mean that you expect the column name to be like '%DTN%', the query you want is:
SELECT owner, table_name, column_name FROM all_tab_columns WHERE column_name LIKE '%DTN%';
But if the 'DTN' string is just a guess on your part, that probably won't help.
By the way, how certain are you that '1/22/2008P09RR8' is a value selected directly from a single column? If you don't know at all where it is coming from, it could be a concatenation of several columns, or the result of some function, or a value sitting in a nested table object. So you might be on a wild goose chase trying to check every column for that value. Can you not start with whatever client application is displaying this value and try to figure out what query it is using to obtain it?
Anyway, diciu's answer gives one method of generating SQL queries to check every column of every table for the value. You can also do similar stuff entirely in one SQL session using a PL/SQL block and dynamic SQL. Here's some hastily-written code for that:
SET SERVEROUTPUT ON SIZE 100000
DECLARE
match_count INTEGER;
BEGIN
FOR t IN (SELECT owner, table_name, column_name
FROM all_tab_columns
WHERE owner <> 'SYS' and data_type LIKE '%CHAR%') LOOP
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||
' WHERE '||t.column_name||' = :1'
INTO match_count
USING '1/22/2008P09RR8';
IF match_count > 0 THEN
dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count );
END IF;
END LOOP;
END;
/
There are some ways you could make it more efficient too.
In this case, given the value you are looking for, you can clearly eliminate any column that is of NUMBER or DATE type, which would reduce the number of queries. Maybe even restrict it to columns where type is like '%CHAR%'.
Instead of one query per column, you could build one query per table like this:
SELECT * FROM table1
WHERE column1 = 'value'
OR column2 = 'value'
OR column3 = 'value'
...
;
I did some modification to the above code to make it work faster if you are searching in only one owner.
You just have to change the 3 variables v_owner, v_data_type and v_search_string to fit what you are searching for.
SET SERVEROUTPUT ON SIZE 100000
DECLARE
match_count INTEGER;
-- Type the owner of the tables you are looking at
v_owner VARCHAR2(255) :='ENTER_USERNAME_HERE';
-- Type the data type you are look at (in CAPITAL)
-- VARCHAR2, NUMBER, etc.
v_data_type VARCHAR2(255) :='VARCHAR2';
-- Type the string you are looking at
v_search_string VARCHAR2(4000) :='string to search here...';
BEGIN
FOR t IN (SELECT table_name, column_name FROM all_tab_cols where owner=v_owner and data_type = v_data_type) LOOP
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM '||t.table_name||' WHERE '||t.column_name||' = :1'
INTO match_count
USING v_search_string;
IF match_count > 0 THEN
dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count );
END IF;
END LOOP;
END;
/
I know this is an old topic. But I see a comment to the question asking if it could be done in SQL rather than using PL/SQL. So thought to post a solution.
The below demonstration is to Search for a VALUE in all COLUMNS of all TABLES in an entire SCHEMA:
Search a CHARACTER type
Let's look for the value KING in SCOTT schema.
SQL> variable val varchar2(10)
SQL> exec :val := 'KING'
PL/SQL procedure successfully completed.
SQL> SELECT DISTINCT SUBSTR (:val, 1, 11) "Searchword",
2 SUBSTR (table_name, 1, 14) "Table",
3 SUBSTR (column_name, 1, 14) "Column"
4 FROM cols,
5 TABLE (xmlsequence (dbms_xmlgen.getxmltype ('select '
6 || column_name
7 || ' from '
8 || table_name
9 || ' where upper('
10 || column_name
11 || ') like upper(''%'
12 || :val
13 || '%'')' ).extract ('ROWSET/ROW/*') ) ) t
14 ORDER BY "Table"
15 /
Searchword Table Column
----------- -------------- --------------
KING EMP ENAME
SQL>
Search a NUMERIC type
Let's look for the value 20 in SCOTT schema.
SQL> variable val NUMBER
SQL> exec :val := 20
PL/SQL procedure successfully completed.
SQL> SELECT DISTINCT SUBSTR (:val, 1, 11) "Searchword",
2 SUBSTR (table_name, 1, 14) "Table",
3 SUBSTR (column_name, 1, 14) "Column"
4 FROM cols,
5 TABLE (xmlsequence (dbms_xmlgen.getxmltype ('select '
6 || column_name
7 || ' from '
8 || table_name
9 || ' where upper('
10 || column_name
11 || ') like upper(''%'
12 || :val
13 || '%'')' ).extract ('ROWSET/ROW/*') ) ) t
14 ORDER BY "Table"
15 /
Searchword Table Column
----------- -------------- --------------
20 DEPT DEPTNO
20 EMP DEPTNO
20 EMP HIREDATE
20 SALGRADE HISAL
20 SALGRADE LOSAL
SQL>
Yes you can and your DBA will hate you and will find you to nail your shoes to the floor because that will cause lots of I/O and bring the database performance really down as the cache purges.
select column_name from all_tab_columns c, user_all_tables u where c.table_name = u.table_name;
for a start.
I would start with the running queries, using the v$session and the v$sqlarea. This changes based on oracle version. This will narrow down the space and not hit everything.
Here is another modified version that will compare a lower substring match. This works in Oracle 11g.
DECLARE
match_count INTEGER;
-- Type the owner of the tables you are looking at
v_owner VARCHAR2(255) :='OWNER_NAME';
-- Type the data type you are look at (in CAPITAL)
-- VARCHAR2, NUMBER, etc.
v_data_type VARCHAR2(255) :='VARCHAR2';
-- Type the string you are looking at
v_search_string VARCHAR2(4000) :='%lower-search-sub-string%';
BEGIN
FOR t IN (SELECT table_name, column_name FROM all_tab_cols where owner=v_owner and data_type = v_data_type) LOOP
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM '||t.table_name||' WHERE lower('||t.column_name||') like :1'
INTO match_count
USING v_search_string;
IF match_count > 0 THEN
dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count );
END IF;
END LOOP;
END;
/
I modified Flood's script to execute once for each table rather than for every column of each table for faster execution. It requires Oracle 11g or greater.
set serveroutput on size 100000
declare
v_match_count integer;
v_counter integer;
-- The owner of the tables to search through (case-sensitive)
v_owner varchar2(255) := 'OWNER_NAME';
-- A string that is part of the data type(s) of the columns to search through (case-insensitive)
v_data_type varchar2(255) := 'CHAR';
-- The string to be searched for (case-insensitive)
v_search_string varchar2(4000) := 'FIND_ME';
-- Store the SQL to execute for each table in a CLOB to get around the 32767 byte max size for a VARCHAR2 in PL/SQL
v_sql clob := '';
begin
for cur_tables in (select owner, table_name from all_tables where owner = v_owner and table_name in
(select table_name from all_tab_columns where owner = all_tables.owner and data_type like '%' || upper(v_data_type) || '%')
order by table_name) loop
v_counter := 0;
v_sql := '';
for cur_columns in (select column_name from all_tab_columns where
owner = v_owner and table_name = cur_tables.table_name and data_type like '%' || upper(v_data_type) || '%') loop
if v_counter > 0 then
v_sql := v_sql || ' or ';
end if;
v_sql := v_sql || 'upper(' || cur_columns.column_name || ') like ''%' || upper(v_search_string) || '%''';
v_counter := v_counter + 1;
end loop;
v_sql := 'select count(*) from ' || cur_tables.table_name || ' where ' || v_sql;
execute immediate v_sql
into v_match_count;
if v_match_count > 0 then
dbms_output.put_line('Match in ' || cur_tables.owner || ': ' || cur_tables.table_name || ' - ' || v_match_count || ' records');
end if;
end loop;
exception
when others then
dbms_output.put_line('Error when executing the following: ' || dbms_lob.substr(v_sql, 32600));
end;
/
I was having following issues for #Lalit Kumars answer,
ORA-19202: Error occurred in XML processing
ORA-00904: "SUCCESS": invalid identifier
ORA-06512: at "SYS.DBMS_XMLGEN", line 288
ORA-06512: at line 1
19202. 00000 - "Error occurred in XML processing%s"
*Cause: An error occurred when processing the XML function
*Action: Check the given error message and fix the appropriate problem
Solution is:
WITH char_cols AS
(SELECT /*+materialize */ table_name, column_name
FROM cols
WHERE data_type IN ('CHAR', 'VARCHAR2'))
SELECT DISTINCT SUBSTR (:val, 1, 11) "Searchword",
SUBSTR (table_name, 1, 14) "Table",
SUBSTR (column_name, 1, 14) "Column"
FROM char_cols,
TABLE (xmlsequence (dbms_xmlgen.getxmltype ('select "'
|| column_name
|| '" from "'
|| table_name
|| '" where upper("'
|| column_name
|| '") like upper(''%'
|| :val
|| '%'')' ).extract ('ROWSET/ROW/*') ) ) t
ORDER BY "Table"
/
I would do something like this (generates all the selects you need).
You can later on feed them to sqlplus:
echo "select table_name from user_tables;" | sqlplus -S user/pwd | grep -v "^--" | grep -v "TABLE_NAME" | grep "^[A-Z]" | while read sw;
do echo "desc $sw" | sqlplus -S user/pwd | grep -v "\-\-\-\-\-\-" | awk -F' ' '{print $1}' | while read nw;
do echo "select * from $sw where $nw='val'";
done;
done;
It yields:
select * from TBL1 where DESCRIPTION='val'
select * from TBL1 where ='val'
select * from TBL2 where Name='val'
select * from TBL2 where LNG_ID='val'
And what it does is - for each table_name from user_tables get each field (from desc) and create a select * from table where field equals 'val'.
if we know the table and colum names but want to find out the number of times string is appearing for each schema:
Declare
owner VARCHAR2(1000);
tbl VARCHAR2(1000);
cnt number;
ct number;
str_sql varchar2(1000);
reason varchar2(1000);
x varchar2(1000):='%string_to_be_searched%';
cursor csr is select owner,table_name
from all_tables where table_name ='table_name';
type rec1 is record (
ct VARCHAR2(1000));
type rec is record (
owner VARCHAR2(1000):='',
table_name VARCHAR2(1000):='');
rec2 rec;
rec3 rec1;
begin
for rec2 in csr loop
--str_sql:= 'select count(*) from '||rec.owner||'.'||rec.table_name||' where CTV_REMARKS like '||chr(39)||x||chr(39);
--dbms_output.put_line(str_sql);
--execute immediate str_sql
execute immediate 'select count(*) from '||rec2.owner||'.'||rec2.table_name||' where column_name like '||chr(39)||x||chr(39)
into rec3;
if rec3.ct <> 0 then
dbms_output.put_line(rec2.owner||','||rec3.ct);
else null;
end if;
end loop;
end;
Procedure to Search Entire Database:
CREATE or REPLACE PROCEDURE SEARCH_DB(SEARCH_STR IN VARCHAR2, TAB_COL_RECS OUT VARCHAR2) IS
match_count integer;
qry_str varchar2(1000);
CURSOR TAB_COL_CURSOR IS
SELECT TABLE_NAME,COLUMN_NAME,OWNER,DATA_TYPE FROM ALL_TAB_COLUMNS WHERE DATA_TYPE in ('NUMBER','VARCHAR2') AND OWNER='SCOTT';
BEGIN
FOR TAB_COL_REC IN TAB_COL_CURSOR
LOOP
qry_str := 'SELECT COUNT(*) FROM '||TAB_COL_REC.OWNER||'.'||TAB_COL_REC.TABLE_NAME||
' WHERE '||TAB_COL_REC.COLUMN_NAME;
IF TAB_COL_REC.DATA_TYPE = 'NUMBER' THEN
qry_str := qry_str||'='||SEARCH_STR;
ELSE
qry_str := qry_str||' like '||SEARCH_STR;
END IF;
--dbms_output.put_line( qry_str );
EXECUTE IMMEDIATE qry_str INTO match_count;
IF match_count > 0 THEN
dbms_output.put_line( qry_str );
--dbms_output.put_line( TAB_COL_REC.TABLE_NAME ||' '||TAB_COL_REC.COLUMN_NAME ||' '||match_count);
TAB_COL_RECS := TAB_COL_RECS||'##'||TAB_COL_REC.TABLE_NAME||'##'||TAB_COL_REC.COLUMN_NAME;
END IF;
END LOOP;
END SEARCH_DB;
Execute Statement
DECLARE
SEARCH_STR VARCHAR2(200);
TAB_COL_RECS VARCHAR2(200);
BEGIN
SEARCH_STR := 10;
SEARCH_DB(
SEARCH_STR => SEARCH_STR,
TAB_COL_RECS => TAB_COL_RECS
);
DBMS_OUTPUT.PUT_LINE('TAB_COL_RECS = ' || TAB_COL_RECS);
END;
Sample Results
Connecting to the database test.
SELECT COUNT(*) FROM SCOTT.EMP WHERE DEPTNO=10
SELECT COUNT(*) FROM SCOTT.DEPT WHERE DEPTNO=10
TAB_COL_RECS = ##EMP##DEPTNO##DEPT##DEPTNO
Process exited.
Disconnecting from the database test.
I don't of a simple solution on the SQL promprt. Howeve there are quite a few tools like toad and PL/SQL Developer that have a GUI where a user can input the string to be searched and it will return the table/procedure/object where this is found.
There are some free tools that make these kind of search, for example, this one works fine and source code is available:
https://sites.google.com/site/freejansoft/dbsearch
You'll need the Oracle ODBC driver and a DSN to use this tool.
Modifying the code to search case-insensitively using a LIKE query instead of finding exact matches...
DECLARE
match_count INTEGER;
-- Type the owner of the tables you want to search.
v_owner VARCHAR2(255) :='USER';
-- Type the data type you're looking for (in CAPS). Examples include: VARCHAR2, NUMBER, etc.
v_data_type VARCHAR2(255) :='VARCHAR2';
-- Type the string you are looking for.
v_search_string VARCHAR2(4000) :='Test';
BEGIN
dbms_output.put_line( 'Starting the search...' );
FOR t IN (SELECT table_name, column_name FROM all_tab_cols where owner=v_owner and data_type = v_data_type) LOOP
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM '||t.table_name||' WHERE LOWER('||t.column_name||') LIKE :1'
INTO match_count
USING LOWER('%'||v_search_string||'%');
IF match_count > 0 THEN
dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count );
END IF;
END LOOP;
END;
I found the best solution but it's a little slow. (It will work perfectly with all SQL IDE's.)
SELECT DISTINCT table_name, column_name, data_type
FROM user_tab_cols,
TABLE (xmlsequence (dbms_xmlgen.getxmltype ('select '
|| column_name
|| ' from '
|| table_name
|| ' where lower('
|| column_name
|| ') like lower(''%'
|| 'your_text_here'
|| '%'')' ).extract ('ROWSET/ROW/*') ) ) a
where table_name not in (
select distinct table_name
from user_tab_cols where data_type like 'SDO%'
or data_type like '%LOB') AND DATA_TYPE = 'VARCHAR2'
order by table_name, column_name;
--it run completed -- no error
SET SERVEROUTPUT ON SIZE 100000
DECLARE
v_match_count INTEGER;
v_counter INTEGER;
v_owner VARCHAR2 (255) := 'VASOA';
v_search_string VARCHAR2 (4000) := '99999';
v_data_type VARCHAR2 (255) := 'CHAR';
v_sql CLOB := '';
BEGIN
FOR cur_tables
IN ( SELECT owner, table_name
FROM all_tables
WHERE owner = v_owner
AND table_name IN (SELECT table_name
FROM all_tab_columns
WHERE owner = all_tables.owner
AND data_type LIKE
'%'
|| UPPER (v_data_type)
|| '%')
ORDER BY table_name)
LOOP
v_counter := 0;
v_sql := '';
FOR cur_columns
IN (SELECT column_name, table_name
FROM all_tab_columns
WHERE owner = v_owner
AND table_name = cur_tables.table_name
AND data_type LIKE '%' || UPPER (v_data_type) || '%')
LOOP
IF v_counter > 0
THEN
v_sql := v_sql || ' or ';
END IF;
IF cur_columns.column_name is not null
THEN
v_sql :=
v_sql
|| 'upper('
|| cur_columns.column_name
|| ') ='''
|| UPPER (v_search_string)||'''';
v_counter := v_counter + 1;
END IF;
END LOOP;
IF v_sql is null
THEN
v_sql :=
'select count(*) from '
|| v_owner
|| '.'
|| cur_tables.table_name;
END IF;
IF v_sql is not null
THEN
v_sql :=
'select count(*) from '
|| v_owner
|| '.'
|| cur_tables.table_name
|| ' where '
|| v_sql;
END IF;
--v_sql := 'select count(*) from ' ||v_owner||'.'|| cur_tables.table_name ||' where '|| v_sql;
--dbms_output.put_line(v_sql);
--DBMS_OUTPUT.put_line (v_sql);
EXECUTE IMMEDIATE v_sql INTO v_match_count;
IF v_match_count > 0
THEN
DBMS_OUTPUT.put_line (v_sql);
dbms_output.put_line('Match in ' || cur_tables.owner || ': ' || cur_tables.table_name || ' - ' || v_match_count || ' records');
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (
'Error when executing the following: '
|| DBMS_LOB.SUBSTR (v_sql, 32600));
END;
/
Borrowing, slightly enhancing and simplifying from this Blog post the following simple SQL statement seems to do the job quite well:
SELECT DISTINCT (:val) "Search Value", TABLE_NAME "Table", COLUMN_NAME "Column"
FROM cols,
TABLE (XMLSEQUENCE (DBMS_XMLGEN.GETXMLTYPE(
'SELECT "' || COLUMN_NAME || '" FROM "' || TABLE_NAME || '" WHERE UPPER("'
|| COLUMN_NAME || '") LIKE UPPER(''%' || :val || '%'')' ).EXTRACT ('ROWSET/ROW/*')))
ORDER BY "Table";
The Oracle LIKE condition allows wildcards to be used in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement.
%: to match any string of any length
Eg-
SELECT last_name
FROM customer_tab
WHERE last_name LIKE '%A%';
-: to match on a single character
Eg-
SELECT last_name
FROM customer_tab
WHERE last_name LIKE 'A_t';

Oracle - Copy data from one row to another based on dynamic selected columns

I have a table with two rows and I need to copy some data in row A to row B.
My biggest concern here is that the columns involved are not static in name or number (table can grow or shrink in column number).
I came up with a solution that i don't like at all...
create or replace
PROCEDURE Z_COPY_TASK_ATT_PE
(
par_oldRowId IN VARCHAR2,
par_newRowId IN VARCHAR2
)
IS
var_update VARCHAR2(4000 BYTE);
var_crr_col_value_old VARCHAR2(10 BYTE);
var_crr_select VARCHAR2(4000 BYTE);
BEGIN
var_update := 'UPDATE PLANNING_ENTITY SET ';
for i in (
Select COLUMN_NAME
from user_tab_columns
where table_name='PLANNING_ENTITY'
and lower(COLUMN_NAME) like 'code%'
)
loop
var_crr_select := 'select ' || i.column_name
|| ' from planning_entity where planning_code = '''
|| par_oldRowId || '''';
execute immediate var_crr_select
into var_crr_col_value_old;
var_update := var_update || i.column_name || ' = ''' || var_crr_col_value_old || ''', ';
end loop;
var_update := SUBSTR(var_update, 0, length(var_update)-2 );
var_update := var_update || ' where planning_code = ''' || par_newRowId || '''';
execute immediate var_update;
commit;
END;
My major issue here (for me at least) is the fact that i need to make the select for each iteration of the loop. It would be really nice if i could make a select that returned something like column|name and then all I needed to do was i.column and i.value.
What do you think?
===== Solution following Marmite Bomber Answer =====
create or replace
PROCEDURE Z_COPY_TASK_ATT_PE
(
par_oldRowId IN VARCHAR2,
par_newRowId IN VARCHAR2
)
IS
var_update VARCHAR2(4000 BYTE);
var_columns varchar2(4000 BYTE);
BEGIN
Select LISTAGG(COLUMN_NAME, ',') WITHIN group (order by column_name) into var_columns
from user_tab_columns
where table_name='PLANNING_ENTITY'
and lower(COLUMN_NAME) like 'code%';
var_update := 'UPDATE PLANNING_ENTITY PE1 SET (' || var_columns ||
') = (SELECT ' || var_columns || ' FROM PLANNING_ENTITY PE2 WHERE PE2.PLANNING_CODE = ''' || par_oldRowId ||
''') WHERE PE1.PLANNING_CODE = ''' || par_newRowId || '''';
execute immediate var_update;
commit;
END;
Based on this update
UPDATE t1 a
SET (code1, code2) = (
SELECT code1,code2
FROM t1 b
WHERE b.planning_code = 'new')
where a.planning_code = 'old';
you need only generate comma separated list of the column names that should be updated and apply it twice in the statement. In the example code1,code2.
How about this?
begin
for i in (select * from PLANNING_ENTITY where planning_code = newrow) loop
update PLANNING_ENTITY set row = i where planning_code = oldrow;
end loop;
end;
For downvoters:
SQL> select version from v$instance;
VERSION
-----------------
11.2.0.4.0
SQL> create table it_works_in_oracle (n1 number, n2 number, n3 number);
Table created.
SQL> insert into it_works_in_oracle values(1, 1, 1);
1 row created.
SQL> insert into it_works_in_oracle values(2, 2, 2);
1 row created.
SQL> insert into it_works_in_oracle values(3, 3, 3);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from it_works_in_oracle;
N1 N2 N3
---------- ---------- ----------
1 1 1
2 2 2
3 3 3
SQL> set serveroutput on
SQL> declare
oldrow number:=1;
newrow number:=3;
begin
for i in (select * from it_works_in_oracle where n1 = newrow) loop
update it_works_in_oracle set row = i where n1 = oldrow;
end loop;
end; 2 3 4 5 6 7 8
9 /
PL/SQL procedure successfully completed.
SQL> select * from it_works_in_oracle;
N1 N2 N3
---------- ---------- ----------
3 3 3
2 2 2
3 3 3
You can do it similar to this (not tested):
var_update := 'UPDATE PLANNING_ENTITY a SET (';
FOR i IN (
SELECT COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE table_name='PLANNING_ENTITY'
AND LOWER(COLUMN_NAME) LIKE 'code%'
)
LOOP
var_update := var_update || i.column_name ||',';
END LOOP;
var_update := REGEXP_REPLACE(var_update, ',$', ')');
var_update := var_update ||' = (SELECT ';
FOR i IN (
SELECT COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE table_name='PLANNING_ENTITY'
AND LOWER(COLUMN_NAME) LIKE 'code%'
)
LOOP
var_update := var_update || i.column_name ||',';
END LOOP;
var_update := REGEXP_REPLACE(var_update, ',$');
var_update := var_update || ' FROM PLANNING_ENTITY b WHERE planning_code = :newRowId) ';
var_update := var_update || ' WHERE a.planning_code = :oldRowId';
EXECUTE IMMEDIATE var_update USING par_newRowId, par_oldRowId;

oracle - write package output results to a file on client side

So i have this:
CREATE OR REPLACE PACKAGE my_first_package
IS
PROCEDURE employee_analysis
(p_id IN NUMBER := 100, /*default formal parameter with no arguments for invokation */
p_percent IN NUMBER := 0.01); /* -||- */
END my_first_package;
CREATE OR REPLACE PACKAGE BODY my_first_package
IS
PROCEDURE employee_analysis
(p_id IN NUMBER := 100,
p_percent IN NUMBER := 0.01)
IS
CURSOR c_city IS
(SELECT l.city
FROM employees e
INNER JOIN departments d
ON (e.department_id = d.department_id)
INNER JOIN locations l
ON (l.location_id = d.location_id)
WHERE e.employee_id = p_id);
CURSOR c_manager IS
(SELECT e1.last_name
FROM employees e1
INNER JOIN
employees e2
ON (e1.employee_id = e2.manager_id)
WHERE e2.employee_id = p_id);
CURSOR c_department_name IS
(SELECT department_name
FROM employees e
INNER JOIN
departments d
ON (e.department_id = d.department_id)
WHERE e.employee_id = p_id);
/*-----------------------------------------------------------------------------*/
v_annual_sal NUMBER(9,2);
v_monthly_sal NUMBER(9,2);
v_last_name VARCHAR2(10);
v_deptno NUMBER(3);
v_length NUMBER(2);
v_tenure NUMBER(5);
v_job_id VARCHAR2(20);
v_hire_date DATE;
v_city VARCHAR(25);
v_commission_pct NUMBER(2,2);
v_phone_number VARCHAR2(20);
v_manager VARCHAR2(20);
v_comm_calc NUMBER(10,2);
v_email VARCHAR2(10);
v_department VARCHAR2(20);
v_count NUMBER(4);
v_old_salary NUMBER(9,2);
v_new_salary NUMBER(9,2);
v_lname VARCHAR2(10);
v_phone_number_format VARCHAR2(25);
v_phone_number_length NUMBER(3);
v_tax NUMBER(8,4);
v_sum_sal_departments NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('Welcome to the summary of an employee based on his unique id');
DBMS_OUTPUT.PUT_LINE('============================================================');
/*-----------------------------------------------------------------------------*/
SELECT salary, last_name, department_id,
TRUNC(MONTHS_BETWEEN(SYSDATE,hire_date),0), job_id,
hire_date, commission_pct, phone_number, email, LENGTH(phone_number)
INTO v_monthly_sal, v_last_name, v_deptno,
v_tenure, v_job_id, v_hire_date, v_commission_pct,
v_phone_number, v_email, v_phone_number_length
FROM employees
WHERE employee_id = p_id;
/*-----------------------------------------------------------------------------*/
v_count := SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE(v_count||' Employee found...');
/*-----------------------------------------------------------------------------*/
v_annual_sal := v_monthly_sal * 12;
v_length := LENGTH(v_last_name);
DBMS_OUTPUT.PUT_LINE('Employee:-> ' || v_last_name || ' ,and his name contains: ' || v_length ||' chars');
DBMS_OUTPUT.PUT_LINE(q'[Belong's to department: ]' || v_deptno);
/*-----------------------------------------------------------------------------*/
IF (v_monthly_sal < v_annual_sal)
THEN
DBMS_OUTPUT.PUT_LINE('Has a annual salary of:-> ' || v_annual_sal);
ELSE
DBMS_OUTPUT.PUT_LINE('Something wrong in the formula!');
END IF;
/*-------------------------------------------------------------------------------*/
IF v_commission_pct IS NULL
THEN
DBMS_OUTPUT.PUT_LINE('No Commission added to the annual salary!');
ELSE
DBMS_OUTPUT.PUT_LINE('Commission percentage to the salary is:-> '|| v_commission_pct ||'%');
v_comm_calc := (v_annual_sal * v_commission_pct) + v_annual_sal;
DBMS_OUTPUT.PUT_LINE('And calculated with annual salary is:-> ' ||v_comm_calc);
END IF;
/*-------------------------------------------------------------------------------*/
DBMS_OUTPUT.PUT_LINE('Working for:-> '|| v_tenure || ' months as '|| v_job_id);
DBMS_OUTPUT.PUT_LINE('Started in:-> '|| v_hire_date);
/*-------------------------------------------------------------------------------*/
IF v_phone_number_length = 12
THEN
v_phone_number_format := '(' || SUBSTR(v_phone_number,1,3) || ')' ||
'-' || SUBSTR(v_phone_number,5,3) ||
'-' || SUBSTR(v_phone_number,9,4);
DBMS_OUTPUT.PUT_LINE('Phone number:-> '|| v_phone_number_format);
ELSIF v_phone_number_length = 18
THEN
v_phone_number_format := '(' || SUBSTR(v_phone_number,1,3) || ')' ||
'-' || SUBSTR(v_phone_number,5,2) ||
'-' || SUBSTR(v_phone_number,8,4) || '-'
|| SUBSTR(v_phone_number,13,6);
DBMS_OUTPUT.PUT_LINE('Phone number:-> '|| v_phone_number_format);
ELSE
DBMS_OUTPUT.PUT_LINE('Phone number digits not in range, check the length of the phone numbers from the table');
END IF;
/*-------------------------------------------------------------------------------*/
DBMS_OUTPUT.PUT_LINE('Email:-> '||v_email);
/*-------------------------------------------------------------------------------*/
OPEN c_city;
FETCH c_city
INTO v_city;
IF c_city%FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Location:-> '||v_city);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee location unknown');
END IF;
CLOSE c_city;
/*-------------------------------------------------------------------------------*/
OPEN c_manager;
FETCH c_manager
INTO v_manager;
IF c_manager%FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Is in the eyes of manager:-> '||v_manager);
ELSE
DBMS_OUTPUT.PUT_LINE('Slave '||v_last_name||' is free!');
END IF;
CLOSE c_manager;
/*-------------------------------------------------------------------------------*/
OPEN c_department_name;
FETCH c_department_name
INTO v_department;
IF c_department_name%FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Department Name:-> '||v_department);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee ' ||v_last_name||' belongs to no department!');
END IF;
/*--------------------------------------------------------------------------------*/
DBMS_OUTPUT.PUT_LINE('Checking the current employee with id:-> '|| p_id ||'..');
IF (check_sal2(p_id) IS NULL)
THEN
DBMS_OUTPUT.PUT_LINE('The function returned NULL due to exception, therefore employee does not exist!');
ELSIF (check_sal2(p_id))
THEN
DBMS_OUTPUT.PUT_LINE('Employees salary > average of department '||v_deptno||' where he belongs.');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary < average of department '||v_deptno||', where he belongs.');
END IF;
/*--------------------------------------------------------------------------------*/
SELECT salary
INTO v_old_salary
FROM employees
WHERE employee_id = p_id;
DBMS_OUTPUT.PUT_LINE('Before the raise of ' || p_percent ||
' %, the salary was:-> '|| v_old_salary);
/*--------------------------------------------------------------------------------*/
IF (p_percent > 0.01)
THEN
DBMS_OUTPUT.PUT_LINE('Maximum increase allowance for the moment is only 0.01 %, thus no increase in salary is made');
ELSIF (p_percent < 0.01)
THEN
DBMS_OUTPUT.PUT_LINE('Minimum percent allowance for the moment is only 0.01 %, thus no increase in salary is made');
ELSE
UPDATE employees
SET salary = salary * (1 + p_percent/100)
WHERE employee_id = p_id;
SELECT last_name, salary
INTO v_lname, v_new_salary
FROM employees
WHERE employee_id = p_id;
DBMS_OUTPUT.PUT_LINE('After the raise of ' || p_percent ||
' %, the salary is:-> '|| v_new_salary);
END IF;
/*--------------------------------------------------------------------------------*/
DBMS_OUTPUT.PUT_LINE('===========================================================');
DBMS_OUTPUT.PUT_LINE('===========================================================');
DBMS_OUTPUT.PUT_LINE('===========================================================');
FOR i IN (SELECT SUM(salary) AS "SUMY", department_id
FROM employees
GROUP BY department_id)
LOOP
IF i.department_id IS NULL
THEN
DBMS_OUTPUT.PUT_LINE('Unknown department '|| i.department_id ||' earns:-> '|| i.sumy);
ELSE
DBMS_OUTPUT.PUT_LINE('Department '|| i.department_id ||' earns:-> '|| i.sumy);
END IF;
END LOOP;
FOR j IN (SELECT SUM(sumy) AS "DEP_SUM"
FROM (SELECT SUM(salary) AS "SUMY"
FROM employees
GROUP BY department_id))
LOOP
v_sum_sal_departments := j.dep_sum;
DBMS_OUTPUT.PUT_LINE('Total income on all departments:-> '|| j.dep_sum);
END LOOP;
SELECT taxes_pkg.tax(salary)
INTO v_tax
FROM employees
WHERE employee_id = p_id;
DBMS_OUTPUT.PUT_LINE('Salary after 0.08 % tax withdrawal:-> '|| v_tax);
FOR k IN (SELECT last_name
FROM employees
WHERE salary = (SELECT MAX(salary)
FROM employees))
LOOP
DBMS_OUTPUT.PUT_LINE('Employee with the highest salary is:-> '||k.last_name);
END LOOP;
FOR k IN (SELECT last_name, hire_date
FROM employees
WHERE hire_date = (SELECT MAX(hire_date)
FROM employees))
LOOP
DBMS_OUTPUT.PUT_LINE('Our newest employees are:-> ' || k.last_name);
END LOOP;
FOR m IN (SELECT last_name
FROM employees
WHERE hire_date = (SELECT MIN(hire_date)
FROM employees))
LOOP
DBMS_OUTPUT.PUT_LINE('Our oldest employees are:-> '||m.last_name);
END LOOP;
/*--------------------------------------------------------------------------------*/
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Employee with id:-> ' || p_id || ' does not exist, check data from your tables!');
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE('Unknown propagation');
END employee_analysis;
END my_first_package;
I seached about something called utl_file package for exporting data from oracle database and so on, but i didn't understand much and didn't found what i was looking for. Basically what i want is, how can i "export" all of the output from the screen after running this useless package above? Is there a way to modify the package add features and stuff to it? It is possible? Thanks...

Resources