7

我在下面Stored Procedure-

ALTER PROCEDURE [dbo].[Optimized_GetArticlePostAMP]                                                              
(                                                                                    
 @PostID int                                                                                    
)                                                                                          

AS                                                                                    
BEGIN                                                                                    
SET NOCOUNT ON;                                                                                    
SET STATISTICS TIME ON                                                    

DECLARE @SectionId int ,@datediff int                                            
DECLARE @postdate datetime

 SELECT P.PostId, P.SectionID, P.PostName,MP.MetaTitle,P.Postdate,P.PostAuthor,P.IsApproved,                                                                                             
 MP.Metadescription, MP.Metakeywords,ISNULL(MP.IsRobotsMetaTag,0) as IsRobotsMetaTag,p.TotalViews, P.Subject, P.FormattedBody,                                                                                               
 MV.Isvideo,MV.VideoCode,MV.VideoCaption, A.DrComment,A.SpanishURL, PS.RedirectUrl, Isnull(PS.IsRedirect,0) as IsRedirect,                                     
 ISNULL(A.CommentedBy,38633) as CommentedBy ,MP.Canonical as Canonical,ISNULL(MP.RRpopUP ,0) as RRpopUP,ISNULL(A.PrevPost,0) as PreviousPostId,        
 ISNULL(A.NextPost,0) as NextPostId,PS.StatusId ,dbo.[mercola_GetCommentCountForPost](@PostId) as TotalReplies, isnull(PA.[FileName],'') as FileName,         
 PRD.StoryImage, PRD.StoryContent, ISNULL(NULLIF(prd.ALT, ''), P.Subject) AS ALT, ISNULL(PR.ReferenceData,'')as ReferenceData,       
 MH.LastModifiedDate,    
 CASE WHEN CHARINDEX('<p><strong>By', FormattedBody, -1)=1 THEN LTRIM(SUBSTRING(REPLACE(CAST(FormattedBody as varchar(max)),'<p><strong>By ',''),0,CHARINDEX('<',REPLACE(cast(FormattedBody as varchar(max)),'<p><strong>By ',''))))   
 ELSE 'Dr.Mercola' END as Name  
 FROM cs_posts P        
 LEFT JOIN Mercola_NewsletterDetails A on (P.Postid = A.postid)                                                                                       
 LEFT JOIN Mercola_PostStatus PS on (PS.postid=p.postid)                                                    
 LEFT JOIN Mercola_PostMetatags MP on(P.postid=MP.Postid)                                                                                             
 LEFT JOIN Mercola_postVideo MV  on(P.postid=MV.Postid)                                                      
 LEFT JOIN CS_PostAttachments PA on P.PostId=PA.PostId AND PA.contenttype LIKE 'audio/mpeg' AND PA.FILENAME LIKE '%.mp3' AND PA.isremote = 1                                                    
 LEFT JOIN Mercola_PostRelatedData PRD on P.PostId=PRD.PostId                                                  
 LEFT JOIN Mercola_PostReferences PR on P.PostId=PR.PostId        
 CROSS APPLY (select top 1 LastModifiedDate from Mercola_ArticleModifiedHistory where Mercola_ArticleModifiedHistory.Postid = P.postid order by LastModifiedDate desc)MH                                                  
 where P.Postid = @Postid 

现在,当我execute在上面SP和下面PostID-

--[Mercola_Optimized_GetArticlePostAMP] 732490我得到以下数据expected。因为,里面的查询cross apply有上面的数据postID

在此处输入图像描述

但是现在,当我executeSP以下不同时PostID-

--[Mercola_Optimized_GetArticlePostAMP] 40702我在下面empty data [rows]。因为,里面的查询cross apply没有上面postIDInfact的数据,其他joins的有数据。

在此处输入图像描述

上述情况 的预期结果 - 返回数据并为CROSS APPLY. 我该怎么做?

4

2 回答 2

16

使用OUTER APPLY代替CROSS APPLY

要溢出 NULL,请使用ISNULL(MH.LastModifiedDate, @DefaultValue) as LastModifiedDate

于 2016-06-23T10:05:29.490 回答
2

将您的查询更改为外部应用,即使没有匹配项,它也会保留左侧的行

   outer APPLY (select top 1 LastModifiedDate
    from ArticleModifiedHistory 
    where ArticleModifiedHistory.Postid = P.postid order by LastModifiedDate desc

交叉应用类似于Inner Join,所以你只会得到匹配的行,Outer apply就像Left join即使没有匹配的行也会保留你的左表

更新:

如果您想在 null 的情况下为外部应用行分配默认值,只需在 select 中使用 IsNull

如下所示:

select *,isnull(b.id,'defaultvalue') from test1 t1 
outer apply(select id from test2 t2 where t1.id=t2.id) b
于 2016-06-23T09:56:30.740 回答