I need to write a ms sql query for advance search which is based on different filters like Title, Description, Category, Author, Language & Dates
I need this query as a stored procedure but i am not sure how i can build query if CatID = 0, WriterID = 0 and so on. I can build this query easy in c# (ASP.Net) using IF Statement
& pass whole query, but i am not sure how to build it as T-SQL or Store Procedure
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 +'%'
Code example in 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; }