0

我有多个 UNION 形式的查询

对于其中一个查询,我需要在 where 子句中使用 Case 语句来检查值@CategoryID我的条件是 if @CategoryID <> 0 then @CategoryID = 0

注意:我不能通过@CategoryID =0,因为 union 中的语句之一需要实际值。

@CategoryID如果 categoryid 不是 0,我需要将 only for last 语句重置为 0。

我尝试使用产生错误的 if 语句,并且我还在 where 子句中尝试了 case 语句,它也会产生错误。我将不胜感激这方面的帮助,以便我可以重置@CategoryID =0存储过程中的最后一个 sql 语句

ALTER PROCEDURE [dbo].[usp_GetBannerByIDsAPICPL]
    @ArticleID int,
    @PageID int,
    @IssueID int,
    @CategoryID int,
    @BannerLayoutPosition int,
    @LangID int
AS
BEGIN
    SET NOCOUNT ON

    -- will show artile related banner
    SELECT BannerID, BannerName, '../images/Banners/' +BannerImageFile AS ImagePath,BannerURL,BannerLocation FROM Banner_Manager    
    WHERE ArticleID = @ArticleID    AND BannerLocation = @BannerLayoutPosition AND LanguageID=@LangID AND Active = 1 
    UNION ALL

    -- show banner by category & Issue
    SELECT BannerID, BannerName, '../images/Banners/' +BannerImageFile AS ImagePath ,BannerURL,BannerLocation FROM Banner_Manager   
    WHERE CategoryID = @CategoryID AND IssueID = @IssueID   AND BannerLocation = @BannerLayoutPosition 
    AND LanguageID=@LangID AND Active = 1 
        UNION ALL

    -- show banner by category 
    SELECT BannerID, BannerName, '../images/Banners/' +BannerImageFile AS ImagePath ,BannerURL,BannerLocation FROM Banner_Manager   
    WHERE CategoryID = @CategoryID  AND BannerLocation = @BannerLayoutPosition 
    AND LanguageID=@LangID AND Active = 1 
    UNION ALL
    -- will show page  related banner
    SELECT BannerID, BannerName, '../images/Banners/' +BannerImageFile AS ImagePath,BannerURL,BannerLocation FROM Banner_Manager    
    WHERE PageID = @PageID  AND BannerLocation = @BannerLayoutPosition AND LanguageID=@LangID AND Active = 1 
    UNION ALL
    --will show issue related banner
    -- Need To Check if @CategoryID is not 0 if it is not 0 then i have to set the @CategoryID = 0
    SELECT BannerID, BannerName, '../images/Banners/' +BannerImageFile AS ImagePath,BannerURL,BannerLocation FROM Banner_Manager    
    WHERE IssueID = @IssueID AND CategoryID = @CategoryID AND BannerLocation = @BannerLayoutPosition AND LanguageID=@LangID AND Active = 1 


END
4

2 回答 2

0

如果您不希望在该查询中更改 @CategoryID,请不要尝试在联合语句中将其设置为 0。在单独的语句中将其设置为 0:

UNION ALL
--will show issue related banner
-- Need To Check if @CategoryID is not 0 if it is not 0 then i have to set the @CategoryID = 0
SELECT BannerID, BannerName, '../images/Banners/' +BannerImageFile AS ImagePath,BannerURL,BannerLocation FROM Banner_Manager    
WHERE IssueID = @IssueID AND CategoryID = @CategoryID AND BannerLocation = @BannerLayoutPosition AND LanguageID=@LangID AND Active = 1 

SET @CategoryID = 0
于 2013-02-19T06:47:19.677 回答
0

如果此条件仅适用于最后一条语句:

@CategoryID <> 0 then @CategoryID = 0

@CategoryID当您要检查的值始终为零时,为什么您仍然传递参数?

在最后一条语句中,为什么不能直接将类别与零进行比较:

-- will show issue related banner
SELECT  BannerID, 
        BannerName, 
        '../images/Banners/' + BannerImageFile AS ImagePath,
        BannerURL,
        BannerLocation 
FROM    Banner_Manager    
WHERE   IssueID = @IssueID AND 
        CategoryID = 0 AND      -- <<== HERE
        BannerLocation = @BannerLayoutPosition AND 
        LanguageID=@LangID AND 
        Active = 1
于 2013-02-19T06:45:36.350 回答