插入多行存储过程时增加变量

时间:2015-04-02 17:17:52

标签: sql-server-2008 stored-procedures

我试图在插入多行时增加变量。

这是我的最后一次尝试。

DECLARE @i  int
Set @i=0
Insert into winners (ContestantID, DrawingID, SlotNumber) 
SELECT TOP (@Total) ID, @DrawingID, @i + 1  FROM Contestants 
where Drawingid=@DrawingID ORDER BY NEWID()

1 个答案:

答案 0 :(得分:0)

使用ROW_NUMBER函数:

INSERT INTO winners (ContestantID
    , DrawingID
    , SlotNumber) 
SELECT TOP (@Total) ID
    , @DrawingID
    , ROW_NUMBER() OVER (ORDER BY ID)  
FROM Contestants 
WHERE Drawingid = @DrawingID
ORDER BY NEWID()