T-SQL query to search table-field for “Keywords”

2019-09-07 19:35发布

问题:

I'm trying to create a search text-box on my website than when submitted will pass the 'string-keyword' into my grid and pull all matching records for that keyword.

I'm having to setup my grid with a where statement that will "listen" for parameters passed in by the form via query-string.

I'm not sure how to approach this. Any insight, tips, examples appreciated.

UPDATE------- This is the query my grid is using to populate records. In addition to City/Region/Country I'm trying to incorporate a keyword Search that would only look at my 'Company' Field inside the BND_Listing table.

select * FROM BND_listing right join BND_ListingCategories
on BND_Listing.CatID=BND_ListingCategories.CatID
where 
 (CategoryName = '[querystring:filter-Category]' or '[querystring:filter-     Category]'='All')
 and
 (City = '[querystring:filter-City]' or '[querystring:filter-City]'='All')
and
(Region= '[querystring:filter-State]' or '[querystring:filter-State]'='All')
and
(Country= '[querystring:filter-Country]' or '[querystring:filter-    Country]'='All')
and
isnull(Company,'') <> ''
ORDER by Company ASC

回答1:

I don't know what you mean by "listen" for parameters, but since you only have TSQL tagged, the matching part in SQL Server is achieved like so:

DECLARE @param VARCHAR(256)   --or what ever size you need
SET @param = 'some text'

SELECT SomeColumns
FROM SomeTable
WHERE aColumn LIKE '%' + @param + '%'

Or using SqlCommand in VB you can check out: https://stackoverflow.com/a/251311/6167855