473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Commit Transaction Even on Trigger Failure

I am trying to pull data out of an application database and transform it to another medium.
I have direct access to the database, but I cannot alter the program code.

What I want to have happen is that when an insert occurs on a particular table, execute a statement that will put the data it needs into a string and export the file.

I have the code working with one small problem. If something goes wrong in my code (like a conversion error, or the file server is not available), it actually does a ROLLBACK TRANSACTION on the initial insert statement. This is bad because it causes a missing reference in the application database.

What I'd like to have happen is that even if my trigger fails, go ahead and insert the record.

Here's what I have so far:
Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE trigger [trg_CIM_WO_Issue]
  3. on [dbo].[RunIDMapReportID]
  4. after insert
  5. as
  6. --delay for 1 second to ensure all ingredients finish inserting
  7. waitfor delay '0:0:1.000'
  8.  
  9. Declare @WO char(4), 
  10. @ComponentItemCode varchar(7), 
  11. @SubWeight decimal(15,5), 
  12. @Lot varchar(25), 
  13. @cimOut nvarchar(4000),
  14. @cnt int
  15.  
  16. --zero out counter
  17. Set @cnt = 0
  18.  
  19. --add beginning to work order issue cim file
  20. Set @cimOut = '@@batchload wowois.p'
  21.  
  22.  
  23. --Scroll through all the ingredients for the inserted ReportID and add them to the CIM output
  24. Declare cur_0 scroll cursor for
  25. Select C.RunID, B.ComponentItemCode, B.SubWeight, B.LotNumber
  26. From BatchReport A
  27. inner join BatchReportDetail B
  28. on A.ReportID = B.ReportID
  29. inner join RunIDMapReportID C
  30. on A.ReportID = C.ReportID
  31. Where A.BatchDone = 1 and B.ComponentItemCode like '2%' and B.Deduct = 1
  32. and B.ReportID = (Select ReportID From inserted)
  33. Order by B.ComponentItemCode
  34.  
  35. open cur_0
  36.  
  37. FETCH NEXT FROM cur_0 into @WO, @ComponentItemCode, @SubWeight, @Lot
  38.  
  39. While @@Fetch_Status = 0
  40. Begin
  41. Set @cnt = @cnt +1
  42.  
  43. --first time through add the work order
  44. If @cnt = 1
  45.     Begin
  46.     Set @cimOut = @cimOut + '
  47. ' + @WO
  48.     End
  49. --each time add the ingredient issue lines
  50. Set @cimOut = @cimOut + '
  51. ' + @ComponentItemCode + '
  52. ' + convert(varchar(25),@SubWeight) + ' - - - - ' + @Lot
  53.  
  54.  
  55. FETCH NEXT FROM cur_0 into @WO, @ComponentItemCode, @SubWeight, @Lot
  56. End
  57.  
  58. close cur_0
  59. deallocate cur_0
  60.  
  61. --Add end line to string
  62. Set @cimOut = @cimOut + '
  63. @@end'
  64.  
  65. --If we had any data to output, use OLE automation to put it into a timestamped file on the 
  66. --server and write out a history file with the WorkOrder, ReportID and the date it was processed
  67. If @cnt > 0
  68. Begin
  69.     --declare variables necessary for processing the CIM output into a file using OLE
  70.     DECLARE @FS int, @OLEResult int, @FileID int, @FileName varchar(255)
  71.  
  72.     --Generate a filename with a fixed length timestamp
  73.     Set @FileName = '\\servername\d$\CIM\wois_cim_' + convert(char(8),GETDATE(),112)+replace(convert(char(12),getdate(),114),':','') + '.cim'
  74.  
  75.     EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUT
  76.     IF @OLEResult <> 0 PRINT 'Scripting.FileSystemObject'
  77.  
  78.     --Open the file
  79.     execute @OLEResult = sp_OAMethod @FS, 'OpenTextFile', @FileID OUT, @FileName, 8, 1
  80.     IF @OLEResult <> 0 PRINT 'OpenTextFile'
  81.  
  82.     --Write the Text
  83.     execute @OLEResult = sp_OAMethod @FileID, 'WriteLine', Null, @cimOut
  84.     IF @OLEResult <> 0 PRINT 'WriteLine'
  85.  
  86.     --cleanup OLE objects
  87.     EXECUTE @OLEResult = sp_OADestroy @FileID
  88.     EXECUTE @OLEResult = sp_OADestroy @FS
  89.  
  90.     --write to history log
  91.     Insert tblCIMhist
  92.     Select RunID, ReportID, getdate()
  93.     From inserted
  94. End
  95.  
Nov 12 '07 #1
1 2540
iburyak
1,017 Expert 512MB
Not sure if it will help but you can try to catch an error:

Expand|Select|Wrap|Line Numbers
  1. EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUT
  2. If @@Error <> 0
  3.     PRINT ‘Error Scripting.FileSystemObject'
  4. ELSE
  5.     IF @OLEResult <> 0 PRINT 'Scripting.FileSystemObject'
Good Luck.
Nov 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Joseph Berkley | last post by:
Hi! Does anyone know of any database product or an API which supports the concept of an asynchronous commit status reporting? What I mean by this is an ability to easily figure out whether or...
3
by: DP | last post by:
Is there any command exists as "commit transaction" ? begin transaction < DML1 stmts > begin transaction < DML2 stmts > rollback transaction commit transaction which set of DML will be...
1
by: anders_tung | last post by:
Hi, I have a procedure which will call 3 functions. First function will update a record. Second function will delete a record. Third function will insert a record. Each function will return...
11
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to...
3
by: Mark | last post by:
If a java applicaiton using the type 4 driver calls a DB2 stored procedure, does the stored procedure need to do its own commit when updates are completed? If the stored procedure does a commit or...
6
by: JohnO | last post by:
Hi Folks, I have an update trigger that fails (it inserts an audit table record) in some circumstances. This is causing the triggering transaction to fail and roll back. Is there any way to...
1
by: Jason Huang | last post by:
Hi, I am just wondering how do we test the transaction's Commit and Rollback? How do we simulate a situation to let the transaction fail then Rollback? Thanks for help. Jason
1
by: Scott Cupstid | last post by:
We are working on a VB.NET application using SQLClient command objects to post data to the underlying SQL Server 2000 database. The application is deployed in a multi-user environment with no more...
9
by: eeriehunk | last post by:
Hi All, The important thing about triggers is that, you can't call Transaction Control Statements(commit/rollback) inside a trigger. But what if a trigger is fired and then it makes an entry into...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.