キーワードに基づいて完全なデータベース検索を構築しようとしていますか?次のコードを使用して、テーブル名、テーブル列名、および行数を取得できます。ただし、同じコード内で行データを取得することはできません。検索結果に基づいて行データを取得できます。
--Set output size
SET serveroutput ON size 1000000
DECLARE
-- var table Name for cursor loop.
t_c1_tname user_tab_columns.table_name%TYPE;
-- var column name for dynamic sql statement.
t_c1_cname user_tab_columns.column_name%TYPE;
-- var string for dynamic sql statement.
t_command VARCHAR2(200);
-- var for your search key word.
l_str varchar2(20) := '%test%';
-- var for dynamic cursor.
t_cid INTEGER;
-- var for total row counts.
t_total_records NUMBER(10);
-- var for stat of executing dynamic sql statement.
stat INTEGER;
--var for each loop row counts.
row_count INTEGER;
-- var for minimum search result, here I set value = 0;
t_limit INTEGER := 0; -- Only show tables with more rows
-- cursor gets all table name, column name.
CURSOR c1 IS select table_name, column_name
from user_tab_columns
where data_type in ( 'VARCHAR2' , 'VARCHAR', 'CHAR' );
BEGIN
t_limit := 0;
OPEN c1;
LOOP
FETCH c1 INTO t_c1_tname,t_c1_cname;
EXIT WHEN c1%NOTFOUND;
-- Here create dynamic sql statement.
t_command := 'SELECT COUNT(0) FROM '||t_c1_tname || ' where ' || t_c1_cname ||' like '''|| l_str||'''';
t_cid := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(t_cid,t_command,DBMS_SQL.native);
DBMS_SQL.DEFINE_COLUMN(t_cid,1,t_total_records);
-- Here execute dynamic sql statement.
stat := DBMS_SQL.EXECUTE(t_cid);
row_count := DBMS_SQL.FETCH_ROWS(t_cid);
-- Here get total row counts for each loop.
DBMS_SQL.COLUMN_VALUE(t_cid,1,t_total_records);
IF t_total_records > t_limit THEN
--Here output results
DBMS_OUTPUT.PUT_LINE(RPAD(t_c1_tname,55,' ')||RPAD(t_c1_cname,55,' ')||
TO_CHAR(t_total_records,'99999999')||' record(s)');
-- here you can insert results into your table.
--INSERT INTO search_db_results VALUES (t_c1_tname,t_c1_cname,t_total_records);
END IF;
DBMS_SQL.CLOSE_CURSOR(t_cid);
END LOOP;
CLOSE c1;
-- COMMIT if you have any insert statement.
-- COMMIT;
END;
/