<p>Can/Should I use a LIKE criteria as part of an INNER JOIN when building a stored procedure/query? I'm not sure I'm asking the right thing, so let me explain.</p>

<p>I'm creating a procedure that is going to take a list of keywords to be searched for in a column that contains text. If I was sitting at the console, I'd execute it as such:</p>

<pre><code>SELECT Id, Name, Description
  FROM dbo.Card
 WHERE Description LIKE '%warrior%' 
       OR
       Description LIKE '%fiend%' 
       OR 
       Description LIKE '%damage%'
</code></pre>

<p>But a trick I picked up a little while go to do "strongly typed" list parsing in a stored procedure is to parse the list into a table variable/temporary table, converting it to the proper type and then doing an INNER JOIN against that table in my final result set. This works great when sending say a list of integer IDs to the procedure. I wind up having a final query that looks like this:</p>

<pre><code>SELECT Id, Name, Description
  FROM dbo.Card
       INNER JOIN @tblExclusiveCard ON dbo.Card.Id = @tblExclusiveCard.CardId
</code></pre>

<p>I want to use this trick with a list of strings. But since I'm looking for a particular keyword, I am going to use the LIKE clause. So ideally I'm thinking I'd have my final query look like this:</p>

<pre><code>SELECT Id, Name, Description
  FROM dbo.Card
       INNER JOIN @tblKeyword ON dbo.Card.Description LIKE '%' + @tblKeyword.Value + '%'
</code></pre>

<p>Is this possible/recommended?</p>

<p>Is there a better way to do something like this?</p>

<hr>

<p>The reason I'm putting wildcards on both ends of the clause is because there are "archfiend", "beast-warrior", "direct-damage" and "battle-damage" terms that are used in the card texts.</p>

<p>I'm getting the impression that depending on the performance, I can either use the query I specified or use a full-text keyword search to accomplish the same task? </p>

<p>Other than having the server do a text index on the fields I want to text search, is there anything else I need to do?</p>

[{'answer_id': 20513, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 1, 'selected': False, 'text': '<p>It seems like you are looking for full-text search.  Because you want to query a set of keywords against the card description and find any hits?  Correct?</p>\n'}, {'answer_id': 20515, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 1, 'selected': False, 'text': "<p>Personally, I have done it before, and it has worked out well for me. The only issues i could see is possibly issues with an unindexed column, but i think you would have the same issue with a where clause.</p>\n\n<p>My advice to you is just look at the execution plans between the two. I'm sure that it will differ which one is better depending on the situation, just like all good programming problems.</p>\n"}, {'answer_id': 20531, 'author': 'Chris Miller', 'author_id': 206, 'author_profile': 'https://Stackoverflow.com/users/206', 'pm_score': 0, 'selected': False, 'text': '<p>Performance will be depend on the actual server than you use, and on the schema of the data, and the amount of data.  With current versions of MS SQL Server, that query should run just fine (MS SQL Server 7.0 had issues with that syntax, but <a href="http://support.microsoft.com/kb/225093" rel="nofollow noreferrer">it was addressed in SP2</a>).</p>\n\n<p>Have you run that code through a profiler?  If the performance is fast enough and the data has the appropriate indexes in place, you should be all set.</p>\n'}, {'answer_id': 20538, 'author': 'jason saldo', 'author_id': 1293, 'author_profile': 'https://Stackoverflow.com/users/1293', 'pm_score': 4, 'selected': True, 'text': '<p>Your first query will work but will require a full table scan because any index on that column will be ignored.   You will also have to do some dynamic SQL to generate all your LIKE clauses.</p>\n\n<p>Try a full text search if your using SQL Server or check out one of the <a href="http://lucene.apache.org/java/docs/index.html" rel="noreferrer">Lucene</a> implementations.  Joel talked about his success with it recently.</p>\n'}, {'answer_id': 20544, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 0, 'selected': False, 'text': "<p>LIKE '%fiend%' will never use an seek, LIKE 'fiend%' will. Simply a wildcard search is not sargable</p>\n"}, {'answer_id': 20599, 'author': 'jason saldo', 'author_id': 1293, 'author_profile': 'https://Stackoverflow.com/users/1293', 'pm_score': 1, 'selected': False, 'text': '<p>@Dillie-O<br>\nHow big is this table?<br>\nWhat is the data type of Description field?  </p>\n\n<p>If either are small a full text search will be overkill.</p>\n\n<p>@Dillie-O<br>\nMaybe not the answer you where looking for but I would advocate a schema change...</p>\n\n<p>proposed schema:</p>\n\n<pre><code>create table name(\n    nameID identity / int\n   ,name varchar(50))\n\ncreate table description(\n    descID identity / int\n   ,desc varchar(50)) --something reasonable and to make the most of it alwase lower case your values\n\ncreate table nameDescJunc(\n    nameID  int\n    ,descID int)\n</code></pre>\n\n<p>This will let you use index\'s without have to implement a bolt on solution, and keeps your data atomic.</p>\n\n<p>related: <a href="https://stackoverflow.com/questions/20856">Recommended SQL database design for tags or tagging</a></p>\n'}, {'answer_id': 810683, 'author': 'onedaywhen', 'author_id': 15354, 'author_profile': 'https://Stackoverflow.com/users/15354', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>a trick I picked up a little while go\n  to do "strongly typed" list parsing in\n  a stored procedure is to parse the\n  list into a table variable/temporary\n  table</p>\n</blockquote>\n\n<p>I think what you might be alluding to here is to put the keywords to include into a table then use <a href="http://www.dbazine.com/ofinterest/oi-articles/celko1" rel="nofollow noreferrer">relational division</a> to find matches (could also use another table for words to exclude). For a worked example in SQL see <a href="http://www.dbazine.com/ofinterest/oi-articles/celko9" rel="nofollow noreferrer">Keyword Searches by Joe Celko</a>.</p>\n'}, {'answer_id': 891792, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Try this;</p>\n\n<pre><code>SELECT Id, Name, Description\nFROM dbo.Card\nINNER JOIN @tblKeyword ON dbo.Card.Description LIKE '%' + \n                                CONCAT(CONCAT('%',@tblKeyword.Value),'%') + '%'\n</code></pre>\n"}, {'answer_id': 14590796, 'author': 'John', 'author_id': 1223661, 'author_profile': 'https://Stackoverflow.com/users/1223661', 'pm_score': 3, 'selected': False, 'text': "<p>Try this</p>\n\n<pre><code>    select * from Table_1 a\n    left join Table_2 b on b.type LIKE '%' + a.type + '%'\n</code></pre>\n\n<p>This practice is not ideal. Use with caution.</p>\n"}, {'answer_id': 30483791, 'author': 'Manoj', 'author_id': 4944899, 'author_profile': 'https://Stackoverflow.com/users/4944899', 'pm_score': 2, 'selected': False, 'text': "<p>try it...</p>\n\n<pre><code>select * from table11 a inner join  table2 b on b.id like (select '%'+a.id+'%') where a.city='abc'.\n</code></pre>\n\n<p>Its works for me.:-)</p>\n"}]
['https://Stackoverflow.com/questions/20484', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/71/']