To start off, NULL does not mean "no value" it means "Unknown value" in SQL Server. There is a session setting called ANSI_NULLS that could make your queries behave as you would like them to, however, it's deprecated and will be forced to ON (which you don't seem to like) in a future version: http://msdn.microsoft.com/en-us/library/ms188048.aspx
I get what you're trying to do, and to make a counter point I would ask if you've seen any queries generated by reporting services or something like Cognos? If so, you'll see exactly what you're describing you don't want to do. Why? Well because with a schema that allows nulls, that's the way to do it. I'm not saying it's a super awesome and great idea but it works all of the time.
What your designer could do is check to see if that column could even be null and if so the appropriate logic could step it and create the correct query. You could also have options such as "This column may be null, do you want those values?". I don't know the end-game per-se and writing your own dynamic querying tool is quite the deat when the logical consistencies are all factored in (such as this).
I would continue to do explicit null checking on columns that could possibly be null, sure it doesn't look the best but it works all of the time.
ANSI_NULL set option will work for now but NOT a good idea especially if you don't control the environment, plus it will be forced set ON later and cause errors where you'll need to re-write your application logic anyway. This is the way SQL Server works with NULLs.
Example 4 has the fewest scans and reads:
Example 1
SQL Server parse and compile time:
CPU time = 4 ms, elapsed time = 4 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
example1 Id FirstName
-----------------------------
1 2 Aaron
1 3 John
1 8 Aaron
1 9 John
1 14 Aaron
1 15 John
1 20 Aaron
1 21 John
(8 row(s) affected)
Table 'Person'.
Scan count 9, logical reads 27, physical reads 0,
Rows Executes StmtText
-------------------- -------------------- ------------------------
8 1 select 1 example1
, parent.Id
, parent.FirstName
, parent.LastName
, case when max(child.Id) is null then 0 else 1 end HasChildren
from Person parent
left outer join Person child
on child.ParentId = parent.Id
where (@LastName is null or parent.Las 1 1 0
0 0 |--Compute Scalar(DEFINE:([Expr1005]=(1), [Expr1
8 1 |--Nested Loops(Left Outer Join, OUTER REFE
8 1 |--Clustered Index Scan(OBJECT:([sub].
3 8 |--Stream Aggregate(DEFINE:([Expr1004]
7 8 |--Clustered Index Scan(OBJECT:([
(6 row(s) affected)
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
Example 2
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 4 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
example2 Id FirstName
----------------------------
2 2 Aaron
2 3 John
2 8 Aaron
2 9 John
2 14 Aaron
2 15 John
2 20 Aaron
2 21 John
(8 row(s) affected)
Table 'Person'.
Scan count 9, logical reads 27, physical reads 0,
Rows Executes StmtText
------ ---------- -----------------------
8 1 select 2 example2
, parent.Id
, parent.FirstName
, parent.LastName
, coalesce(child.hasChildren, 0) HasChildren
from Person parent
left outer join
(
select distinct parentId, 1 hasChildren
from Person
where parentId is not null
) child
1 1 0 NULL
0 0 |--Compute Scalar(DEFINE:([Expr1007]=(2), [Expr1
8 1 |--Nested Loops(Left Outer Join, OUTER REFE
8 1 |--Clustered Index Scan(OBJECT:([sub].
3 8 |--Stream Aggregate(DEFINE:([Expr1006]
0 0 |--Compute Scalar(DEFINE:([Expr10
7 8 |--Clustered Index Scan(OBJE
(7 row(s) affected)
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
Example 3
SQL Server parse and compile time:
CPU time = 7 ms, elapsed time = 7 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
example3 Id FirstName
---- ------ ------------------------
3 2 Aaron
3 3 John
3 8 Aaron
3 9 John
3 14 Aaron
3 15 John
3 20 Aaron
3 21 John
(8 row(s) affected)
Table 'Person'. Scan count 9, logical reads 41, physical reads 0, r
Rows Executes StmtText
------ --------- ------------------
8 1 with parentCTE as (
select Id, FirstName, LastName
from person
where (@LastName is null or LastName like @LastName)
and (@FirstName is null or FirstName like @FirstName)
)
select 3 example3
, parentCTE.Id
, parentCTE.FirstName
, parentCTE.La 1 1 0 NULL
0 0 |--Compute Scalar(DEFINE:([Expr1011]=(3), [Expr1
8 1 |--Nested Loops(Left Outer Join, OUTER REFE
8 1 |--Clustered Index Scan(OBJECT:([sub].
3 8 |--Stream Aggregate(DEFINE:([Expr1010]
0 0 |--Compute Scalar(DEFINE:([Expr10
7 8 |--Nested Loops(Inner Join,
7 8 |--Clustered Index Scan
7 7 |--Clustered Index Seek
(9 row(s) affected)
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
Example 4
SQL Server parse and compile time:
CPU time = 3 ms, elapsed time = 3 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
example4 Id FirstName
---- ----- -------------
4 2 Aaron
4 3 John
4 8 Aaron
4 9 John
4 14 Aaron
4 15 John
4 20 Aaron
4 21 John
(8 row(s) affected)
Table 'Person'. Scan count 3, logical reads 26, physical reads 0,
Rows Executes StmtText
-------- ---------- ---------------------
8 1 select 4 example4
, parent.Id
, parent.FirstName
, parent.LastName
, coalesce((select top 1 1 from Person child where child.parentId =
from Person parent
where (@LastName is null or LastName like @LastName)
and (@FirstName 1 1 0 NULL
0 0 |--Compute Scalar(DEFINE:([Expr1002]=(4), [Expr1
8 1 |--Nested Loops(Left Outer Join, PASSTHRU:(
8 1 |--Nested Loops(Left Outer Join, OUTER
8 1 | |--Clustered Index Scan(OBJECT:([
0 0 | |--Compute Scalar(DEFINE:([Expr10
3 8 | |--Top(TOP EXPRESSION:((1)))
3 8 | |--Clustered Index Scan
0 0 |--Compute Scalar(DEFINE:([Expr1008]=(
3 3 |--Top(TOP EXPRESSION:((1)))
3 3 |--Clustered Index Scan(OBJE
(11 row(s) affected)
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
You want the fewest scans possible. The more times we have to scan the table, the longer it will take.
Best Answer
The update function is the part of a DML trigger where you test if a required update has occured.
For Example:
This will trigger a further action every time column column_1 is updated in table mytable. The update function is UPDATE(column_1).