I have a stored procedure in SQL 7 which runs fine when called from Query
Analyzer, but fails when it is called as a scheduled job task.
The sp (Customer_Updat eLastEntry_All) gets the last "event" for each
customer from the CustomerEvents table using a cursor (curItems), then loops
through each of those, and, for each one, gets the data from that event
record, using a non-looping cursor (curEvents). Finally, with the data from
curEvents, it calls another sp (Customer_Updat eLastEntry_With Date), passing
the data to it, which then updates the Customer table based on the input it
receives.
As noted, this sp runs fine when called from QA. However, when run as a job
in SQL Server Agent, I get the message,
"A cursor with the name 'curEvents' does not exist. [SQLSTATE 34000] (Error
16916) A cursor with the name 'curEvents' does not exist. [SQLSTATE 34000]
(Error 16916). The step failed."
The sp and the one it calls are below. Any ideas are appreciated.
Thanks,
Neil
------------------------------------------------------------------------
Alter Procedure Customer_Update LastEntry_All
AS
SET NOCOUNT ON
declare @EventID int
declare @CustID int
declare @Date smalldatetime
declare @EntryType varchar(1)
declare @ActivityLogTyp e varchar(50)
declare @Amount float
declare @Notes varchar
declare curItems Cursor Local Forward_Only
For
SELECT Max(ID) AS LastEventID
FROM CustomerEvents
GROUP BY CustID
Open curItems
Fetch Next From curItems
Into @EventID
If @EventID Is Null Goto return_here
While @@Fetch_Status = 0
Begin
declare curEvents Cursor Local Forward_Only
For
SELECT CustID, EntryDate, EntryType, ActivityLogType , Amount, Notes
FROM CustomerEvents
WHERE ID = @EventID
Open curEvents
Fetch Next From curEvents Into @CustID, @Date , @EntryType ,
@ActivityLogTyp e , @Amount , @Notes
If @@Fetch_Status <> 0 Goto on_error
Exec Customer_Update LastEntry_WithD ate @CustID, @Date , @EntryType ,
@ActivityLogTyp e , @Amount , @Notes
Close curEvents
Deallocate curEvents
Fetch Next From curItems
Into @EventID
End
Close curItems
Deallocate curItems
Close curEvents
Deallocate curEvents
return_here:
Select -1 As ReturnVal
RETURN(0)
on_error:
Select 0 As ReturnVal
RETURN(1)
----------------------------------------------
Alter Procedure Customer_Update LastEntry_WithD ate
@CustID int,
@Date smalldatetime,
@EntryType varchar(1),
@ActivityLogTyp e varchar(50),
@Amount float,
@Notes text
AS
SET NOCOUNT ON
Update Customer
Set LastEntryType =
Case @EntryType
When 'I' Then 'Invoice'
When 'A' Then @ActivityLogTyp e
When 'W' Then 'Want List'
End,
LastEntryAmt=
Case @EntryType
When 'I' Then @Amount
End,
LastEntryNotes=
Case
When @EntryType= 'A' or @EntryType= 'W' Then @Notes
End,
LastEntryDate=@ Date
Where ([INDEX] = @CustID)
IF @@ERROR <> 0 GOTO On_Error
Select -1 As ReturnVal
RETURN(0)
On_Error:
Select 0 As ReturnVal
RETURN(1)
Analyzer, but fails when it is called as a scheduled job task.
The sp (Customer_Updat eLastEntry_All) gets the last "event" for each
customer from the CustomerEvents table using a cursor (curItems), then loops
through each of those, and, for each one, gets the data from that event
record, using a non-looping cursor (curEvents). Finally, with the data from
curEvents, it calls another sp (Customer_Updat eLastEntry_With Date), passing
the data to it, which then updates the Customer table based on the input it
receives.
As noted, this sp runs fine when called from QA. However, when run as a job
in SQL Server Agent, I get the message,
"A cursor with the name 'curEvents' does not exist. [SQLSTATE 34000] (Error
16916) A cursor with the name 'curEvents' does not exist. [SQLSTATE 34000]
(Error 16916). The step failed."
The sp and the one it calls are below. Any ideas are appreciated.
Thanks,
Neil
------------------------------------------------------------------------
Alter Procedure Customer_Update LastEntry_All
AS
SET NOCOUNT ON
declare @EventID int
declare @CustID int
declare @Date smalldatetime
declare @EntryType varchar(1)
declare @ActivityLogTyp e varchar(50)
declare @Amount float
declare @Notes varchar
declare curItems Cursor Local Forward_Only
For
SELECT Max(ID) AS LastEventID
FROM CustomerEvents
GROUP BY CustID
Open curItems
Fetch Next From curItems
Into @EventID
If @EventID Is Null Goto return_here
While @@Fetch_Status = 0
Begin
declare curEvents Cursor Local Forward_Only
For
SELECT CustID, EntryDate, EntryType, ActivityLogType , Amount, Notes
FROM CustomerEvents
WHERE ID = @EventID
Open curEvents
Fetch Next From curEvents Into @CustID, @Date , @EntryType ,
@ActivityLogTyp e , @Amount , @Notes
If @@Fetch_Status <> 0 Goto on_error
Exec Customer_Update LastEntry_WithD ate @CustID, @Date , @EntryType ,
@ActivityLogTyp e , @Amount , @Notes
Close curEvents
Deallocate curEvents
Fetch Next From curItems
Into @EventID
End
Close curItems
Deallocate curItems
Close curEvents
Deallocate curEvents
return_here:
Select -1 As ReturnVal
RETURN(0)
on_error:
Select 0 As ReturnVal
RETURN(1)
----------------------------------------------
Alter Procedure Customer_Update LastEntry_WithD ate
@CustID int,
@Date smalldatetime,
@EntryType varchar(1),
@ActivityLogTyp e varchar(50),
@Amount float,
@Notes text
AS
SET NOCOUNT ON
Update Customer
Set LastEntryType =
Case @EntryType
When 'I' Then 'Invoice'
When 'A' Then @ActivityLogTyp e
When 'W' Then 'Want List'
End,
LastEntryAmt=
Case @EntryType
When 'I' Then @Amount
End,
LastEntryNotes=
Case
When @EntryType= 'A' or @EntryType= 'W' Then @Notes
End,
LastEntryDate=@ Date
Where ([INDEX] = @CustID)
IF @@ERROR <> 0 GOTO On_Error
Select -1 As ReturnVal
RETURN(0)
On_Error:
Select 0 As ReturnVal
RETURN(1)
Comment