使用Temp表列从主表中删除行?

时间:2013-09-23 18:09:11

标签: sql-server tsql sql-server-2008-r2 cursor sql-delete

我必须使用@flow从主表中删除## BeDel中的stackID。

#BeDel

StackID
45
56
48
56
49


DECLARE @flow int
@flow=(SELECT FLOWID FROM MainTable WHERE FLOWNAME='AAA')

MainTable

StackID   VALUE  FLOWID   FLOWNAME   
67         34     1          AAA
45         56     1          AAA
56         22     1          AAA
34         56     1          ZZZ

我已经使用了游标,但我不想因为性能原因而使用游标。

我的光标代码是

DECLARE Rotation CURSOR 
FOR 
                  SELECT StackID FROM #BeDel
                   DECLARE  StackID INT

                   OPEN Rotation
                   FETCH NEXT FROM Rotation INTO @StackID
                   While @@FETCH_STATUS = 0
                   BEGIN
                   DELETE MainTable
                   WHERE StackID=@StackID AND FLOWID=@@flow

                   FETCH NEXT FROM Rotation INTO @StackID 

                   END

1 个答案:

答案 0 :(得分:1)

delete MainTable
from MainTable as m
where
    exists (select * from #BeDel as b where b.StackID = m.StackID) and
    m.FLOWNAME = 'AAA'