我需要为高级搜索编写一个 ms sql 查询,该查询基于不同的过滤器,如标题、描述、类别、作者、语言和日期
我需要将此查询作为存储过程,但我不确定如果 CatID = 0、WriterID = 0 等,我如何构建查询。我可以使用 & 传递整个查询在 c# (ASP.Net) 中轻松构建此查询IF Statement
,但我不确定如何将其构建为 T-SQL 或存储过程
DECLARE @keyword nvarchar(300)
DECLARE @CatID int
DECLARE @WritterID int
DECLARE @IssueID int
DECLARE @sDate date
DECLARE @eDate date
DECLARE @LangID int
SET @keyword = 'xyz';
SET @CatID = 1;
SET @WritterID = 1;
SET @IssueID = 1;
SET @sDate = '1/01/2012';
SET @eDate = '1/01/2013';
SET @LangID = 1
SELECT ArticleID,ArticleTitle,ArticleCategoryID,ArticleAuthorID,IssueID,ArticlePublishDate FROM art_Articles
WHERE ArticleTitle LIKE '%'+ @keyword +'%'
OR ArticleDesc LIKE '%'+ @keyword +'%'
C# 中的代码示例
strSql = "SELECT ArticleID, ArticleTitle, ArticleDesc, ArticlePublishDate FROM art_Articles WHERE ";
strSql += "( (ArticleVisible = 1 AND ArticleActive =1 AND LanguageID =" + LangID + " ))";
if (CatID > 0)
{ strSql += " AND ArticleCategoryID ="+ CatID; }
if (WriterID > 0)
{ strSql += " AND ArticleAuthorID ="+ WriterID; }