Hello Experts,
I would very much appreciate some expert advise / help on a query I am writing, below is an example of what I am writing. I have a user interface that will present to the users several teat boxes / search form, The ID, month and year always have a default value but they have the option of entering an account # , PO # and a date range, if they leave the date range, Account and Account blank then it will return result for just ID, month, year ( default values).. What is the best way to implement the query so I don't have to repeat the select statement for the Date Range section, and is this the best way to implement this type of search interfaces in my SQL.
SO basically, the user will have the option of searching for specific record or leaving the parameters blank which will return all records..
Any help will be greatly appreciated... Thank You..
@Year AS SMALLINT = NULL,
@month AS SMALLINT = NULL,
@Acct_Mask as VARCHAR(20) = NULL
@PO_Num AS VARCHAR(20) = NULL
@StartDate AS Datetime = NULL,
@EndDate AS Datetime = NULL,
IF @StartDate IS NULL OR @StartDate = '' OR @EndDate IS NULL OR @EndDate = ''
SELECT * from Table1
WHERE
ID = @ID
AND month= @month
AND Year = @Year
AND (ISNULL(@Acct_Mask, '') = '' OR (Account LIKE @Acct_Mask))
AND (ISNULL(@PO_Num , '') = '' OR (Po = @PO_Num ))
ELSE
SELECT * from Table1
WHERE
ID = @ID
AND month= @month
AND Year = @Year
AND (ISNULL(@Acct_Mask, '') = '' OR (Account LIKE @Acct_Mask))
AND (ISNULL(@PO_Num , '') = '' OR (Po = @PO_Num ))
AND CheckDate >= @StartDate AND CheckDate <= @EndDate
RETURN
END