How to avoid cursor in sql server? also want to avoid while loop
-
I have two tables in my database, 1st one contains a couple of sentences, like 'I like apples and bananas', 2nd one contains key word, like 'apple' & 'orange'. I want to create a sql statements or usp to list all the colomns in the 1st table with the keywords in the 2nd one. How can I achieve that without using cursors? Would anyone have some smart ideas rather than while loop? That'll be great. Thanks, Ling
-
Answer:
See if that's what you need... Option 1 would return a row for each keyword matching the phrase... Option 2 returns a CSV of the keywords that are in the phrase. Note that this uses SQL 2005 related functions. I've made a test case for you... Perhaps would help your explanation of the problem. create table #test(id int identity(1,1), phrase varchar(1000)) create table #kenter code hereeyword(id int identity(1,1), keyword varchar(50)) insert into #test select 'I like apples and bananas' insert into #keyword select 'APPLE' UNION select 'BANANA' UNION select 'RASPBERY' select t.*, k.keyword from #test t inner join #keyword k on t.phrase like '%' + k.keyword + '%' --OR... select t.*, Keywords = ( select k.keyword + ',' as [text()] from #keyword k where t.phrase like '%' + k.keyword + '%' FOR XML PATH('')) from #test t drop table #test drop table #keyword
Ling at Stack Overflow Visit the source
Related Q & A:
- How to control the excessive use of ram by SQL Server?Best solution by Database Administrators
- How to do GROUP_CONCAT in SQL Server?Best solution by Stack Overflow
- How to import XML into SQL Server database?Best solution by Stack Overflow
- How do I connect to SQL Server using C#?Best solution by Stack Overflow
- How to replace Cursor in SQL Server?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.