SQL While traversing a table

0

Friends I'm trying to run a table from SQL but for some reason I do not get the data for each row of my table. Can someone tell me what I'm doing wrong?

declare @count int = 0,
  @countb int = 1
set @count = (select count( * ) from stats_ddl)
while @countb <= @count
begin
declare @fechaMov varchar(50) = (select top(1) fecha + hora + importe from stats_ddl)
print 'fecha' + @fechaMov + ' ' + convert(varchar, @countb)
set @countb = @countb + 1
end
    
asked by E.Rawrdríguez.Ophanim 22.08.2018 в 22:34
source

1 answer

2

Friend tries to do this, you must clone your table in a temporary table and iterate:

DECLARE @count INT;

CREATE TABLE #stats_ddl(
    fecha DATETIME, 
    hora TIME,
    importe DECIMAL
);

INSERT INTO #stats_ddl 
SELECT * FROM stats_ddl

SELECT @count = COUNT(*) FROM #stats_ddl;

WHILE @count > 0
BEGIN
    DECLARE @fechaMov VARCHAR(50) = (SELECT TOP(1) fecha + hora + importe FROM #stats_ddl);
    PRINT 'fecha' + @fechaMov + ' ' + CONVERT(VARCHAR, @count);
    DELETE TOP (1) FROM #stats_ddl
    SELECT @count = COUNT(*) FROM #stats_ddl;
END

DROP TABLE #stats_ddl;
    
answered by 22.08.2018 / 23:19
source