Der er ingen garanti for denne adfærd.
Et eksempel på kortslutningsevaluering ikke sker med expr1 AND expr2
er
SET STATISTICS IO ON
IF EXISTS(SELECT COUNT(*) FROM master..spt_monitor HAVING COUNT(*)=2)
AND EXISTS (SELECT COUNT(*) FROM master..spt_values HAVING COUNT(*)=1)
PRINT 'Y'
EXISTS(SELECT COUNT(*) FROM master..spt_monitor HAVING COUNT(*)=2)
er false
(betyder And
-ed udtryk skal være False
), men IO-resultaterne viser, at den anden betingelse stadig blev evalueret.
Table 'spt_values'. Scan count 1, logical reads 14, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'spt_monitor'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server kan gør dog dette. Jeg ser dette i min test
SET STATISTICS IO ON
DECLARE @p1 BIT = NULL
IF ( @p1 = 1
AND EXISTS(SELECT *
FROM master..spt_values) )
PRINT '1'
ELSE IF ( @p1 = 0
AND EXISTS(SELECT *
FROM master..spt_values) )
PRINT '2'
Udgangen er
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
Viser spt_values
blev aldrig tilgået.
Dette implementeres ved en pass through-prædikatbetingelse i udførelsesplanen. Der er nogle oplysninger om dem her.
Hvis passthru-prædikatet evalueres til sandt, returnerer joinforbindelsen rækken.... Hvis passthru-prædikatet evalueres til falsk, fortsætter joinforbindelsen normalt