Pre-question info:
I'm writing a stored-procedure that would take some parameters and depending on those parameters(if they are filled - because they don't have to be) I'm adding few where clauses. The thing is I don't know if I'm gonna even use the where clause from start because I don't know if any of my params is going to be non-empty/not-null.
The inside of procedure looks cca like:
BEGIN
DECLARE @strMySelect varchar(max)
SET @strMySelect ='SELECT myparams FROM mytable'
// add some WHERE statement(*)
IF(ISNULL(@myParamDate1,'')<>'')BEGIN
SET @strMySelect =@strMySelect +'
AND param1 >='''+CAST(@myParamDate1 as varchar(30))+''''
END
IF(ISNULL(@myParamDate2,'')<>'')BEGIN
SET @strMySelect =@strMySelect +'
AND param1 <='''+CAST(@myParamDate2 as varchar(30))+''''
END
//... bit more of these "AND"s
EXECUTE(@strExec)
QUESTION: Is it ok(correct way of doing this) to put in my query some WHERE statement that I know that will be always true so I can use in my parameter cases AND always? OR do I have to check for each param if it's first one that is filled or is there an easy way of checking in SQL that at least one of my parameters isn't NULL/empty?