Table Variables with an Alias in a Delete From Statement

后端 未结 2 882
慢半拍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 18:53

    Specify the alias name before FROM statement Meaning, you are deleting from the aliased table.

    delete o1
    from   @O as o1
    where  ACount = 0 
           and exists ( select  Month 
                        from    @O o2 
                        where   o1.Month = o2.Month 
                                and o2.ACount > 0)
    


    Result

    alt text

提交回复
热议问题