0

For Sql Server 2014, what syntax do I need, if this is even possible? (in pseudo-code)

DECLARE @searchstring nvarchar(20)
LOOP @searchstringstring = (SELECT keyword FROM table1)
SELECT column FROM table2 where column LIKE '%@searchstring%'
END LOOP

I want it to return all columns in a single table.

3
  • 2
    What are you trying to achieve here? Maybe it can be done using a set-based solution. Commented Apr 26, 2016 at 4:24
  • you can loop through while clause or cursor in sql server. but it is not a recommended approach Commented Apr 26, 2016 at 4:48
  • its not clear what you are trying to achieve,some example will help Commented Apr 26, 2016 at 4:57

1 Answer 1

1

Unless I'm missing something, you want to select all the values in table2.Column that contains the text in table2.Keyword. This can be done easily with a simple inner join:

SELECT t2.column
FROM table2 t2
INNER JOIN table1 t1 ON(t2.column LIKE '%'+ t1.keyword +'%'

Sql works best with set based operations. looping is rarely the desired approach.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.