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
 SQL Server Development (2000)
 db transaction error

Author  Topic 

PeterG
Posting Yak Master

156 Posts

Posted - 2003-11-04 : 16:12:32
When I run this stored proc, I get this error:
"The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION." I'm not sure wht the rules are for using transaction...


SET NOCOUNT ON

DECLARE ...

-- append a separator at the end of the array
SET @compNums = @compNums + @separator
SET @frequencies = @frequencies + @separator
SET @duedates = @duedates + @separator
SET @cycledates = @cycledates + @separator

while patindex('%' + @separator + '%' , @compNums) <> 0
BEGIN
select @separator_comp = patindex('%' + @separator + '%' , @compNums)
select @separator_date = patindex('%' + @separator + '%' , @duedates)
select @separator_freq = patindex('%' + @separator + '%' , @frequencies)
select @separator_cycle = patindex('%' + @separator + '%' , @cycledates)

--get the member element
select @compNum = left(@compNums , @separator_comp - 1)
select @duedate = left(@duedates , @separator_date - 1)
select @freq = left(@frequencies , @separator_freq - 1)
select @cycle = left(@cycledates , @separator_cycle - 1)

SET @bookexists = NULL
-- check if PDF request exists in AutoRun for company
SELECT @bookexists = 1 FROM dbo.AutoRun WHERE CompNum = @compNum AND FileType = 'PDFSTMT'

IF @stmt = 1 --stmt requested
BEGIN
IF @bookexists IS NULL --record doesn't exist in AutoRun table
BEGIN
IF @duedate <> '' OR @duedate = '0'
BEGIN

BEGIN TRANSACTION

INSERT INTO dbo.AutoRun(ICN, FileType, CompNum, Frequency, DueDate)
VALUES (@icn, 'PDFSTMT', @compNum, @freq, @duedate)

IF @@ERROR != 0 GOTO ERROR_HANDLER

Update dbo.CompNames
Set AutomateStmts = 1
Where CompNum = @compNum AND InternetNumber = @icn

IF @@ERROR != 0 GOTO ERROR_HANDLER

IF @freq = 'CDPLUS'
BEGIN
--if submission (today) is past the cycle date
IF (SELECT DATEDIFF(d, GETDATE(), (@cd))) <= 0
BEGIN
--if today's date is less than or equal to the cycle date + duedate, insert or update record in FileAdmin table with the rundate as cycledate+duedate
IF (SELECT DATEDIFF(d,GETDATE(),(@cd + CAST(@duedate as int)))) >= 0
BEGIN
--if record exists in FileAdmin, update rundate
IF Exists (Select 1 from dbo.Fileadmin Where txtCompanyNum = @compNum And blnExported = 0 And txtFileType = 'PDFSTMT' And blnAutomatedRequest = 1)
BEGIN
Update dbo.FileAdmin
Set dteRunDate = @rundate
Where txtCompanyNum = @compNum And blnExported = 0 And txtFileType = 'PDFSTMT' And blnAutomatedRequest = 1

IF @@ERROR != 0 GOTO ERROR_HANDLER
END
ELSE -- record doesn't exist in FileAdmin, do an insert
BEGIN
INSERT INTO dbo.FileAdmin (txtCompanyNum, txtAddOpt, dteCycleDate, dteLastUsed, blnExported, txtInternetNum, txtStatus, dteRunDate, txtFileType, blnAutomatedRequest )
VALUES (@compNum, '', @cd, GETDATE(), 0, @icn, '', @rundate , 'PDFSTMT', 1 )

IF @@ERROR != 0 GOTO ERROR_HANDLER
END
END
END
END
COMMIT TRANSACTION

END
END
ELSE --record already exists in AutoRun table
BEGIN
IF @duedate <> '' Or @duedate = '0'
BEGIN

BEGIN TRANSACTION

UPDATE dbo.AutoRun
SET DueDate = CAST(@duedate as int), Frequency = @freq
WHERE CompNum = @compNum AND FileType = 'PDFSTMT'

IF @@ERROR != 0 GOTO ERROR_HANDLER

--does record already exist in FileAdmin?
IF Exists (Select 1 from dbo.Fileadmin Where txtCompanyNum = @compNum And blnExported = 0 And txtFileType = 'PDFSTMT' And blnAutomatedRequest = 1)
BEGIN
IF @freq = 'CDPLUS'
BEGIN
--if today's date is less than or equal to the cycle date + duedate, update record in FileAdmin table with the rundate as cycledate+duedate
IF (SELECT DATEDIFF(d,GETDATE(),(@cd + CAST(@duedate as int)))) >= 0
BEGIN
Update dbo.FileAdmin
Set dteRunDate = @rundate
Where txtCompanyNum = @compNum And blnExported = 0 And txtFileType = 'PDFSTMT' And blnAutomatedRequest = 1

IF @@ERROR != 0 GOTO ERROR_HANDLER
END
END
ELSE --DOM
BEGIN
Delete From dbo.FileAdmin
Where txtCompanyNum = @compNum And blnExported = 0 And txtFileType = 'PDFSTMT' And blnAutomatedRequest = 1

IF @@ERROR != 0 GOTO ERROR_HANDLER
END
END

COMMIT TRANSACTION

END
ELSE --due date is empty
BEGIN

BEGIN TRANSACTION

