Rechercher dans une base oracle un nombre (Search_Valeur), il est possible de filtrer les colonnes en renseignant Search_Colonne.
/* VERSION VALEUR NUMERIQUE */
SET SERVER OUTPUT ON
SET SERVEROUTPUT ON
DECLARE
TYPE curtype
IS
REF
CURSOR;
Curseur_Table curtype;
Curseur_Colonne curtype;
Curseur_Search curtype;
Counter NUMBER:=0;
RequeteSQL_Colonne VARCHAR2(200);
RequeteSQL_Table VARCHAR2(500);
RequeteSQL_Searching VARCHAR2(2048);
Search_Table VARCHAR (100):= '';
Search_Colonne VARCHAR (100):= '';
Nom_Table VARCHAR (200);
Nom_Colonne VARCHAR2(200);
Search_Valeur VARCHAR (200):='126980';
BEGIN
RequeteSQL_Table:='SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME LIKE ''%'||Search_Table||'%''';
-- dbms_output.put_line(RequeteSQL_Table);
OPEN Curseur_Table FOR RequeteSQL_Table;
LOOP
FETCH Curseur_Table INTO Nom_Table;
EXIT
WHEN Curseur_Table%notfound;
RequeteSQL_Colonne:='SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='''||Nom_Table||''' AND DATA_TYPE IN (''NUMBER'') AND COLUMN_NAME LIKE ''%'|| Search_Colonne ||'%''';
-- dbms_output.put_line(RequeteSQL_Colonne);
OPEN Curseur_Colonne FOR RequeteSQL_Colonne;
LOOP
FETCH Curseur_Colonne INTO Nom_Colonne;
EXIT
WHEN Curseur_Colonne%notfound;
RequeteSQL_Searching :='SELECT count(*) FROM "'||Nom_Table||'" WHERE "'||Nom_Colonne||'"=' || Search_Valeur;
-- dbms_output.put_line(RequeteSQL_Searching);
EXECUTE IMMEDIATE RequeteSQL_Searching INTO Counter;
IF Counter > 0 THEN
dbms_output.put_line(''|| Search_Valeur ||' trouvé '||Counter || ' fois dans "'|| Nom_Table ||'"."'|| Nom_Colonne ||'" SELECT * FROM ' || Nom_Table || ' WHERE ' || Nom_Colonne || ' = ' || Search_Valeur || ';' );
END IF;
END LOOP;
CLOSE Curseur_Colonne;
END LOOP;
CLOSE Curseur_Table;
END;
Rechercher dans une base oracle une chaine (Search_String), il est possible de filtrer les colonnes en renseignant Search_Colonne.
/* VERSION CHAINE DE CARACTERES */
SET SERVEROUTPUT ON
SET SERVER OUTPUT ON
DECLARE
TYPE curtype
IS
REF
CURSOR;
Curseur_Table curtype;
Curseur_Colonne curtype;
Curseur_Search curtype;
Counter NUMBER:=0;
RequeteSQL_Colonne VARCHAR2(200);
RequeteSQL_Table VARCHAR2(500);
RequeteSQL_Searching VARCHAR2(2048);
Search_String VARCHAR (100) := '' ;
Search_Table VARCHAR (100) := '' ;
Nom_Table VARCHAR (200) ;
Nom_Colonne VARCHAR2(200);
BEGIN
RequeteSQL_Table:='SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME LIKE ''%'||Search_Table||'%''';
-- dbms_output.put_line(RequeteSQL_Table);
OPEN Curseur_Table FOR RequeteSQL_Table;
LOOP
FETCH Curseur_Table INTO Nom_Table;
EXIT
WHEN Curseur_Table%notfound;
RequeteSQL_Colonne:='SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='''||Nom_Table||''' AND DATA_TYPE IN (''VARCHAR2'',''NVARCHAR2'')';
-- dbms_output.put_line(RequeteSQL_Colonne);
OPEN Curseur_Colonne FOR RequeteSQL_Colonne;
LOOP
FETCH Curseur_Colonne INTO Nom_Colonne;
EXIT
WHEN Curseur_Colonne%notfound;
RequeteSQL_Searching :='SELECT count(*) FROM "'||Nom_Table||'" WHERE "'||Nom_Colonne||'" LIKE ''%'||Search_String||'%''';
-- dbms_output.put_line(RequeteSQL_Searching);
EXECUTE IMMEDIATE RequeteSQL_Searching INTO Counter;
IF Counter > 0 THEN
dbms_output.put_line(''''||Search_String ||''' trouvé '||Counter || ' fois dans "'|| Nom_Table ||'"."'|| Nom_Colonne ||'"');
END IF;
END LOOP;
CLOSE Curseur_Colonne;
END LOOP;
CLOSE Curseur_Table;
END;