暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 如何将结果插入表格而不是屏幕(put_line)。

askTom 2021-08-22
375

问题描述

Hi ,我有一个脚本,它扫描架构中的所有表和列,以查找包含'%@%'
我把结果拿回来。
如何将其插入到创建的表中?

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;
/
复制



专家解答

简单的加法就可以了。

创建表结果,作为从用户标签列1=0的位置选择table_name、列名;

然后在那里你有

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


添加插入,使其看起来像

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


如果希望看到每个结果都是找到的,请在结束循环之前添加提交。

文章转载自askTom,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论