2011-01-05 87 views
1

我不確定我想要做什麼的語法。我需要根據條件選擇幾個地址字段。需要有關複雜案例陳述的幫助SQL 2005

該邏輯是堅實的,但它似乎語法不正確。它給'錯誤的語句'靠近關鍵字'Case','Else','Else'。 如果isprimary = 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

你能簡化查詢,而不會丟失你的問題的本質是什麼? – 2011-01-05 21:58:50

回答

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

沒有挖掘到這太多了,你似乎缺少「值」關鍵字至少。 INSERT語句的語法骨架是

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

我建議寫一個正確的INSERT語句與文字字符串(或其他)嘗試替換什麼更復雜了。

0

如果我正確地理解了這個問題,我不認爲你想要一個CASE語句:CASE語句返回一個值表達式,而不是你試圖創建它的元組。 (至少它在SQL Server中是我最熟悉的SQL數據庫)

EDITED刪除我的第一個建議的解決方案使用UNION,我意識到是不正確的。

再次編輯:

嘗試是這樣的:

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