我有多个 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