sql - Are these 2 SELECT statements identical? Weird use of ISNULL -


i'm familiar isnull function in sql server, have never used in way. came across code in db support:

select * employee deptid = 58 , gender = isnull('m', gender) 

it looks me same

select * employee deptid = 58 , gender = 'm' 

i tried playing around nulls see if 2 statements return different results same. know if there benefits or scenarios first select preferred on second? i'm on sql 2014. maybe syntax useful on previous versions?

thanks

these statements, exact way put, same.

using gender = isnull('m', gender) not different gender = 'm'

isnull might make sense in scenario:

declare  @gender char(1)  select  *    employee   deptid = 58         , gender = isnull(@gender, gender) 

, return rows non-null gender if @gender have been set null, , rows given gender otherwise.

if using statement, make sure have recompile on each invocation, plans null , non-null value might quite different.

this can tested creating sample table:

create table test (id int not null primary key) go  ;    q         (         select  1 id         union         select  id + 1            q           id <= 100000         ) insert    test select  id    q option  (maxrecursion 0) go 

then running 2 batches actual execution plan enabled:

set statistics io on set nocount on go  declare @id int = 7  select  *    test   id = isnull(@id, id) go  declare @id int = 7  select  *    test   id = isnull(@id, id) option  (recompile) go 

and looking plans , statistics:

table 'test'. scan count 1, logical reads 337, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. table 'test'. scan count 0, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -