2011-01-05 15 views
1

私は何をしようとしているのかわかりません。私は条件に基づいていくつかのアドレスフィールドを選択する必要があります。複雑なケースステートメントのヘルプSQL 2005

ロジックは固いですが、構文が正しくないようです。それは、キーワード 'Case'、 'Else'、 'Else'の近くに 'Incorrect syntax'というエラーが表示されています。 アイデアは、isprimary = 1の場合はテーブルにアドレスを挿入し、1次挿入がない場合は最新のアドレスisactive = 1、アクティブでない場合は最新のアドレスを挿入します。


を助けてください - レコード02

DECLARE @ygcaddress TABLE 
(
[AccountID] varchar(10), 
[Address1] varchar(25), 
[City] varchar(22), 
[State] varchar(3), 
[Zip] varchar(9) 
) 
INSERT INTO @ygcaddress 
CASE 
WHEN Address.IsPrimary=1 
THEN 
SELECT 
LEFT(Address.AddressLine1,25), 
Address.City, 
[Lookup].LookupValue, 
Address.Zip 
FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) 
INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) 
INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) 
INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID 
WHERE Address.IsPrimary=1 
ELSE 
CASE 
WHEN Address.IsActive=1 
THEN 
SELECT TOP 1 
LEFT(Address.AddressLine1,25), 
Address.City, 
[Lookup].LookupValue, 
Address.Zip 
FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) 
INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) 
INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) 
INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID 
WHERE Address.IsActive=1 
ELSE 
SELECT TOP 1 
LEFT(Address.AddressLine1,25), 
Address.City, 
[Lookup].LookupValue, 
Address.Zip 
FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) 
INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) 
INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) 
INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID 
END 
END 
+0

あなたがあなたの質問の本質を失うことなく、クエリを簡素化でした:

はこのような何かを試してみてください? –

答えて

0

結果を小文字に追加カッコための一つの最高のアドレスを選択し、これは(構文的に少なくとも)動作するはずです。

--Select One best address for Record 02 
DECLARE @ygcaddress TABLE ([AccountID] varchar(10), [Address1] varchar(25), [City] varchar(22), [State] varchar(3), [Zip] varchar(9)) 
INSERT INTO @ygcaddress 
SELECT 
CASE WHEN Address.IsPrimary=1 THEN 
    (
     SELECT LEFT(Address.AddressLine1,25), Address.City, [Lookup].LookupValue, Address.Zip 
     FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID WHERE Address.IsPrimary=1 
    ) 
ELSE 
    CASE WHEN Address.IsActive=1 THEN 
     (SELECT TOP 1 LEFT(Address.AddressLine1,25), Address.City, [Lookup].LookupValue, Address.Zip FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID WHERE Address.IsActive=1) 
    ELSE 
     (SELECT TOP 1 LEFT(Address.AddressLine1,25), Address.City, [Lookup].LookupValue, Address.Zip FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID) 
    END 
END 
0

私はそれがあなたの元の質問の構文エラーのように見える見ることができるものから、これを試してみてください。あまりこの掘り下げるなければ

DECLARE @ygcaddress TABLE 
(
    [AccountID] varchar(10), 
    [Address1] varchar(25), 
    [City] varchar(22), 
    [State] varchar(3), 
    [Zip] varchar(9) 
) 

INSERT INTO @ygcaddress 
    CASE 

     WHEN Address.IsPrimary=1 
     THEN 
     SELECT 
     LEFT(Address.AddressLine1,25), 
     Address.City, 
     [Lookup].LookupValue, 
     Address.Zip 
     FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) 
      INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) 
      INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) 
      INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID 
      WHERE Address.IsPrimary=1 

     WHEN Address.IsActive=1 
     THEN 
     SELECT TOP 1 
     LEFT(Address.AddressLine1,25), 
     Address.City, 
     [Lookup].LookupValue, 
     Address.Zip 
     FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) 
      INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) 
      INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) 
      INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID 
      WHERE Address.IsActive=1 

     ELSE 
     SELECT TOP 1 
     LEFT(Address.AddressLine1,25), 
     Address.City, 
     [Lookup].LookupValue, 
     Address.Zip 
     FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) 
      INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) 
      INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) 
      INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID 
    END 
0

、あなたは「VALUES」のキーワードが欠落しているように見えます最低でも。 INSERT文のスケルトンの構文を使用すると、より複雑なものを代用しようとする前に、私はリテラル文字列(または何でも)で正しいINSERT文を書くことをお勧めしたい

 
INSERT INTO table_name (column1, column2, column3,...) 
VALUES (value1, value2, value3,...) 

です。

0

私が問題を正しく理解していれば、CASE文はまったく欲しかったと思います。CASE文は、それを作成しようとしているときにタプルではなく値式を返します。 (少なくともSQL Serverの中で私が最もよく似ているSQLデータベースです)

UNIONを使用して私の最初に提案した解決策を削除するように編集しました。再び

EDITED:

INSERT INTO @ygcaddress 
SELECT 
    Account.AccountID 
    ,LEFT(Address.AddressLine1,25) 
    ,Address.City 
    ,[Lookup].LookupValue 
    ,Address.Zip 
FROM AccountPerson 
INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID 
INNER JOIN 
(
    SELECT TOP 1 
    A1.* 
    CASE 
     WHEN A1.IsPrimary=1 THEN 10 
     WHEN A1.IsPrimary=0 AND A1.IsActive=1 THEN 5 
     ELSE 1 
    END [Rank] 
    FROM Address A1 
    ORDER BY [Rank] DESC 
) Address ON AccountPerson.PersonID=Address.PersonID 
INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID