snippetsqlMinor
How can I perform a full-text CONTAINS call across a list of phrases?
Viewed 0 times
acrosscanfulltextcallperformcontainshowlistphrases
Problem
I have a list of words/phrases in a table
I want to populate a table
No matter how I try, this results in the error:
I would really rather not use a cursor just so I can assign each
phrases. I have another table with a full-text catalog indexing the description column.I want to populate a table
results with a record for each phrase, and the number of times that phrases was found in the description:INSERT INTO results(phrase, foundcount)
SELECT phrase,
(SELECT COUNT(*) FROM table WHERE CONTAINS(table.description, phrase))
FROM phrases;No matter how I try, this results in the error:
Incorrect syntax near `phrase`. Expecting STRING, TEX_LEX, or VARIABLEI would really rather not use a cursor just so I can assign each
phrase to a variable. Is that really the only option?Solution
Try this:
go
create function dbo.fn_GetCountForPhrase (@phrase varchar(500))
returns int
as
begin
declare @count int
declare @localphrase varchar(8000)
set @localphrase='"' + @phrase + '"'
SELECT @count=COUNT(*) FROM table a WHERE CONTAINS(table.description, @localphrase)
return @count
end
go
SELECT
phrase,
dbo.fn_GetCountForPhrase(phrase)
FROM phrasesCode Snippets
go
create function dbo.fn_GetCountForPhrase (@phrase varchar(500))
returns int
as
begin
declare @count int
declare @localphrase varchar(8000)
set @localphrase='"' + @phrase + '"'
SELECT @count=COUNT(*) FROM table a WHERE CONTAINS(table.description, @localphrase)
return @count
end
go
SELECT
phrase,
dbo.fn_GetCountForPhrase(phrase)
FROM phrasesContext
StackExchange Database Administrators Q#144593, answer score: 2
Revisions (0)
No revisions yet.