sql >> Base de Datos >  >> RDS >> Sqlserver

¿Puedo recorrer una variable de tabla en T-SQL?

Agregue una identidad a la variable de su tabla y realice un ciclo sencillo desde 1 hasta @@ROWCOUNT de INSERT-SELECT.

Prueba esto:

DECLARE @RowsToProcess  int
DECLARE @CurrentRow     int
DECLARE @SelectCol1     int

DECLARE @table1 TABLE (RowID int not null primary key identity(1,1), col1 int )  
INSERT into @table1 (col1) SELECT col1 FROM table2
SET @[email protected]@ROWCOUNT

SET @CurrentRow=0
WHILE @CurrentRow<@RowsToProcess
BEGIN
    SET @[email protected]+1
    SELECT 
        @SelectCol1=col1
        FROM @table1
        WHERE [email protected]

    --do your thing here--

END