beechner78
4/13/2019 - 9:22 PM

simple cursor example

declare @userId bigint;

DECLARE my_cursor CURSOR FOR
select userId from users where userdetailId is NULL;

OPEN my_cursor
FETCH NEXT FROM my_cursor INTO @userId

WHILE @@FETCH_STATUS = 0
BEGIN
	insert into UserDetail DEFAULT VALUES;
	update Users set UserDetailId = (SELECT SCOPE_IDENTITY()) where UserId = @userId;
	FETCH NEXT FROM my_cursor INTO @userId
END

CLOSE my_cursor
DEALLOCATE my_cursor
GO