如果CROSS APPLY什么都没有,SQL 2008存储过程将不返回任何内容。我该怎么办?

时间:2009-10-02 22:48:38

标签: sql sql-server sql-server-2008

下面存储的proc返回来自多个相关表的数据,并且还在投票表上进行交叉应用。此交叉应用返回与特定articleid关联的factid列中的所有真值的平均值,以及与relevantid列相同的值。这种方法很有效,除非尚未为特定的文章投票。在这种情况下,存储过程根本不返回任何内容。考虑到我不想在投票表中捏造记录,谁能想到解决这个问题的好方法?

(     @ArticleID int     )

AS
BEGIN
WITH Article AS
(

SELECT
tbrm_Article.ArticleID, 
tbrm_Article.CountryID,
tbrm_Article.CategoryID,
tbrm_Article.Title,
tbrm_Article.ArticleDetail,
tbrm_Article.Source,
tbrm_Article.ArticleDateTimeAdded,
tbrm_Article.UserId,
tbrm_Article.ViewCount,
tbrm_Article.CommentCount,
tbrm_CountryList.CountryName AS CountryName,
tbrm_CategoryList.CategoryName AS CategoryName,
aspnet_Users.UserName AS UserName,
Truthfulness,
Relevance

FROM         
tbrm_Article INNER JOIN
tbrm_CountryList ON tbrm_Article.CountryID = tbrm_CountryList.CountryID INNER JOIN
tbrm_CategoryList ON tbrm_Article.CategoryID = tbrm_CategoryList.CategoryID INNER JOIN
aspnet_Users ON tbrm_Article.UserID = aspnet_Users.UserID
CROSS APPLY (
SELECT tbrm_Votes.ArticleID, AVG(tbrm_Votes.TruthID), AVG(tbrm_Votes.RelevanceID)
FROM tbrm_Votes
WHERE tbrm_Article.ArticleID = tbrm_Votes.ArticleID
GROUP BY tbrm_Votes.ArticleID
) AS Votes(ArticleID,Truthfulness,Relevance)

WHERE

tbrm_Article.ArticleID = @ArticleID)
SELECT * FROM Article
END

1 个答案:

答案 0 :(得分:2)

OUTER APPLY

OUTER JOIN类似。

BTW,不要调用SQL 2008'SQL 08'。它让我想到了SQL 2000(即v8)。