I would like to create the equivalent of a FOR loop. I need this in SQL. My goal is to create a new column called Difference which calculates the date difference in days between the end of each row and the beginning of the next row. In essence I need to know if there are any breaks in contracts for each personID. RowID from min to max defines a contract with a specific organisation, such as each new contract with one organisation starts with 1.
My table is:
The SQL code I wrote is:
select RowID, start_contract, end_contract from table
open the_cursor
fetch next from the_cursor into @id
while @@FETCH_STATUS = 0
begin
select
DATEDIFF(DAY, (select end_contract from table where RowID = @id-1),
(select start_contract from t where RowID = @id)) AS [Difference]
if (select RowID from t) = 1
break
else
continue
fetch next from the_cursor into @id
end
close the_cursor
deallocate the_cursor
But I get an error:
Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.
Could anybody help me please?
Thank you so much.

FORloop in SQL: stackoverflow.com/questions/13565093/…