Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Error running cursor in stored procedure

Author  Topic 

Antonio
Posting Yak Master

168 Posts

Posted - 2005-10-04 : 06:05:16
Hi all,

I am getting the following error when I attempt to use the FOR UPDATE syntax with my cursor.

FOR UPDATE cannot be specified on a READ ONLY cursor.

The code for my cursor is as follows :



Declare @LocalError integer
declare @CURRENT_DATETIME datetime
declare @LocalPackageName char(50)
declare @LocalProcessName char(50)
declare @LocalVersion integer
DECLARE @Cost_Centre_Number integer
DECLARE @Aggregation_Completed_Date integer
DECLARE @Stopped_Flag integer
DECLARE @Step_Start datetime
DECLARE @Step_End datetime
declare @Process_From_date integer
DECLARE @Target_date integer

SELECT @Step_Start = getdate()
select @LocalPackageName = 'AGGREGATION'
Select @LocalProcessName = 'Process_Weekly_Aggregation_Queue'
select @LocalVersion = 1
select @Stopped_Flag = 0

update [ETL_SERVER].ETlControl.dbo.TDL_TASKS
set STOP = 0
where PROCESSNAME = 'AGGREGATION_Q_PROCESSOR'

EXECUTE DW_WRITE_LOG
@Process = @LocalProcessName,
@Package = @LocalPackageName,
@Message = 'Process Started',
@Start = @Step_Start,
@VERSION = @LocalVersion,
@Keep = N,
@Error = @LocalError

declare cu_WEEKLY_Aggregation_Queue cursor FOR
SELECT * FROM WEEKLY_AGGREGATION_PROCESS_QUEUE2
where Aggregation_Completed_Date IS NULL
ORDER BY Cost_Centre_Number
FOR UPDATE

open cu_WEEKLY_Aggregation_Queue
fetch next from cu_WEEKLY_Aggregation_Queue INTO @Cost_Centre_Number, @Target_date , @Aggregation_Completed_Date
while @@fetch_status = 0 and @Stopped_Flag = 0
BEGIN
SELECT @Step_Start = getdate()
EXECUTE DW_WRITE_LOG
@Process = @LocalProcessName,
@Package = @LocalPackageName,
@Message = 'Start Loop',
@Start = @Step_Start,
@VERSION = @LocalVersion,
@Keep = N,
@Cost_Centre_Number = @Cost_Centre_Number,
@Error = @LocalError

EXEC @LocalError = AGG_WEEK_LOADER2 @Cost_Centre_Number, @Target_date


if @LocalError = 0
BEGIN
SELECT @Step_End = getdate()

EXECUTE DW_WRITE_LOG
@Process = @LocalProcessName,
@Package = @LocalPackageName,
@Message = 'Aggregated WEEK OK',
@Start = @Step_Start,
@end = @Step_End,
@VERSION = @LocalVersion,
@Keep = N,
@Cost_Centre_Number = @Cost_Centre_Number,
@Error = @LocalError

UPDATE WEEKLY_AGGREGATION_PROCESS_QUEUE2
SET Aggregation_Completed_Date = convert( char(10), getdate(), 112)
where CURRENT OF cu_WEEKLY_Aggregation_Queue
END

ELSE
BEGIN

SELECT @Step_End = getdate()

EXECUTE DW_WRITE_LOG
@Process = @LocalProcessName,
@Package = @LocalPackageName,
@Message = 'Aggregation ERROR',
@Start = @Step_Start,
@end = @Step_End,
@VERSION = @LocalVersion,
@Keep = Y,
@Cost_Centre_Number = @Cost_Centre_Number,
@Error = @LocalError
END


select @Stopped_Flag = STOP from [ETL_SERVER].ETlControl.dbo.TDL_TASKS where PROCESSNAME = 'AGGREGATION_Q_PROCESSOR'
fetch next from cu_WEEKLY_Aggregation_Queue INTO @Cost_Centre_Number, @Target_Date, @Aggregation_Completed_Date

END


close cu_WEEKLY_Aggregation_Queue
deallocate cu_WEEKLY_Aggregation_Queue


Your help would be much appreciated. Many thanks in advance.

Antonio
Posting Yak Master

168 Posts

Posted - 2005-10-04 : 06:17:35
Terribly sorry guys, I have just realised that I don't have an index on the table I am attempting to update.

Problem solved!
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2005-10-04 : 06:25:57
You "may" even be able to improve things far more, by eliminiating cursors.....the performance in general is VERY poor. Search here for previous discussions. There are few activities that CANNOT be done in a far more efficient manner...using set-based processing.
Go to Top of Page

Antonio
Posting Yak Master

168 Posts

Posted - 2005-10-04 : 09:17:57
quote:
Originally posted by AndrewMurphy

You "may" even be able to improve things far more, by eliminiating cursors.....the performance in general is VERY poor. Search here for previous discussions. There are few activities that CANNOT be done in a far more efficient manner...using set-based processing.



Hi Andrew, thanks for your advice. However, looking at the various processes that are already in place, I can not see how much of it can be done with out the use of cursors (or while loops). I am happy to explain the current process to see if any SQL gurus have suggestions for adapting the code to be set based rather than cursor based.

I have read sevral articles about using cursors and how to adapt code with cursors but for our data wahrehouse processes I can not see how the cursors can be eliminated to be honest.

Where to start?
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-10-04 : 10:00:52
if you have to execute different sprocs for each row of data and those sprocs can't be replaced with an UDF then cursors are the way to go.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Antonio
Posting Yak Master

168 Posts

Posted - 2005-10-04 : 10:10:22
quote:
Originally posted by spirit1

if you have to execute different sprocs for each row of data and those sprocs can't be replaced with an UDF then cursors are the way to go.

Go with the flow & have fun! Else fight the flow



Hi Spirit,

I am going to stick with using the cursor for the time being but will look into replacing this (hopefully) in the near future once I have got my aggregation process sorted out.

Thanks for your suggestion.
Go to Top of Page
   

- Advertisement -