檢視哪些表的哪些列含有指定字串(如‘andy’存在哪些表的哪些列中)
-- 檢視錶中列含有指定字元。
SQL> select * from demo1;
NAME ID
------------------------------ ----------
???? 4
andy 1
andy1 2
andy2 3
liudehua 3
???? 5
6 rows selected.
SQL> select * from demo2;
NAME ID
------------------------------ ----------
andy 1
andy2 3
andy2 3
liudehua 3
SQL> select * from demo3;
NAME ID
------------------------------ ----------
andy 1
andy2 3
andy2 3
liudehua 3
SQL> set serveroutput on
SQL>DECLARE
v_sql VARCHAR2(4000);
v_tb_column VARCHAR2(4000);
v_cnt NUMBER(18,0);
cursor cur is SELECT 'SELECT '''||'"'||t1.table_name||'"."'||t1.Column_Name||'"'||''''||' as col_name, NVL(COUNT(t."'||t1.Column_Name||'"),0) as cnt FROM "'||
t1.table_name||'" t WHERE t."'||t1.column_name||'" like ''%andy%''' AS str
FROM cols t1 left join user_col_comments t2
on t1.Table_name=t2.Table_name and t1.Column_Name=t2.Column_Name
left join user_tab_comments t3
on t1.Table_name=t3.Table_name
WHERE NOT EXISTS ( SELECT t4.Object_Name FROM User_objects t4
WHERE t4.Object_Type='TABLE'
AND t4.Temporary='Y'
AND t4.Object_Name=t1.Table_Name )
AND (t1.Data_Type='CHAR' or t1.Data_Type='VARCHAR2' or t1.Data_Type='VARCHAR')
-- AND t1.table_name='RUN_STATS'
ORDER BY t1.Table_Name, t1.Column_ID;
BEGIN
FOR i IN cur LOOP
v_sql := i.str; -- 獲取將要執行的SQL語句;
EXECUTE IMMEDIATE v_sql INTO v_tb_COLUMN, v_cnt;
IF v_cnt > 0 THEN
dbms_output.put_line('table'||substr(v_tb_column,1,instr(v_tb_column,'.',1,1)-1)||' col'||substr(v_tb_column,instr(v_tb_column,'.',1,1)+1)||' has string"andy" ');
END IF;
END LOOP;
EXCEPTION WHEN OTHERS THEN
BEGIN
dbms_output.put_line(v_sql);
dbms_output.put_line(v_tb_column);
END;
END;
/
結果輸出:
table"DEMO1" col"NAME" has string"andy"
table"DEMO2" col"NAME" has string"andy"
table"DEMO3" col"NAME" has string"andy"
table"TEAM" col"MENTOR" has string"andy"
SQL> select * from demo1;
NAME ID
------------------------------ ----------
???? 4
andy 1
andy1 2
andy2 3
liudehua 3
???? 5
6 rows selected.
SQL> select * from demo2;
NAME ID
------------------------------ ----------
andy 1
andy2 3
andy2 3
liudehua 3
SQL> select * from demo3;
NAME ID
------------------------------ ----------
andy 1
andy2 3
andy2 3
liudehua 3
SQL> set serveroutput on
SQL>DECLARE
v_sql VARCHAR2(4000);
v_tb_column VARCHAR2(4000);
v_cnt NUMBER(18,0);
cursor cur is SELECT 'SELECT '''||'"'||t1.table_name||'"."'||t1.Column_Name||'"'||''''||' as col_name, NVL(COUNT(t."'||t1.Column_Name||'"),0) as cnt FROM "'||
t1.table_name||'" t WHERE t."'||t1.column_name||'" like ''%andy%''' AS str
FROM cols t1 left join user_col_comments t2
on t1.Table_name=t2.Table_name and t1.Column_Name=t2.Column_Name
left join user_tab_comments t3
on t1.Table_name=t3.Table_name
WHERE NOT EXISTS ( SELECT t4.Object_Name FROM User_objects t4
WHERE t4.Object_Type='TABLE'
AND t4.Temporary='Y'
AND t4.Object_Name=t1.Table_Name )
AND (t1.Data_Type='CHAR' or t1.Data_Type='VARCHAR2' or t1.Data_Type='VARCHAR')
-- AND t1.table_name='RUN_STATS'
ORDER BY t1.Table_Name, t1.Column_ID;
BEGIN
FOR i IN cur LOOP
v_sql := i.str; -- 獲取將要執行的SQL語句;
EXECUTE IMMEDIATE v_sql INTO v_tb_COLUMN, v_cnt;
IF v_cnt > 0 THEN
dbms_output.put_line('table'||substr(v_tb_column,1,instr(v_tb_column,'.',1,1)-1)||' col'||substr(v_tb_column,instr(v_tb_column,'.',1,1)+1)||' has string"andy" ');
END IF;
END LOOP;
EXCEPTION WHEN OTHERS THEN
BEGIN
dbms_output.put_line(v_sql);
dbms_output.put_line(v_tb_column);
END;
END;
/
結果輸出:
table"DEMO1" col"NAME" has string"andy"
table"DEMO2" col"NAME" has string"andy"
table"DEMO3" col"NAME" has string"andy"
table"TEAM" col"MENTOR" has string"andy"
PL/SQL procedure successfully completed.
參考:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31383567/viewspace-2131564/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 檢視oracle資料庫中,哪些表的欄位是null值比較多Oracle資料庫Null
- javascript哪些屬性是可列舉的JavaScript
- MAC快速檢視有哪些技巧?Mac
- 有哪些命令列的軟體堪稱神器?命令列
- 展廳中的全息風扇有哪些表現特徵?特徵
- Mac上有哪些好用的檢視器工具呢?Mac
- Linux檢視檔案的命令有哪些?Linux
- 機房存在哪些安全隱患?需要排查哪些地方?
- 有利潤表模板的BI軟體有哪些?
- Linux中如何檢視開啟了哪些埠?Linux
- go語言中遍歷陣列的方法有哪些Go陣列
- 短視訊的平臺有哪些?哪些平臺適合新手?
- 查詢使用者中哪些表沒主鍵
- Python中的字串切割和拼接方法都有哪些?Python字串
- js查詢指定的陣列中是否含有指定的值JS陣列
- 在Linux系統中如何檢視有哪些組?Linux
- DNS安全問題有哪些?DNS系統存在哪些安全隱患?DNS
- linux 檢視日誌的命令引數有哪些?Linux
- 前端技巧:遍歷陣列都有哪些方式呢?前端陣列
- python有哪些is開頭的字串方法Python字串
- 面試官:你用過哪些JDK自帶的命令列工具面試JDK命令列
- 流程執行期儲存流程變數的表有哪些變數
- Python如何檢視安裝了哪些模組?Python
- 如何使用 Laravel 的佇列機制?有哪些場景需要使用佇列 ?Laravel佇列
- OA系統中的HRM的發展和存在哪些誤區?
- es6新增陣列方法簡便了哪些操作?陣列
- Oracle表移動後應該做哪些事情?Oracle
- java新手視訊看哪些Java
- JavaScript中[]作用有哪些JavaScript
- 如何檢視哪些SQL使用了動態取樣?SQL
- 計算機視覺(AI)的演算法有哪些,具體都有哪些特點?計算機視覺AI演算法
- Sqlserver定位哪些物件和哪些會話哪些sql語句消耗了tempdbSQLServer物件會話
- 大資料分析存在哪些困難大資料
- sql server 修改欄位名,檢視指定表是否存在SQLServer
- Python中的有序序列有哪些Python
- .net core中的哪些過濾器過濾器
- 常見的資料整合有哪些方法?有哪些分類?
- 如何檢視某一表空間的資料檔案都分佈了哪些資料物件薦物件