2011-10-27 217 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)