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 2008 Forums
 Transact-SQL (2008)
 Where condition if no value pass in don't run this

Author  Topic 

micnie_2020
Posting Yak Master

232 Posts

Posted - 2011-10-14 : 04:36:19
Hi all,

I trying to do the sql if @ExportTy doesn't pass in 1 mean will never checking on this criteria BU.regname & DE.segment

How can i do that? I try below code. Having issue on 1=1

Please advise.

Tq


where .....
AND Case when @ExportTy='0' then 1=1 else BU.regname in (@Region) end
AND Case when @ExportTy='0' then 1=1 else DE.segment in (@Segment) end

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-14 : 04:37:51
[code]
...
where .....
1=1
or
( ',' + @Region + ',' LIKE '%,' + BU.regname + ',%'
and
',' + @Segment + ',' LIKE '%,' + DE.segment + ',%')
[/code]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sshelper
Posting Yak Master

216 Posts

Posted - 2011-10-15 : 00:55:03
If I understood your question correctly, your WHERE clause will look something like this:

WHERE (@ExportTy = 0 OR BU.regname IN (@Region)) AND
(@ExportTy = 0 OR DE.segment IN (@Segment))

SQL Server Helper
http://www.sql-server-helper.com/error-messages/msg-1-500.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-15 : 02:07:25
quote:
Originally posted by sshelper

If I understood your question correctly, your WHERE clause will look something like this:

WHERE (@ExportTy = 0 OR BU.regname IN (@Region)) AND
(@ExportTy = 0 OR DE.segment IN (@Segment))

SQL Server Helper
http://www.sql-server-helper.com/error-messages/msg-1-500.aspx


you cant use variable in IN like this.
You need to use dynamic sql or use LIKE as in suggestion before

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -