从别名表中删除不起作用(但选择确实)

时间:2015-05-04 15:33:59

标签: sql-server alias

我需要从表中删除一些在其他类似表中不可用的行。我可以使用这段代码正确选择要删除的行

SELECT *
FROM [DB01].[forward].[forward_value] as DB01
WHERE Id_forward like 'test' and
NOT EXISTS (SELECT *
    FROM [DB02].[db_marketdata].[forward].[forward_value] as DB02
    WHERE DB01.Id_Forward = DB02.Id_Forward and
        DB01.Id_Block = DB02.Id_Block and
        DB01.Id_PriceType = DB02.Id_PriceType and
)

当我将SELECT (*)更改为DELETE时,我收到错误,因为我无法对第一个表进行别名。

  

Msg 156,Level 15,State 1,Line 2

     

关键字'附近的语法错误为'。

如何在不使用别名的情况下删除这些值以正确选择行?

2 个答案:

答案 0 :(得分:4)

好的,你可以使用这样的别名

DELETE DB01
FROM [DB01].[forward].[forward_value] AS DB01
WHERE Id_forward LIKE 'test'
    AND NOT EXISTS (
        SELECT *
        FROM [DB02].[db_marketdata].[forward].[forward_value] AS DB02
        WHERE DB01.Id_Forward = DB02.Id_Forward
            AND DB01.Id_Block = DB02.Id_Block
            AND DB01.Id_PriceType = DB02.Id_PriceType
            AND
        )

答案 1 :(得分:2)

将您的别名放在DELETE语法中,如下所示:

delete DB01
FROM [DB01].[forward].[forward_value] as DB01
WHERE Id_forward like 'test' and
NOT EXISTS (SELECT *
    FROM [DB02].[db_marketdata].[forward].[forward_value] as DB02
    WHERE DB01.Id_Forward = DB02.Id_Forward and
        DB01.Id_Block = DB02.Id_Block and
        DB01.Id_PriceType = DB02.Id_PriceType 
)