Solved

Search Freetext Multiple Columns SQL2000

Posted on 2007-11-29
7
443 Views
Last Modified: 2008-02-01
I am trying to query multiple columns in multiple tables.   I need to weigh certain columns in the results.  My current code looks as follows:
"select Key_Tbl.[key], tblBlogThreads.BlogThreadNum,  tblBlogThreads.Body,tblBlogThreads.PublishDate, tblBlogThreads.Title, tblBlogThreads.Teaser, tblBlogThreads.MetaKeywords,  [rank] from freetexttable(tblBlogThreads, body, '" & tstr & "' ) AS Key_Tbl  INNER JOIN tblBlogThreads ON tblBlogThreads.BlogthreadID = Key_Tbl.[key] order by [rank] desc"

The problem is i need to search in columns tblBlogThreads.Title,  tblBlogThreads, Keywords, and tblBlogThreads.Body, and weigh them in that order.    
0
Comment
Question by:DidiahProd
  • 4
  • 3
7 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20379581
select
Key_Tbl.[key], tblBlogThreads.BlogThreadNum,  
tblBlogThreads.Body,tblBlogThreads.PublishDate,
tblBlogThreads.Title,
tblBlogThreads.Teaser,
tblBlogThreads.MetaKeywords,  
[rank]
from
(select [key], rank + 2000 as rank from freetexttable(tblBlogThreads, title, '" & tstr & "' )
 union all
 select [key], rank + 1000 from freetexttable(tblBlogThreads, keywords, '" & tstr & "' )
 union all
 select [key], rank from freetexttable(tblBlogThreads, body, '" & tstr & "' )
) AS Key_Tbl
INNER JOIN tblBlogThreads ON tblBlogThreads.BlogthreadID = Key_Tbl.[key]
order by [rank] desc
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20379590
The ranks returned are 1-1000, therefore match by title will have high rank, then keywords, then body.  I forgot to trim duplicates. this one fixes it

select
Key_Tbl.[key], tblBlogThreads.BlogThreadNum,  
tblBlogThreads.Body,tblBlogThreads.PublishDate,
tblBlogThreads.Title,
tblBlogThreads.Teaser,
tblBlogThreads.MetaKeywords,  
[rank]
from
(select [key], max(rank) as rank from
(select [key], rank + 2000 as rank from freetexttable(tblBlogThreads, title, '" & tstr & "' )
 union all
 select [key], rank + 1000 from freetexttable(tblBlogThreads, keywords, '" & tstr & "' )
 union all
 select [key], rank from freetexttable(tblBlogThreads, body, '" & tstr & "' )
) tbl1
group by [key]) AS Key_Tbl
INNER JOIN tblBlogThreads ON tblBlogThreads.BlogthreadID = Key_Tbl.[key]
order by [rank] desc
0
 

Author Comment

by:DidiahProd
ID: 20379703
Thanks!

these look good but there's a syntax error in there somewhere- getting error near a space, near a space and near a ).
Looking into it...
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 25

Expert Comment

by:imitchie
ID: 20379706
That was meant to include a start " and an end " to be assigned to a string from ASP.NET.  That's why there are two
" & tstr & "
bits in the middle

It does not run as is and will cause an error from Query Analyzer!
But I think you get the idea.

0
 

Author Comment

by:DidiahProd
ID: 20379752
yah thanks- it runs in the QA but won't run in sql- looking more into it.  i can't get it to go in an SP either
0
 

Author Comment

by:DidiahProd
ID: 20379939
How would this be represented as a stored procedure where tsq is the parameter (@tsq)

select 

Key_Tbl.[key], tblBlogThreads.BlogThreadNum,  

tblBlogThreads.Body,tblBlogThreads.PublishDate, 

tblBlogThreads.Title, 

tblBlogThreads.Teaser, 

tblBlogThreads.MetaKeywords,  

[rank] 

from

(select [key], max(rank) as rank from

(select [key], rank + 2000 as rank from freetexttable(tblBlogThreads, title, '" & tstr & "' )

 union all

 select [key], rank + 1000 from freetexttable(tblBlogThreads, keywords, '" & tstr & "' )

 union all

 select [key], rank from freetexttable(tblBlogThreads, body, '" & tstr & "' )

) tbl1

group by [key]) AS Key_Tbl

INNER JOIN tblBlogThreads ON tblBlogThreads.BlogthreadID = Key_Tbl.[key] 

order by [rank] desc

Open in new window

0
 
LVL 25

Accepted Solution

by:
imitchie earned 325 total points
ID: 20379997
create procedure myproc ( @tsq varchar(100) )
as
select
Key_Tbl.[key], tblBlogThreads.BlogThreadNum,  
tblBlogThreads.Body,tblBlogThreads.PublishDate,
tblBlogThreads.Title,
tblBlogThreads.Teaser,
tblBlogThreads.MetaKeywords,  
[rank]
from
(select [key], max(rank) as rank from
(select [key], rank + 2000 as rank from freetexttable(tblBlogThreads, title, @tsq )
 union all
 select [key], rank + 1000 from freetexttable(tblBlogThreads, keywords, @tsq )
 union all
 select [key], rank from freetexttable(tblBlogThreads, body, @tsq )
) tbl1
group by [key]) AS Key_Tbl
INNER JOIN tblBlogThreads ON tblBlogThreads.BlogthreadID = Key_Tbl.[key]
order by [rank] desc
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now