Skip to Main Content
  • Questions
  • How to insert the results into a table instead of to the screen (put_line).

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Hili.

Asked: August 22, 2021 - 6:53 am UTC

Last updated: August 26, 2021 - 1:52 am UTC

Version: 19.3

Viewed 1000+ times

You Asked

Hi I have this script which scan all tables and coluns in schema to find any column that includes '%@%'
I get back the result to the scree.
How I can insert it into a table I created?

SQL> desc masking_emails
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 TABLE_NAME                                NOT NULL VARCHAR2(100)
 COLUMN_NAME                               NOT NULL VARCHAR2(100)
 NULABLE                                            VARCHAR2(1)
 DATETIME                                           DATE


the script:

create or replace procedure search_schema( p_string in varchar2 )
authid current_user
as
l_cols long;
l_where long;
l_cursor sys_refcursor;
l_cname varchar2(4000);

begin
dbms_application_info.set_client_info( '%' || p_string || '%' );
for x in ( select table_name from user_tables )
loop
l_cols := 'case when 1=0 then ''x'' ';
l_where := ' where ( 1=0 ';
for y in ( select '"' || column_name || '"' column_name
from user_tab_columns
where table_name = upper(x.table_name)
and (data_type in ( 'VARCHAR2' ) and DATA_LENGTH>6
 )
)
loop
l_cols := l_cols || ' when ' || y.column_name ||
' like sys_context(''userenv'',''client_info'') then ' ||
' ''' || y.column_name || '''';
l_where := l_where || ' or ' || y.column_name || ' like sys_context(''userenv'',''client_info'') ';

end loop;
open l_cursor for 'select ' || l_cols || 'else null end cname from ' ||
x.table_name || l_where || ') and rownum=1';
fetch l_cursor into l_cname;
if ( l_cursor%found  )
then
 dbms_output.put_line( x.table_name || ' ' || l_cname );
end if;
close l_cursor;
end loop;
end;
/



and Connor said...

Just a simple addition will do the trick.

create table results as select table_name, column_name from user_tab_columns where 1=0;

and then where you have

dbms_output.put_line( x.table_name || ' ' || l_cname );


add an insert, so it looks like

dbms_output.put_line( x.table_name || ' ' || l_cname );
insert into results values (x.table_name, l_cname);


Add a commit before the end loop if you want to see each result as its found.

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Hili, August 23, 2021 - 5:34 am UTC

Hi, thank you very much, that works!
I have another 2 questions for you for this procedure:

1. I tried also to get the info if the column is nullable or not and save it to results table.. How I can add it in the procedure itself?

2. I want to exclude any column that contains more than 20% rows of not null but not including '%@%' too. (meaning this column has few rows that including '%@% but it's not an email column)

Thanks in advanced.

Connor McDonald
August 26, 2021 - 1:52 am UTC

1) In

select '"' || column_name || '"' column_name

add the NULLABLE so you can reference it later in the insert

2) You'll need to have counts rather than simple existence, so

case when col like ... then 1 else 0 end

becomes

count(case when col like ... then 1 else 0 end)

the main query also would have a count(*) so that you can then compare your count versys total rows in the table

Hili, August 26, 2021 - 1:04 am UTC

Hi it's very important part of my script please see if you can solve it :

2. I want to exclude any column that contains more than 20% rows of not null but not including '%@%' too. (meaning this column has few rows that including '%@% but it's not an email column)

Hili, August 26, 2021 - 2:40 am UTC


Hi, thank you for the answers.
I'm not so familiar with write procedures.
Would you be able to show me how to put this peace of code in the procedure itself? (I talk about #2)

A reader, August 29, 2021 - 2:58 pm UTC

Hi any update?

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library