简体   繁体   English

如果CROSS APPLY为空,则SQL 2008存储的proc不返回任何内容。 我该如何解决?

[英]SQL 2008 stored proc returns nothing if CROSS APPLY is nothing. How can I fix?

The below stored proc returns data from a number of related tables and also does a cross apply on the votes table. 下面存储的proc返回来自许多相关表的数据,并且对票表进行了叉号运算。 This cross apply returns an average of all truthfulness values in the truthid column associated with a particular articleid and the same for the relevanceid column. 此交叉应用返回与特定articleid关联的trueid列中所有真实值的平均值,而relevanceid列则相同。 This works great except for when there are no votes yet cast for a particular articleid. 这非常有效,除了尚未为特定商品ID投任何票时。 In this case the stored proc returns nothing at all. 在这种情况下,存储的过程根本不返回任何内容。 Can anyone think of a good way to solve this issue given I don't want to fudge a record in the votes table? 鉴于我不想在投票表中捏造记录,有人能想到解决此问题的好方法吗?

( @ArticleID int ) (@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
OUTER APPLY

Similar to OUTER JOIN . 类似于OUTER JOIN

BTW, don't call SQL 2008 'SQL 08'. 顺便说一句,不要将SQL 2008称为“ SQL 08”。 It made me think at SQL 2000 (ie. v8). 这让我想到了SQL 2000(即v8)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM