Sqlserver
 SQL >> Base de données >  >> RDS >> Sqlserver

Parcourir une variable de table T-SQL

Ajoutez une colonne IDENTITY et bouclez de 1 à @@ROWCOUNT.

DECLARE @table1 TABLE (
 RowID int NOT NULL PRIMARY KEY IDENTITY(1,1),
 col1 int
);

INSERT INTO @table1(col1)
SELECT col1 FROM table2;

DECLARE @RowsToProcess int = @@ROWCOUNT;
DECLARE @CurrentRow int = 0;

WHILE @CurrentRow < @RowsToProcess
BEGIN
 SET @CurrentRow += 1;
 SELECT col1 FROM @table1 WHERE RowID = @CurrentRow;
END;