2011-10-27 179 views
0

我写一个脚本,将更新/插入关于属性的记录。我确定了一种方法来确保脚本不会产生重复,但我不完全确定如何实现它。SQL嵌套/复杂的选择语句

我们从CSV文件中导入,该文件将具有一系列属性。该物业拥有代理商提供的第三方参考号码。这可以创建一个副本,因为显然一个单独的代理可能使用相同的引用。

在表中需要更新/插入buildingNumber,buildingName或AddressLine1不会为空。然后我需要检查现有的记录邮政编码以及哪些列对于我将要插入的列不为空。

我知道表结构是狗屎。我没有设计它,我无法改变它。

我正在寻找类似的东西。

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

这将有助于大大如果你能证明你的表结构,样本数据和样本所需的输出。 – JNK

回答

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)