Hi everyone, I have a question about searching in SQL. We have a piece of code below responsible for searching a set of content within our site. When we wrote the StaticContent algorithm, it worked fine. After adding ContribContent (2nd and 4th par) it does not appear to work. Do you have any idea why?
Thanks so much!
ALTER PROCEDURE [dbo].[SearchContent]
@PageNumber tinyint,
@ResultsPerPage tinyint,
@HowManyResults smallint output,
@AllWords bit,
@Word1 varchar(15) = null,
@Word2 varchar(15) = null,
@Word3 varchar(15) = null,
@Word4 varchar(15) = null,
@Word5 varchar(15) = null
AS
BEGIN
DECLARE @Content TABLE
(RowNumber smallint IDENTITY (1,1) NOT NULL,
ContentID int,
Title varchar(50),
UserID int,
Summary text,
FirstName varchar(50),
LastName varchar(50),
Picture varchar(100),
Type varchar(50),
Rank int
)
IF @AllWords = 0
INSERT INTO @Content
SELECT ContribContent.ContentID, ContribContent.Title, ContribContent.UserID,
SUBSTRING(Article, 1, 300) + '...' AS Summary, Users.FirstName, Users.LastName, Users.Picture, 'Contributed Content' AS Type,
3 * dbo.WordCount(@Word1, Title) + dbo.WordCount(@Word1, Article) +
3 * dbo.WordCount(@Word2, Title) + dbo.WordCount(@Word2, Article) +
3 * dbo.WordCount(@Word3, Title) + dbo.WordCount(@Word3, Article) +
3 * dbo.WordCount(@Word4, Title) + dbo.WordCount(@Word4, Article) +
3 * dbo.WordCount(@Word5, Title) + dbo.WordCount(@Word5, Article)
AS Rank
FROM ContribContent
INNER JOIN Users
ON ContribContent.UserID = Users.UserID
ORDER BY Rank DESC
IF @AllWords = 0
INSERT INTO @Content
SELECT StaticContent.ContentID, StaticContent.Title, StaticContent.UserID,
SUBSTRING(Article, 1, 300) + '...' AS Summary, Users.FirstName, Users.LastName, Users.Picture, 'Static Content' AS Type,
3 * dbo.WordCount(@Word1, Title) + dbo.WordCount(@Word1, Article) +
3 * dbo.WordCount(@Word2, Title) + dbo.WordCount(@Word2, Article) +
3 * dbo.WordCount(@Word3, Title) + dbo.WordCount(@Word3, Article) +
3 * dbo.WordCount(@Word4, Title) + dbo.WordCount(@Word4, Article) +
3 * dbo.WordCount(@Word5, Title) + dbo.WordCount(@Word5, Article)
AS Rank
FROM StaticContent
INNER JOIN Users
ON StaticContent.UserID = Users.UserID
ORDER BY Rank DESC
IF @AllWords = 1
INSERT INTO @Content
SELECT ContribContent.ContentID, ContribContent.Title, ContribContent.UserID,
SUBSTRING(Article, 1, 300) + '...' AS Summary, Users.FirstName, Users.LastName, Users.Picture, 'Contributed Content' AS Type,
(3 * dbo.WordCount(@Word1, Title) + dbo.WordCount(@Word1, Article)) *
CASE
WHEN @Word2 IS NULL THEN 1
ELSE
3 * dbo.WordCount(@Word2, Title) + dbo.WordCount(@Word2, Article)
END *
CASE
WHEN @Word3 IS NULL THEN 1
ELSE
3 * dbo.WordCount(@Word2, Title) + dbo.WordCount(@Word2, Article)
END *
CASE
WHEN @Word4 IS NULL THEN 1
ELSE
3 * dbo.WordCount(@Word2, Title) + dbo.WordCount(@Word2, Article)
END *
CASE
WHEN @Word5 IS NULL THEN 1
ELSE
3 * dbo.WordCount(@Word2, Title) + dbo.WordCount(@Word2, Article)
END
AS Rank
FROM ContribContent
INNER JOIN Users
ON ContribContent.UserID = Users.UserID
ORDER BY Rank DESC
IF @AllWords = 1
INSERT INTO @Content
SELECT StaticContent.ContentID, StaticContent.Title, StaticContent.UserID,
SUBSTRING(Article, 1, 300) + '...' AS Summary, Users.FirstName, Users.LastName, Users.Picture, 'Static Content' AS Type,
(3 * dbo.WordCount(@Word1, Title) + dbo.WordCount(@Word1, Article)) *
CASE
WHEN @Word2 IS NULL THEN 1
ELSE
3 * dbo.WordCount(@Word2, Title) + dbo.WordCount(@Word2, Article)
END *
CASE
WHEN @Word3 IS NULL THEN 1
ELSE
3 * dbo.WordCount(@Word2, Title) + dbo.WordCount(@Word2, Article)
END *
CASE
WHEN @Word4 IS NULL THEN 1
ELSE
3 * dbo.WordCount(@Word2, Title) + dbo.WordCount(@Word2, Article)
END *
CASE
WHEN @Word5 IS NULL THEN 1
ELSE
3 * dbo.WordCount(@Word2, Title) + dbo.WordCount(@Word2, Article)
END
AS Rank
FROM StaticContent
INNER JOIN Users
ON StaticContent.UserID = Users.UserID
ORDER BY Rank DESC
SELECT @HowManyResults = Count(*)
FROM @Content
WHERE Rank > 0
SELECT ContentID, Title, Title, UserID, Summary, FirstName, LastName, Picture, Type, Rank
FROM @Content
WHERE Rank > 0 AND RowNumber BETWEEN (@PageNumber-1) * @ResultsPerPage+1 AND @PageNumber * @ResultsPerPage
ORDER BY Rank DESC
END