从UPDATE SET引用CASE中的子查询别名

时间:2015-07-27 20:51:15

标签: sql sql-server tsql

任何人都可以帮助我理解为什么这个操作

UPDATE
    [Case_11268].[dbo].[Annotations]
SET
    [usercreated_id] = CASE 
        WHEN 
             EXISTS (SELECT id from gu where anno.[usercreated_id] = gu.id)
        THEN 
            'SOMEVALUE'
        ELSE 
            [usercreated_id] 
        END
FROM (
    SELECT 
        agu.userid, us.id 
    FROM
        [Case_11268].[dbo].[Annotations] anno
    INNER JOIN 
        [TSI_Main].[dbo].aspnet_Users us
    INNER JOIN 
        [TSI_Main].[dbo].aspnet_Membership mem on us.userid = mem.userid
    INNER JOIN
        [TSI_Main].[dbo].AdminGroupUsers agu on mem.userid = agu.userid
    INNER JOIN
        [TSI_Main].[dbo].AdminGroups ag on agu.adminGroup = ag.id
    WHERE
        ag.[deleted] = 0
    GROUP BY agu.userid, us.id
) AS gu

引发此错误:

  

Msg 208,Level 16,State 1,Line 6
  无效的对象名称' gu'。

1 个答案:

答案 0 :(得分:3)

几乎可以肯定这是一种更为简单的写作方式。可能

UPDATE [Case_11268].[dbo].[Annotations]
SET    [usercreated_id] = 'SOMEVALUE'
WHERE  [usercreated_id] IN (SELECT us.id
                            FROM   [TSI_Main].[dbo].aspnet_Users us
                                   INNER JOIN [TSI_Main].[dbo].aspnet_Membership mem
                                           ON us.userid = mem.userid
                                   INNER JOIN [TSI_Main].[dbo].AdminGroupUsers agu
                                           ON mem.userid = agu.userid
                                   INNER JOIN [TSI_Main].[dbo].AdminGroups ag
                                           ON agu.[adminGroup] = ag.id
                            WHERE  ag.[deleted] = 0) 

但关于你的问题,移动到CTE将其放在范围内。

WITH gu
     AS (SELECT agu.userid,
                us.id
         FROM   [TSI_Main].[dbo].aspnet_Users us
                INNER JOIN [TSI_Main].[dbo].aspnet_Membership mem
                        ON us.userid = mem.userid
                INNER JOIN [TSI_Main].[dbo].AdminGroupUsers agu
                        ON mem.userid = agu.userid
                INNER JOIN [TSI_Main].[dbo].AdminGroups ag
                        ON agu.[adminGroup] = ag.id
         WHERE  ag.[deleted] = 0
         GROUP  BY agu.userid,
                   us.id)
UPDATE anno 
SET    [usercreated_id] = CASE
                            WHEN EXISTS (SELECT id
                                         FROM   gu
                                         WHERE  anno.[usercreated_id] = gu.id) THEN 'SOMEVALUE'
                            ELSE [usercreated_id]
                          END
FROM   [Case_11268].[dbo].[Annotations] anno 

当派生表紧接在别名之前时,只能选择派生表FROM。因此,您需要移动整个定义以在那里使用派生表。

UPDATE anno 
SET    [usercreated_id] = CASE
                            WHEN EXISTS (SELECT id
                                         FROM   (SELECT agu.userid,
                                                        us.id
                                                 FROM   [TSI_Main].[dbo].aspnet_Users us
                                                        INNER JOIN [TSI_Main].[dbo].aspnet_Membership mem
                                                                ON us.userid = mem.userid
                                                        INNER JOIN [TSI_Main].[dbo].AdminGroupUsers agu
                                                                ON mem.userid = agu.userid
                                                        INNER JOIN [TSI_Main].[dbo].AdminGroups ag
                                                                ON agu.[adminGroup] = ag.id
                                                 WHERE  ag.[deleted] = 0
                                                 GROUP  BY agu.userid,
                                                           us.id) AS gu
                                         WHERE  anno.[usercreated_id] = gu.id) THEN 'SOMEVALUE'
                            ELSE [usercreated_id]
                          END
FROM   [Case_11268].[dbo].[Annotations] anno