2011-10-27 28 views
0

私は、プロパティに関するレコードを更新/挿入するスクリプトを書くことができます。スクリプトで重複が作成されないようにする方法を決定しましたが、実装方法は完全にはわかりません。SQLネストされた/複雑なselectステートメント

プロパティをロードする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

あなたのテーブル構造、サンプルデータ、およびサンプル所望の出力を示すことができるかどうかは非常に役立つだろう。 – 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) 
関連する問題