Search code examples
sqlsql-serversql-server-2008sql-server-2005t-sql

Apply like over all columns without specifying all column names?


I've found myself in a position where I'm working an unfamiliar database that has a vast number of columns to each table. I have an idea of what data I'm looking for but I don't know what column it resides in and need to use like in order to locate the exact data that I need (and have to repeat this task for multiple sets of data).

Is there a way to apply like over a cartesian select?

The following should explain what I'd like to do a bit better (even though it's syntactically ridiculous):

select 
    *
from    
    a_table
where   
    * like '%x%'

edit:

Note that I'm not intending on using a cartesion select in any reports - it's purposes here would be to help me to identify the relevant columns that I would need to put into my queries and to help me gain familiarity with the database.


Solution

  • Generally - its not possible in reasonable way (without digging in DB metadata), but if you know the names of columns, you may use trick like this:

    select 
        YourTable.*
    FROM YourTable
    JOIN
    ( 
        select 
          id, 
          ISNULL(column1,'')+ISNULL(Column2,'')+...+ISNULL(ColumnN,'') concatenated
          FROM YourTable
    ) T ON T.Id = YourTable.Id
    where   t.concatenated like '%x%'
    

    OR

    if you search for words - use the FTS capabilities, because the upper query is a performance killer