Link to home
Start Free TrialLog in
Avatar of pwang1973
pwang1973

asked on

A utlity to search all fields in all tables?

Does anyone know how to do this?
Avatar of catchmeifuwant
catchmeifuwant

In Oracle you can do something like in sqlplus:

set serveroutput on size 100000


declare
v_search varchar2(100);
v_count number(5);
v_sql varchar2(1000);

begin
v_search := 'SCOTT';  --- Set the search string

for rec in (select table_name,column_name from user_tab_columns )
loop
v_sql := 'select count(*) from '||rec.table_name ||' where '||rec.column_name||'='''||v_search||' ''';

execute immediate v_sql into v_count;

if v_count > 0 then
dbms_output.put_line('Table :'||rec.table_name||' and Column :'||rec.column_name||'-contains '||v_search);
end if;

end loop;
end;
/
Avatar of pwang1973

ASKER

I am using Microsoft SQL server, it seems that MIcrosoft provides a utility to do this.

THanks
Pat
' Try this:

' Copy this code in the code event of a command button named 'Command0' and execute it.
' You will be prompted to enter the text to search for
'
' Please not that you need a reference to DAO for the code to work. Point to
' Tools, References in the VBA module and then choose Microsoft DAO 3.6/3.51 Object Library
'
' Cheers,
' Starman75

Private Sub Command0_Click()
  Dim tdf As TableDef
  Dim fld As Field
  Dim sSearch As String
  Dim rs As Recordset
  Dim db As Database
  Dim bFound As Boolean
 
  bFound = False
  Set db = CurrentDb
  sSearch = InputBox("Enter string to search for")
  For Each tdf In CurrentDb.TableDefs
    If Left(tdf.Name, 4) <> "MSYS" Then  ' IGNORE SYSTEM TABLES
      Set rs = db.OpenRecordset(tdf.Name)
      If rs.RecordCount > 0 Then  ' make sure that the table has records in it
        rs.MoveFirst
        Do Until rs.EOF
          For Each fld In tdf.Fields
            If rs.Fields(fld.Name) = sSearch Then
              MsgBox sSearch & " found in table " & tdf.Name & " in field " & fld.Name  ' found the string
              bFound = True
            End If
          Next
          rs.MoveNext
        Loop
      End If
    End If
  Next
 
  If bFound = True Then
    MsgBox "Finished. String Found"
  Else
    MsgBox "Finished. String not Found"
  End If
 
   
End Sub
I think I mean Microsoft Site Server Catelog search functionality.
ASKER CERTIFIED SOLUTION
Avatar of DanSivan
DanSivan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial