2012-06-28 113 views
-1
UPDATE DB4010.dbo.EntityStagedData 
SET 
EntityData = (
    SELECT 
     geo.City + ' ' + geo.Description + ' ' + geo.Street + ' ' + 
     geo2.City + ' ' + geo2.Description + ' ' + geo2.Street 
    FROM DB4010.dbo.RouteTemplates templates 
     INNER JOIN DB4010.dbo.RouteTemplateClients clients 
     ON clients.RouteTemplateID = templates.RouteTemplateID 
     INNER JOIN DB4010.dbo.RouteTemplateStopMasters masters 
     ON masters.RouteTemplateClientID = clients.RouteTemplateClientID 
     INNER JOIN DB4010.dbo.RouteTemplateStopDetails details 
     ON details.RouteTemplateStopID = masters.PickupStopID 
     INNER JOIN DB4010.dbo.RouteTemplateStopDetails details2 
     ON details2.RouteTemplateStopID = masters.DeliveryStopID 
     INNER JOIN DB4010.dbo.Geofences geo 
     ON geo.GeofenceID = details.GeofenceID 
     INNER JOIN DB4010.dbo.Geofences geo2 
     ON geo2.GeofenceID = details2.GeofenceID 
    WHERE clients.RouteTemplateID = DB4010.dbo.EntityStagedData.EntityID 
) 
WHERE EXISTS (SELECT RouteTemplateID FROM DB4010.dbo.RouteTemplates) 

這是給我的錯誤:更新選擇

Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'EntityData', table 'DB4010.dbo.EntityStagedData'; column does not allow nulls. UPDATE fails.

我不能,我的生活,找出如何從結果更新+串聯「EntityData」內Select語句...

+1

不知道我理解你所說的「從內Select語句的結果‘’更新+串聯」 EntityData意味着你是什麼意思添加「實體數據」到選擇的結果,或添加EntityData對結果的現有值,還是別的? – Bort

+0

那麼,EntityData會從內部select語句中追加NVARCHAR響應... – MatBee

回答

2

我做了一些新的變化:

  • 單字母別名可讀性
  • 修復專有的SQL Server更新來自語法
  • 消除不相關的WHERE EXISTS子句

您可能仍需要決定在geo.City等包含NULL值的情況下應採取的行動。您可能需要簡單地將表達式包裝在COALESCE中,或者將NULL行全部過濾出來。

UPDATE s SET EntityData = 
     geo.City + ' ' + geo.Description + ' ' + geo.Street + ' ' + 
     geo2.City + ' ' + geo2.Description + ' ' + geo2.Street 
FROM DB4010.dbo.EntityStagedData AS s 
INNER JOIN DB4010.dbo.RouteTemplateClients AS c 
    ON c.RouteTemplateID = s.EntityID 
INNER JOIN DB4010.dbo.RouteTemplates AS t 
    ON t.RouteTemplateID = c.RouteTemplateID 
INNER JOIN DB4010.dbo.RouteTemplateStopMasters AS m 
    ON m.RouteTemplateClientID = c.RouteTemplateClientID 
INNER JOIN DB4010.dbo.RouteTemplateStopDetails AS d 
    ON d.RouteTemplateStopID = m.PickupStopID 
INNER JOIN DB4010.dbo.RouteTemplateStopDetails AS d2 
    ON d2.RouteTemplateStopID = m.DeliveryStopID 
INNER JOIN DB4010.dbo.Geofences AS geo 
    ON geo.GeofenceID = d.GeofenceID 
INNER JOIN DB4010.dbo.Geofences AS geo2 
    ON geo2.GeofenceID = d2.GeofenceID; 
+0

這似乎隻影響一行而不是在我原來的SELECT語句中返回的每一行... – MatBee

+0

哪一行?你有沒有提到你沒有告訴我們的where子句?也許你寫連接的方式意味着只能有一行?原來的SELECT(括號內)不可能自己運行,所以如果你想得到答案,你需要包含更多的細節。 –

2

您可以在COALESCE聲明包裹第一部分爲這樣:

SET EntityData = COALESCE(( SELECT   geo.City + ' ' + geo.Description 
    + ' ' + geo.Street + ' ' +   geo2.City + ' ' + geo2.Description + ' ' 
    + geo2.Street  
FROM DB4010.dbo.RouteTemplates templates   
INNER JOIN DB4010.dbo.RouteTemplateClients clients ON clients.RouteTemplateID = 
    templates.RouteTemplateID   
INNER JOIN DB4010.dbo.RouteTemplateStopMasters masters ON 
    masters.RouteTemplateClientID = clients.RouteTemplateClientID   
INNER JOIN DB4010.dbo.RouteTemplateStopDetails details ON 
    details.RouteTemplateStopID = masters.PickupStopID   
INNER JOIN DB4010.dbo.RouteTemplateStopDetails details2 ON 
    details2.RouteTemplateStopID = masters.DeliveryStopID   
INNER JOIN DB4010.dbo.Geofences geo ON geo.GeofenceID = details.GeofenceID   
INNER JOIN DB4010.dbo.Geofences geo2 ON geo2.GeofenceID = details2.GeofenceID  
WHERE clients.RouteTemplateID = DB4010.dbo.EntityStagedData.EntityID), '') 

到將該值設置爲空白值。

如果你希望得到儘可能多的信息,可能的話,在COALESCE報表包裝每個單獨的部分,除去NULL值,如:

SELECT   COALESCE(geo.City, '') + ' ' + COALESCE(geo.Description, '') ... 

這樣一來,如果在子選擇你的價值觀之一是NULL,您不會因串聯而得到NULL值。