Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Eliminating WHERE clauses with NOT
Message
De
04/11/2014 11:15:34
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2012
Application:
Web
Divers
Thread ID:
01610458
Message ID:
01610467
Vues:
39
>Here is a sample:
>
>
>declare @t table (id int identity, col1 Int, col2 Int)
>
>insert into @t (col1, col2) values (1, 3), (2, 5)
>
>select * from @t where not (col1 > 3 and col2 < 6)
>
>select * from @t T where not exists (select * from @t T1 where col1 > 3 and col2 < 6 and T.id = T1.id)
>
>select * from @t where col1 <=3 or col2 >=6
>
>Normally NOT (condition1 and condition2) translates into opposite condition1 OR opposite condition2

Yes, after second verification, I see that the execution is the same.

I always use the NOT in such approach as it is more easy to understand. When I have something like this:
DECLARE @ModDate DateTime
DECLARE @NoStatus Int
DECLARE @ModDate2 DateTime
DECLARE @NoStatus2 Int
DECLARE @ModDate3 DateTime
DECLARE @NoStatus3 Int

SET @ModDate='2013-11-09 10:31:18'
SET @NoStatus=5
SET @ModDate2='2014-11-03 10:31:18'
SET @NoStatus2=8
SET @ModDate3='2014-11-03 10:31:18'
SET @NoStatus3=7

SELECT Client.Numero FROM Client (NOLOCK)
 WHERE NOT (Client.ModDate<@ModDate AND Client.NoStatus=@NoStatus) AND
  NOT (Client.ModDate<@ModDate2 AND Client.NoStatus=@NoStatus2) AND
  NOT (Client.ModDate<@ModDate3 AND Client.NoStatus=@NoStatus3)
...it seems more easy to understand that the second one:
DECLARE @ModDate DateTime
DECLARE @NoStatus Int
DECLARE @ModDate2 DateTime
DECLARE @NoStatus2 Int
DECLARE @ModDate3 DateTime
DECLARE @NoStatus3 Int

SET @ModDate='2013-11-09 10:31:18'
SET @NoStatus=5
SET @ModDate2='2014-11-03 10:31:18'
SET @NoStatus2=8
SET @ModDate3='2014-11-03 10:31:18'
SET @NoStatus3=7

SELECT Client.Numero FROM Client (NOLOCK)
 WHERE (Client.ModDate>=@ModDate OR Client.NoStatus<>@NoStatus) AND
  (Client.ModDate>=@ModDate2 OR Client.NoStatus<>@NoStatus2) AND
  (Client.ModDate>=@ModDate3 OR Client.NoStatus<>@NoStatus3)
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform