SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to search all columns of all tables in a database for a keyword

    Dear Team,

    i have an requirement that : i want to search all the columns of all the tables in the particular database based on the specific key word or an free text.

    example :

    table 1: columns data
    empname sam
    empid 01

    table 2 columns data
    deptname sam
    departmentid 10

    table 3 columns data
    organization name sam
    organization id 1

    when i search for text " SAM"

    it should search me from the entire database, all tables and columns of it and display the result

    output : tablename cloumn value
    table1 empname sam
    table2 deptname sam
    table3 organizationame sam

    the example is just an sample not the real data .

    please help me with sample code or any link related to it .


    thanks in advance

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use a select union
    Code:
    select id,'column_x_table1',column_x
    from table1
    where name like 'pattern%'
    union
    select id,'column_y_table2',column_y
    from table2 
    where name like 'pattern%'
    union
    select id,'column_z_table3',column_z
    from table3
    where name like 'pattern%'
    ....

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,015
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    i'm fairly sure you do not want to search in numeric or date columns for a character string

    here is an example of how to do it in SQL Server:
    http://vyaskn.tripod.com/search_all_...all_tables.htm
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •