Cursor Snippets

Actuarial Cursor
DECLARE actuarial_cursor CURSOR LOCAL FAST_FORWARD READ_ONLY FOR 
	SELECT DISTINCT [id_field], [date_field]
	FROM [ds].[table - potentially a table of staged data]
	ORDER BY [id_field], [date_field] DESC
;

 ---Iterate through records of query.-------------------------------------------------
OPEN actuarial_cursor
FETCH actuarial_cursor INTO @policy_no, @cert_eff_dt
WHILE(@@FETCH_status = 0)
BEGIN
		SELECT DISTINCT TOP 1 
		@id_field = [id_field],
		@id_field2 = [st].[id_field2],
		@id_field3 = [st].[id_field3]
		FROM [dbo].[source_table] st
		WHERE [id_field] = @id_field
		SET @thisdate = @date_field
		PRINT @id_field
		PRINT @id_field + ' ' + CAST(@date_field AS char(20))

		UPDATE [ds].[table - potentially a table of staged data]
		SET [nh_infl_mdb] = [pbl].[mdb],
			[nh_infl_lifemax] = [pbl].[lifemax] 
		
		FROM [ds].[table - potentially a table of staged data] stage
		JOIN [dbo].[source_table]( @id_field3, @id_field2, @thisdate, 1 ) st ON stage.id_field = st.id_field
		
		
		WHERE [id_field] = @id_field
		      AND [stage].[id_field] = @id_field AND [stage].[date_field] = @date_field  
	FETCH actuarial_cursor INTO @id_field, @date_field
END
;