sql server 2008 - sql combine multiple where operators -
i have query writing in ms-sql must check see if info client in table before inserting it. if 1 entity has changed row inserted. questions can combine operators in clause? right have query looks this:
select * @temp c exists (select * clients c2 (c.clientid = c2.clientid , c.clientfname <> c2.firstname) or (c.clientid = c2.clientid , c.clientlname <> c2.lastname) or (c.clientid = c2.clientid , c.clientaddress <> c2.address) or (c.clientid = c2.clientid , c.clientcity <> c2.city) or (c.clientid = c2.clientid , c.clientstate <> c2.state) or (c.clientid = c2.clientid , c.clientzip <> c2.zip)
is there advantage or disadvantage writing query this:
select * @temp c exists (select * clients c2 (c.clientid = c2.clientid , (c.clientfname <> c2.firstname or c.clientlname <> c2.lastname or c.clientaddress <> c2.address or c.clientcity <> c2.city or c.clientstate <> c2.state or c.clientzip <> c2.zip)))
to me both queries work best way write this?
in practice, if @ query plans 2 queries, you're find optimizer reduces them same thing. if doesn't, you'd take version provides best performance (query plan), 2 equivalent , optimizer notice , exploit that.
i note if of columns allows nulls, comparing column inadequate. you'd need more like:
or c1.clientaddress <> c2.clientaddress or (c1.clientaddress null , c2.clientaddress not null) or (c1.clientaddress not null , c2.clientaddress null)
sql sql-server-2008
No comments:
Post a Comment