2013-02-19 41 views
0

我在UNIONSQL與Case語句的where子句重置價值

的形式和查詢的一個多次查詢,我需要Case語句的where子句,使支票價值@CategoryID我的條件如果@CategoryID <> 0那麼@CategoryID = 0

注意:我不能通過@CategoryID =0作爲聯合聲明中的一個需要實際值。

我需要將@CategoryID僅重置爲類別ID的情況下最後的陳述爲0不爲0

我試着用在where子句也產生錯誤,如果產生錯誤&聲明我也試過case語句。我希望幫助在這方面,這樣我可以爲過去的SQL語句重置@CategoryID =0在存儲過程

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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] AND Active = 1 


END 

回答

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 
     [email protected] AND 
     Active = 1 
0

不要試圖在@CategoryID在union語句設置爲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 [email protected] AND Active = 1 

SET @CategoryID = 0