Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with SQL-transact!

Author  Topic 

krainov
Yak Posting Veteran

57 Posts

Posted - 2008-09-28 : 16:17:13
Please help me to solve the problem!

I have a forum on web site which consist of 3 tables: Categories, Forums, Posts
I can retrieve last 4 posts from whole forum by procedure below. But I have a case when some1 by clicking on the CategoryTitle gets the page with this Category forums where I want to display last 4 posts for this category. Please help!



ALTER PROCEDURE dbo.tbh_Forums_GetLast4Posts

AS
SET NOCOUNT ON

SELECT TOP 4
tbh_Posts.PostID,
tbh_Posts.AddedDate,
tbh_Posts.AddedBy,
tbh_Posts.AddedByIP,
tbh_Posts.ForumID,
tbh_Posts.ParentPostID,
tbh_Posts.Title,
tbh_Posts.Body,
tbh_Posts.Approved,
tbh_Posts.Closed,
tbh_Posts.ViewCount,
tbh_Posts.ReplyCount,
tbh_Posts.LastPostDate,
tbh_Posts.LastPostBy,
tbh_Forums.Title AS ForumTitle,
tbh_Forums.CategoryID AS CategoryID,
tbh_Posts.Priority,
(SELECT Title From dbo.tbh_Forums_Categories WHERE CategoryID = tbh_Forums.CategoryID) AS CategoryTitle
FROM tbh_Posts
INNER JOIN tbh_Forums ON tbh_Posts.ForumID = tbh_Forums.ForumID

WHERE tbh_Posts.ParentPostID = 0
ORDER BY tbh_Posts.AddedDate DESC

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2008-09-28 : 16:49:37
Sorry, could you explain clearer please?

What's the procedure doing that it shouldn't be, or what is it not doing that it should be?

--
Gail Shaw
SQL Server MVP
Go to Top of Page

krainov
Yak Posting Veteran

57 Posts

Posted - 2008-09-28 : 17:01:21
quote:
Originally posted by GilaMonster

Sorry, could you explain clearer please?

What's the procedure doing that it shouldn't be, or what is it not doing that it should be?

--
Gail Shaw
SQL Server MVP



This procedure is getting the last 4 posts on the whole forum (for all categories). WHat I need is new procedure which will retrieve last 4 posts for the CategoryID parameter. SOrry for my poor English :-)
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2008-09-28 : 19:04:30
Then write a new procedure that has virtually the same query, takes a parameter for the categoryID and filters on that categoryID in the where clause of the query.

CREATE PROCEDURE dbo.tbh_Forums_GetLast4PostsForCategory @CategoryID int
AS
... rest of query here ...
WHERE tbh_Posts.ParentPostID = 0 AND tbh_Forums.CategoryID = @CategoryID
ORDER BY ...

--
Gail Shaw
SQL Server MVP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-29 : 00:52:05
quote:
Originally posted by GilaMonster

Then write a new procedure that has virtually the same query, takes a parameter for the categoryID and filters on that categoryID in the where clause of the query.

CREATE PROCEDURE dbo.tbh_Forums_GetLast4PostsForCategory @CategoryID int
AS
... rest of query here ...
WHERE tbh_Posts.ParentPostID = 0 AND tbh_Forums.CategoryID = @CategoryID
ORDER BY ...

--
Gail Shaw
SQL Server MVP


No need of new procedure. you could just create a optional parameter CategoryID which when passed will return info for that category else will give entire info for all categories. somethink like

CREATE PROCEDURE dbo.tbh_Forums_GetLast4PostsForCategory @CategoryID int=NULL
AS
... rest of query here ...
WHERE tbh_Posts.ParentPostID = 0
AND (tbh_Forums.CategoryID = @CategoryID OR @CategoryID IS NULL)
ORDER BY ...
Go to Top of Page

krainov
Yak Posting Veteran

57 Posts

Posted - 2008-09-29 : 05:26:54
quote:
Originally posted by visakh16

quote:
Originally posted by GilaMonster

Then write a new procedure that has virtually the same query, takes a parameter for the categoryID and filters on that categoryID in the where clause of the query.

CREATE PROCEDURE dbo.tbh_Forums_GetLast4PostsForCategory @CategoryID int
AS
... rest of query here ...
WHERE tbh_Posts.ParentPostID = 0 AND tbh_Forums.CategoryID = @CategoryID
ORDER BY ...

--
Gail Shaw
SQL Server MVP


No need of new procedure. you could just create a optional parameter CategoryID which when passed will return info for that category else will give entire info for all categories. somethink like

CREATE PROCEDURE dbo.tbh_Forums_GetLast4PostsForCategory @CategoryID int=NULL
AS
... rest of query here ...
WHERE tbh_Posts.ParentPostID = 0
AND (tbh_Forums.CategoryID = @CategoryID OR @CategoryID IS NULL)
ORDER BY ...



Thats right! But the categoryID is in different table! I have no CategoryID in Posts table! The forums table has the CategoryID key
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-29 : 05:29:51
quote:
Originally posted by krainov

quote:
Originally posted by visakh16

quote:
Originally posted by GilaMonster

Then write a new procedure that has virtually the same query, takes a parameter for the categoryID and filters on that categoryID in the where clause of the query.

CREATE PROCEDURE dbo.tbh_Forums_GetLast4PostsForCategory @CategoryID int
AS
... rest of query here ...
WHERE tbh_Posts.ParentPostID = 0 AND tbh_Forums.CategoryID = @CategoryID
ORDER BY ...

--
Gail Shaw
SQL Server MVP


No need of new procedure. you could just create a optional parameter CategoryID which when passed will return info for that category else will give entire info for all categories. somethink like

CREATE PROCEDURE dbo.tbh_Forums_GetLast4PostsForCategory @CategoryID int=NULL
AS
... rest of query here ...
WHERE tbh_Posts.ParentPostID = 0
AND (tbh_Forums.CategoryID = @CategoryID OR @CategoryID IS NULL)
ORDER BY ...



Thats right! But the categoryID is in different table! I have no CategoryID in Posts table! The forums table has the CategoryID key


then join with forums table based on its relation with posts table and take categoryid from it.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-29 : 05:55:10
No subquery either!
ALTER PROCEDURE dbo.tbh_Forums_GetLast4Posts
(
@CategoryID INT = NULL
)
AS

SET NOCOUNT ON

SELECT TOP 4 p.PostID,
p.AddedDate,
p.AddedBy,
p.AddedByIP,
p.ForumID,
p.ParentPostID,
p.Title,
p.Body,
p.Approved,
p.Closed,
p.ViewCount,
p.ReplyCount,
p.LastPostDate,
p.LastPostBy,
d.Title AS ForumTitle,
d.CategoryID AS CategoryID,
p.Priority,
fc.Title AS CategoryTitle
FROM tbh_Posts AS p
INNER JOIN tbh_Forums AS f ON f.ForumID = p.ForumID
LEFT JOIN dbo.tbh_Forums_Categories AS fc ON fc.CategoryID = f.CategoryID
WHERE p.ParentPostID = 0
AND (f.CategoryID = @CategoryID OR @CategoryID IS NULL)
ORDER BY p.AddedDate DESC



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -