Wednesday, January 15, 2020

T-SQL: CURSOR

drop table #Practitioner;
   select userId as PractitionerId into #Practitioner from tblUsers
  where userStatus = 'Needs to be researched';


DECLARE @PractitionerId NVARCHAR(MAX)

DECLARE MY_CURSOR CURSOR
  LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT DISTINCT PractitionerId
FROM #Practitioner

OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @PractitionerId
WHILE @@FETCH_STATUS = 0
BEGIN
    --Do something with Id here
    PRINT @PractitionerId
    FETCH NEXT FROM MY_CURSOR INTO @PractitionerId
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR

No comments: