我试图在插入多行时增加变量。
这是我的最后一次尝试。
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()
答案 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()