Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all I need a help to write a SP which searches from any data of the table means i have one table which contains 100 columns i want to search based on any field of these column.. wat should i specify in where clause..

Thanks
Posted

While searching for similar solution i found this link.
The function can search any table in database.you need to customise it ad per your requirement.

http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm[^]

Hope this helps.
 
Share this answer
 
There are no smart way of doing this. You have 100 colmuns and the only way is to build a sql-query dynamically and use a LIKE to search for text. (if all columns are text?)

You need to achive:

SQL
SELECT * FROM Table WHERE
         Column1 LIKE '%query%'
      OR Column2 LIKE '%query%'
      OR Column3 LIKE '%query%'
and so on


You could query some system tables to list all columns and iterate through them and generate the sql you need, but that would be nasty.

To search in sql you need to set up some indexing for performance and there's a lot of things to think about before firing a query to a large database.

My tips is to either look for some 3rd party search engine tools like Lucene.net or similar. Or do the job writing the query by hand.

A third option is to generate a second table with 2 columns (id and text) holding the values from all the columns in the table you want to search, and then search in that column.
A few downsides is that it will take up some space in your database and you can't weight score for each column.

SQL
CREATE TABLE SearchTable(
  [Id] [int] NOT NULL,
  [Text] [varchar](max) NOT NULL -- you should index this column
) 

And then copy data from your first table:
SQL
INSERT INTO SearchTable 
SELECT Id, Column1 + ' ' + Column2 + ' ' + Column3 -- and so on
FROM OriginalTable

Then you can search SearchTable:
SQL
SELECT OriginalTable.* FROM OriginalTable
INNER JOIN SearchTable ON OriginalTable.Id = SearchTable.Id
WHERE SearchTable.Text LIKE '%query%'

This could be done dynamically "on the fly" using temp tables, but if theres a lot of data I would schedule this to run and do indexing at specific times.
 
Share this answer
 
Comments
Espen Harlinn 24-Jul-12 4:34am    
5'ed!
wat should i specify in where clause..
Whatever is the column name and related search text!

SQL
WHERE @ColumnName = @ColumnSearchText
 
Share this answer
 
try to write you sp dynamically..
 
Share this answer
 
use dynamic query for this purpose

look this simple example:-

SQL
ALTER proc [dbo].[usp_SelectTableData]
 @FieldName varchar(50)=null
,@FldType int =0 -- 0 for numeric and 1 = string
,@TableName varchar(50)
,@SearchStr VARCHAR(200) = null
as
begin
declare @Qry nvarchar(max)
if @FldType=0
set @Qry='Select  '+ @FieldName + ' from '  + @TableName +' where  '+ @FieldName +' =' + @SearchStr + ' '
else
set @Qry='Select  '+ @FieldName + ' from '  + @TableName +' where  '+ @FieldName +' =''' + @SearchStr + ''' '

EXEC(@Qry)
end
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900