Jump to content


Check out our Community Blogs





- - - - -

Multiple OR’s in T-SQL WHERE Condition SQL Server

  Posted by Barnsite, 30 November 2012 · 796 views

programming
Just been rewriting some SQL Server stored procedures for a client. There were a lot of SELECT statements like the one below:

SELECT
*
FROM dbo.TABLE
WHERE (field1 = 'SBO' OR field1 = 'RBO' OR field1 = 'PYO' OR field1 = 'TCO'
OR field1 = 'BRS' OR field1 = 'BMS') AND field2 = 'COMPLETED'

I’ve seen this quite a few times, and I’m not suggesting this is wrong, however I find that this is much better written as:

SELECT
*
FROM dbo.TABLE
WHERE field1 IN ('SBO', 'RBO', 'PYO', 'TCO', 'BRS', 'BMS') AND field2 = 'COMPLETED'

For me this makes it much clearer that we are comparing field1 to the the set of values and field2 to another value.
http://feeds.wordpre...dpress.com/487/ Posted Image

Source

  • 0



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download