Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Do not include param in Where clause

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2009-07-08 : 13:41:35

Done this before, just can't find it.

3 params

@param1 INT = NULL,
@param2 INT = 0,
@param3 INT = 0

SELECT id FROM myTable
WHERE fld1 = @parm1 AND fld2 = @param2 AND fld3 = @param3


Now, if param2 or param3 is 0, do NOT include in where clause.
The same goes if param1 is null.

I know there's a way to set this but just can't remember the syntax.

Thanks,

Zath

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-08 : 13:44:57
[code]
..
@param1 INT = NULL,
@param2 INT = 0,
@param3 INT = 0

SELECT id FROM myTable
WHERE (fld1 = @parm1 OR @parm1 IS NULL)
AND (fld2 = @param2 OR @param2=0)
AND (fld3 = @param3 OR @param3=0)
...
[/code]
Go to Top of Page

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2009-07-08 : 13:53:25
I know it was something like that.

Thanks, too easy!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-08 : 14:02:44
welcome
Go to Top of Page
   

- Advertisement -