我有一个用 MS-SQL 编写的查询,在插入之前必须检查有关客户端的信息是否已经在表中。如果一个实体已更改,则将插入该行。问题是我可以在 where 子句中组合运算符吗?现在我有一个看起来像这样的查询:
select * from @Temp c
where exists (select * from Clients c2
where (c.ClientId = c2.ClientId and c.ClientFName <> c2.FirstName)
or (c.ClientId = c2.ClientId and c.ClientLName <> c2.LastName)
or (c.ClientId = c2.ClientId and c.ClientAddress <> c2.Address)
or (c.ClientId = c2.ClientId and c.ClientCity <> c2.City)
or (c.ClientId = c2.ClientId and c.ClientState <> c2.State)
or (c.ClientId = c2.ClientId and c.ClientZip <> c2.Zip)
像这样编写查询有什么优点或缺点:
select * from @Temp c
where exists (select * from Clients c2
where (c.ClientId = c2.ClientId
and (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)))
对我来说,这两个查询都有效,但是写这个的最好方法是什么?