Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch function, SQL Server
    primarykey
    data
    text
    <p>I am creating a small search function on my site, this enables the search for articles in the system. Each article has a set of keywords associated to it and these keywords are stored inside a SQL Server database.</p> <p>This is the table:</p> <pre><code>CREATE TABLE [dbo].[SearchWords] ( [ID] [int] IDENTITY(1,1) NOT NULL, [ArticleID] [int] NOT NULL, [SearchWord] [nvarchar](20) NOT NULL, CONSTRAINT [PK_SearchWords] PRIMARY KEY CLUSTERED ([ID] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] </code></pre> <p>Each article can have an unlimited amount of keywords. Now my problem is the search itself. When for example the user types:</p> <p>France actors</p> <p>I want the system to find all articles (just once DISTINCT) with the keywords France and actors. I am passing the search criteria as a varchar (separated with space) to the stored procedure. Then i am splitting the words up with the following function: (Erland Sommarskog) <a href="http://www.sommarskog.se/arrays-in-sql-2005.html#iter-list-of-strings" rel="nofollow noreferrer">http://www.sommarskog.se/arrays-in-sql-2005.html#iter-list-of-strings</a></p> <p>How do i then match the criteria words against the search words and only get the distinct article ids?</p> <p>I am working with something like this, just that i cant understand how to match all the keywords. This method works if just one keyword is entered. If the user enters multiple, then it doesn't return anything even if the article has all involved keywords.</p> <pre><code>declare @temp nvarchar(50) set @temp = 'France actors' SELECT DISTINCT Article.ArticleID FROM Article INNER JOIN SearchWords ON Article.ArticleID = SearchWords.ArticleID JOIN iter_charlist_to_tbl(@temp, DEFAULT) s ON SearchWords.SearchWord = s.nstr </code></pre> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload