Tony Hendrix
Tony Hendrix

Reputation: 39

SQL Server loop programming

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.

Upvotes: 0

Views: 82

Answers (1)

Zohar Peled
Zohar Peled

Reputation: 82474

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.

Upvotes: 1

Related Questions