Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query for text search

Author  Topic 

DR_J
Starting Member

1 Post

Posted - 2012-05-02 : 22:34:58
Hey guys... SQL query question: I need to construct a query that will search several columns in a table from several words entered by a user in a textbox. This is handled by a webservice. The table is called Products, and the text columns are: ProductCategory, ProductBrand, ProductModel and ProductDetails. If a user types: 'maxtor hard dr', the query results must include any and all records that have a combination of partial or full matches to any of those requested search words.
Thanks!

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-05-03 : 08:33:50
This can be done. You'll have to Split the input string that the user inputs into words and store them in variables at the front end.

An important fact here is that you can't decide beforehand how many words are there in the string and how many variables have to be created so you'll have to do it dynamically.
Then you need to pass these variables to SQL server where you can use search the table like:


Select * From <TableName> Where <ColumnName> LIKE '%@tempvariable%'


You can do this for one word at a time and store the results to a temporary table in which the records found for every word can be appended to the previously found records in the same table. After this you can Select all the records from the table to your frontend application.
In SQL Server you can use a Table Valued Function to hold the code for doing this or else you can do all the above using a Stored procedure which Selects and displays the resultset.
I personally prefer using an SP.

Hope this helps you.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page
   

- Advertisement -