Table Variables with an Alias in a Delete From Statement

后端 未结 2 881
慢半拍i
慢半拍i 2021-02-03 18:35

I want to delete rows from a SQL Server 2000/2005 table variable based on the presence of other rows in the same table (delete all 0 count rows if a non-0 count row exists with

2条回答
  •  后悔当初
    2021-02-03 19:15

    Try this, it ought to work (the first FROM is optional):

    DELETE [FROM] @O
    FROM @O o1
    where ACount = 0
    and exists (select Month from @O o2
          where o1.Month = o2.Month and o2.ACount > 0)
    

    The rationale is: DELETE, as explained here, expects a non-aliased table first, an optional FROM can precede it. After that you do can put an alias on a table in the second FROM, if you need to do a JOIN, subquery, etc.

提交回复
热议问题