MrPink1138
Programmer
Here is, in a nutshell, what I'm trying to do.
Two tables - articles and comments
articles has:
article_id, headline, article_text
comments has:
comment_id, article_id, comment_text, headline
Now... SOME of the comments are linked to articles in the database. Others are not because they are general comments and don't relate to any article.
I need to be able to pull out comment_id, comment_text, and headline.
Here's the catch:
For the comments that are linked to articles I need to do an INNER JOIN and pull the headline from the articles table. For the comments WHERE article_id=0 I need to pull the headline from the comments table.
My thoughts are to do something like this in my stored procedure:
CREATE PROCEDURE spCommentarySearch
(
@CommentID int
)
AS
DECLARE @ArticleID int
SELECT @ArticleID = comments.article_id FROM comments WHERE comments_id=@CommentID
IF (@ArticleID) <> 0
RETURN SELECT comment_id, comment_text,,articles.headline FROM comments INNER JOIN Articles ON Articles.ID = comments.article_id
ELSE
RETURN SELECT comment_id, comment_text, headline FROM comments
GO
This however does not appear to work. Does anyone have any thoughts on this?
Thanks in advance.
Two tables - articles and comments
articles has:
article_id, headline, article_text
comments has:
comment_id, article_id, comment_text, headline
Now... SOME of the comments are linked to articles in the database. Others are not because they are general comments and don't relate to any article.
I need to be able to pull out comment_id, comment_text, and headline.
Here's the catch:
For the comments that are linked to articles I need to do an INNER JOIN and pull the headline from the articles table. For the comments WHERE article_id=0 I need to pull the headline from the comments table.
My thoughts are to do something like this in my stored procedure:
CREATE PROCEDURE spCommentarySearch
(
@CommentID int
)
AS
DECLARE @ArticleID int
SELECT @ArticleID = comments.article_id FROM comments WHERE comments_id=@CommentID
IF (@ArticleID) <> 0
RETURN SELECT comment_id, comment_text,,articles.headline FROM comments INNER JOIN Articles ON Articles.ID = comments.article_id
ELSE
RETURN SELECT comment_id, comment_text, headline FROM comments
GO
This however does not appear to work. Does anyone have any thoughts on this?
Thanks in advance.