DELETE FROM dbo.AutoRun
WHERE CompNum = @compNum AND FileType = 'PDFSTMT'

IF @@ERROR != 0 GOTO ERROR_HANDLER

UPDATE dbo.CompNames
Set AutomateStmts = 0
Where CompNum = @compNum AND InternetNumber = @icn

IF @@ERROR != 0 GOTO ERROR_HANDLER

DELETE From dbo.FileAdmin
Where txtCompanyNum = @compNum And blnExported = 0 And txtFileType = 'PDFSTMT' And blnAutomatedRequest = 1

IF @@ERROR <> 0 GOTO ERROR_HANDLER

COMMIT TRANSACTION

END
END
END
ELSE --stmt not requested
BEGIN
IF @bookexists = 1
BEGIN

BEGIN TRANSACTION

DELETE FROM dbo.AutoRun
WHERE CompNum = @compNum AND FileType = 'PDFSTMT'

IF @@ERROR != 0 GOTO ERROR_HANDLER

UPDATE dbo.CompNames
Set AutomateStmts = 0
Where CompNum = @compNum AND InternetNumber = @icn

IF @@ERROR != 0 GOTO ERROR_HANDLER

DELETE From dbo.FileAdmin
Where txtCompanyNum = @compNum And blnExported = 0 And txtFileType = 'PDFSTMT' And blnAutomatedRequest = 1

IF @@ERROR != 0 GOTO ERROR_HANDLER

COMMIT TRANSACTION

END
END

--remove member element from the array
select @compNums = stuff(@compNums, 1, @separator_comp, '')
select @duedates = stuff(@duedates, 1, @separator_date, '')
select @frequencies = stuff(@frequencies, 1, @separator_freq, '')
select @cycledates = stuff(@cycledates, 1, @separator_cycle, '')

END


ERROR_HANDLER:
ROLLBACK TRANSACTION
SET NOCOUNT OFF

SET NOCOUNT OFF

GO

X002548
Not Just a Number

15586 Posts

Posted - 2003-11-04 : 16:25:19
Well, it looks like no matter what happens, you ROLLBACK

You need A BEGIN TRAN and a logically matching COMMIT TRAN OR ROLLBACK TRAN

You can't COMMIT TRAN, then logically fall through a ROLLBACK

Any reason you can't treat this whole thing as 1 TRAN?

BEGIN TRAN at the top and have to exits, one for success, and one for failure....

I usually fall though tp the commit and have a housekeeping exit the both success and failure navigate through

Like



BEGIN TRAN

...code

SELECT @Result_Count = @@ROWCOUNT, @error_out = @@error

If @Error_Out <> 0
BEGIN
Select @Error_Loc = 51
Select @Error_Type = 50001
GOTO Load_Ledger_Init_sp_Error
END

If @Result_Count = 0
BEGIN
SELECT @Error_Loc = 52
SELECT @Error_Message = 'No Rows Inserted in to the BATCH_CNTL Table For Third Months File'
SELECT @Error_Type = 50002
GOTO Load_Ledger_Init_sp_Error
END


COMMIT TRAN

Load_Ledger_Init_sp_Exit:

SET NOCOUNT OFF

return @Comp_Code

Load_Ledger_Init_sp_Error:
ROLLBACK TRAN

Select @Comp_Code = -1, @Comp_msg = @Error_Message, @Load_Ind = 'N'

-- Values set during error handling in the code

If @Error_Type = 50001
BEGIN
Select @error_message = (Select 'Location: ' + RTRIM(Convert(char(3),@Error_Loc))
+ ' @@ERROR: ' + RTRIM(Convert(char(6),error))
+ ' Severity: ' + RTRIM(Convert(char(3),severity))
+ ' Message: ' + RTRIM(description)
From master..sysmessages
Where error = @error_out)
END
If @Error_Type = 50002
BEGIN
Select @Error_Message = 'Location: ' + RTRIM(Convert(char(3),@Error_Loc))
+ ' Severity: UserLevel '
+ ' Message: ' + RTRIM(@Error_Message)
END

RAISERROR @Error_Type @Error_Message

GOTO Load_Ledger_Init_sp_Exit
GO






Brett

8-)
Go to Top of Page

PeterG
Posting Yak Master

156 Posts

Posted - 2003-11-04 : 16:50:06
Ok, I treated the whole thing as 1 tran by moving the Begin tran before the if statement and the commit tran at the end. I also added a goto sp_exit statement in the error_handler. It still gave me an error.

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-11-04 : 18:12:34
PeterG,

If you repost your code with the modifications that you made, we will be able to help you. Looking at your code as it stands now though, it is going to enter ERROR_HANDLER section regardless of error. The rollback command needs to be part of the IF @@ERROR <> 0 and not in the ERROR_HANDLER section. You can also remove the first SET NOCOUNT OFF because on error it will reach it and not on error it will reach it. Meaning, even if the code doesn't error out, it will still reach the ERROR_HANDLER section. It doesn't skip over it.

Tara
Go to Top of Page

PeterG
Posting Yak Master

156 Posts

Posted - 2003-11-04 : 18:36:21
Thanks for your inputs. Tara, that's what I exactly did (instead of calling the ERROR_HANDLER section I do a rollback transaction if @@ERROR <> 0). I don't have the error handler section anymore, and it's now working fine.
Go to Top of Page
   

- Advertisement -