How can I speed up this Fetch next cursor? Below is my code. Is their another way to perform this task more efficently. I am running this task against 3 million records where I convert varchar(10) to a datetime. I would appreciate your help
/*Declare Variables*/
DECLARE @PERIMAGE_DATE varchar(10), @FCN varchar(13), @PERIMAGE_ID varchar(13),
@DOB_MO smallint, @DOB_DAY smallint, @DOB_YEAR smallint, @BIRTH char(8),
@MONTH char(2), @DAY char(2), @YEAR char(4), @BIRTH_DATE datetime
/*Create a forward only cursor */
DECLARE update_dob CURSOR FOR
SELECT FCN, PERIMAGE_DATE, PERIMAGE_ID
FROM Los_Angeles_CALPHOTO.dbo.FCNPERIMAGETemp WHERE PERIMAGE_DATE is not null
/*Begin moving dob records.*/
OPEN update_dob
FETCH update_dob INTO @FCN, @PERIMAGE_DATE, @PERIMAGE_ID
WHILE (@@fetch_status = 0)
BEGIN
--print 'here'
IF (@PERIMAGE_DATE is Not Null)
Begin
/* Create a Birthdate*/
IF (@PERIMAGE_DATE is not Null)
SET @MONTH = substring(@PERIMAGE_DATE, 5, 2)
/*Thirty days hath September*/
IF (@PERIMAGE_DATE is not Null)
SET @DAY = substring(@PERIMAGE_DATE, 7, 2)
IF (@PERIMAGE_DATE is not Null)
SET @YEAR = substring(@PERIMAGE_DATE, 1, 4)
/*Update the record*/
UPDATE CALPHOTO.dbo.Perimage
SET PERIMAGE_DATE = Cast((@YEAR + @MONTH + @DAY) as datetime)
WHERE CALPHOTO.dbo.Perimage.CP_FCN like 'LASD%' and CALPHOTO.dbo.Perimage.FCN = @FCN and CALPHOTO.dbo.Perimage.PERIMAGE_ID = @PERIMAGE_ID and CALPHOTO.dbo.Perimage.PERIMAGE_DATE is null
End
Delete LOS_ANGELES_CALPHOTO.dbo.FCNPerimagetemp
Where FCN = @FCN and PERIMAGE_ID = @PERIMAGE_ID
FETCH next from update_dob INTO @FCN, @PERIMAGE_DATE, @PERIMAGE_ID
END
CLOSE update_dob
DEALLOCATE update_dob
GO
/*Declare Variables*/
DECLARE @PERIMAGE_DATE varchar(10), @FCN varchar(13), @PERIMAGE_ID varchar(13),
@DOB_MO smallint, @DOB_DAY smallint, @DOB_YEAR smallint, @BIRTH char(8),
@MONTH char(2), @DAY char(2), @YEAR char(4), @BIRTH_DATE datetime
/*Create a forward only cursor */
DECLARE update_dob CURSOR FOR
SELECT FCN, PERIMAGE_DATE, PERIMAGE_ID
FROM Los_Angeles_CALPHOTO.dbo.FCNPERIMAGETemp WHERE PERIMAGE_DATE is not null
/*Begin moving dob records.*/
OPEN update_dob
FETCH update_dob INTO @FCN, @PERIMAGE_DATE, @PERIMAGE_ID
WHILE (@@fetch_status = 0)
BEGIN
--print 'here'
IF (@PERIMAGE_DATE is Not Null)
Begin
/* Create a Birthdate*/
IF (@PERIMAGE_DATE is not Null)
SET @MONTH = substring(@PERIMAGE_DATE, 5, 2)
/*Thirty days hath September*/
IF (@PERIMAGE_DATE is not Null)
SET @DAY = substring(@PERIMAGE_DATE, 7, 2)
IF (@PERIMAGE_DATE is not Null)
SET @YEAR = substring(@PERIMAGE_DATE, 1, 4)
/*Update the record*/
UPDATE CALPHOTO.dbo.Perimage
SET PERIMAGE_DATE = Cast((@YEAR + @MONTH + @DAY) as datetime)
WHERE CALPHOTO.dbo.Perimage.CP_FCN like 'LASD%' and CALPHOTO.dbo.Perimage.FCN = @FCN and CALPHOTO.dbo.Perimage.PERIMAGE_ID = @PERIMAGE_ID and CALPHOTO.dbo.Perimage.PERIMAGE_DATE is null
End
Delete LOS_ANGELES_CALPHOTO.dbo.FCNPerimagetemp
Where FCN = @FCN and PERIMAGE_ID = @PERIMAGE_ID
FETCH next from update_dob INTO @FCN, @PERIMAGE_DATE, @PERIMAGE_ID
END
CLOSE update_dob
DEALLOCATE update_dob
GO