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 2005 Forums
 Transact-SQL (2005)
 Cursor processing

Author  Topic 

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2009-11-06 : 10:09:23
Hi All,

I have to write a T-SQL which would update number of tables based on the paraemters coming from cursor.

If any of the update/insert fails there should not be commit. There has to be rollback on all the tables in case of error and I need to record that in a errortable. And then the cursor shoudl move on to the next record fetch.

Can anyone tell me how to trap the error code in this case?

Thanks,
-S

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-06 : 10:27:39
Look at TRY CATCH.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2009-11-06 : 10:28:03
Can you give me an example of it?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-06 : 10:31:11
http://msdn.microsoft.com/en-us/library/ms175976(SQL.90).aspx


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2009-11-06 : 10:41:59
Do you have a sample of it in cursor where multiple updates are happening?
Go to Top of Page

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2009-11-06 : 12:13:12
Here is my existing code - Hope that will help. Thanks in advance for any help


DECLARE @Location INT,
@taskJ INT,
@@ProjectID INT,
@@ErrorCode INT

CREATE TABLE #Errortasks (LocationID INT, taskJ INT)

SELECT DISTINCT s.Location, s.CPPS_task-- ,j.SiteId,j.JobID
INTO #NewGPMtasks
FROM Orders s (NOLOCK)
WHERE s.Location > 0

--BEGIN TRANSACTION

DECLARE Cur_taskJ2 CURSOR FOR
Select Location, taskj
FROM #NewGPMtasks

--Open cursor
OPEN Cur_taskJ2
FETCH NEXT FROM Cur_taskJ2 INTO @Location, @taskJ

WHILE @@FETCH_STATUS = 0
BEGIN

IF EXISTS (SELECT Product FROM Products WHERE SiteID = @Location and Tasks = @taskJ)
BEGIN

BEGIN TRANSACTION

UPDATE LocationChart
SET Numeric1 = Numeric1 + 1
WHERE Location = 1
AND Control = 40

SET @@ErrorCode = @@Error

IF @@ErrorCode <> 0
GOTO OnExit

--Insert a new Step
INSERT INTO OrderSteps
(Location, Details, Tasks, CDate)
VALUES(@Location, '', @taskJ, getdate())

SET @@ErrorCode = @@Error

IF @@ErrorCode <> 0
GOTO OnExit

COMMIT TRANSACTION
END
ELSE
BEGIN
BEGIN TRANSACTION

INSERT INTO LocationMaster
(Location, Description, CDate)
VALUES(@Location, '', getdate())


SET @@ErrorCode = @@Error

IF @@ErrorCode <> 0
GOTO OnExit

INSERT INTO OrderSteps
(Location, Details, Tasks, CDate)
VALUES(@Location, '', @taskJ, getdate())

SET @@ErrorCode = @@Error

IF @@ErrorCode <> 0
GOTO OnExit

COMMIT TRANSACTION

END

FETCH NEXT FROM Cur_taskJ2 INTO @Location, @taskJ

END

CLOSE Cur_taskJ2
DEALLOCATE Cur_taskJ2

OnExit:
ROLLBACK TRANSACTION
INSERT INTO #Errortasks VALUES (@Location, @taskJ)

CLOSE Cur_taskJ2
DEALLOCATE Cur_taskJ2
Go to Top of Page
   

- Advertisement -