我正在尝试创建一个 SQL While 循环,它将使用另一个表中的值更新临时表。另一个表中的值:
477286
560565
499330
391827
127375
526354
501736
357359
410433
500946
261297
377667
135931
235691
247239
143672
548752
471945
...
Run Code Online (Sandbox Code Playgroud)
但是,编写了以下内容,它只会多次插入最后一个值。
这是代码:
USE Reports
GO
CREATE TABLE #TempTable (CreatedByID int, LastUpdatedByID int, ID int,
AlertDE int, Alert char(50), StartDTTM datetime, EndDTTM datetime,
IsInactiveFLAG char(1),AlertDetails char(1));
DECLARE @numrows INT
SELECT @numrows = COUNT(*) FROM [Reports].[dbo].[Eligible]
DECLARE @id int
DECLARE @LoopCount INT = 1
DECLARE @count int = @numrows
SELECT @id = [id] FROM [Reports].[dbo].[Eligible]
WHILE (@LoopCount <= @count) …Run Code Online (Sandbox Code Playgroud)