内部复杂SELECT的更新

时间:2012-06-28 20:25:15

标签: sql sql-server tsql

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
  无法将值NULL插入   列'EntityData',表'DB4010.dbo.EntityStagedData';专栏   不允许空值。更新失败。

我不能,为了我的生活,弄清楚如何从内部Select语句的结果更新+ Concatenate“EntityData”...

2 个答案:

答案 0 :(得分: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值。

答案 1 :(得分:2)

我做了一些改变:

  • 单字母别名以提高可读性
  • 修复专有的SQL Server UPDATE FROM语法
  • 消除不相关的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;