SQL嵌套/复杂的select语句

时间:2011-10-27 10:15:50

标签: sql-server-2005

我要编写一个脚本来更新/插入有关属性的记录。我已经确定了一种方法来确保脚本不会创建重复项,但我不完全确定如何实现它。

我们从具有大量属性的CSV文件中导入。此属性具有代理的第三方引用号。这可以创建副本,因为显然单独的代理可以使用相同的引用。

在表中需要更新/插入buildingNumber,buildingName或AddressLine1不为null。然后我需要检查现有的记录邮政编码和任何一列不对着我要插入的列。

我知道桌面结构很糟糕。我没有设计它,我无法改变它。

我正在寻找类似的东西。

if exists(
  select * from tblMemberProperties
  where ThirdPartyReference = @PropertyThirdPartyReference
  and ((if buildingNumber is not null (then BuildingNumber = @BuildingNumber)
  or (if buildingName is not null and above if isn't satisfied (then buildingName = @BuildingName)
  or (if AddressLine1 is not null and above 2 are null (then AddressLine1 = @AddressLine1))
  and (postcode = @postcode)

1 个答案:

答案 0 :(得分:0)

if exists(
select * from tblMemberProperties
where Postcode = @Postcode
and BuildingNumber = @BuildingNumber
and BuildingNumber is not null
or 
Postcode = @Postcode
and BuildingName = @BuildingName
and BuildingName is not null
or
Postcode = @Postcode
and AddressLine1 = @AddressLine1
and AddressLine1 is not null)