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: -
-
CREATE trigger [trg_CIM_WO_Issue]
-
on [dbo].[RunIDMapReportID]
-
after insert
-
as
-
--delay for 1 second to ensure all ingredients finish inserting
-
waitfor delay '0:0:1.000'
-
-
Declare @WO char(4),
-
@ComponentItemCode varchar(7),
-
@SubWeight decimal(15,5),
-
@Lot varchar(25),
-
@cimOut nvarchar(4000),
-
@cnt int
-
-
--zero out counter
-
Set @cnt = 0
-
-
--add beginning to work order issue cim file
-
Set @cimOut = '@@batchload wowois.p'
-
-
-
--Scroll through all the ingredients for the inserted ReportID and add them to the CIM output
-
Declare cur_0 scroll cursor for
-
Select C.RunID, B.ComponentItemCode, B.SubWeight, B.LotNumber
-
From BatchReport A
-
inner join BatchReportDetail B
-
on A.ReportID = B.ReportID
-
inner join RunIDMapReportID C
-
on A.ReportID = C.ReportID
-
Where A.BatchDone = 1 and B.ComponentItemCode like '2%' and B.Deduct = 1
-
and B.ReportID = (Select ReportID From inserted)
-
Order by B.ComponentItemCode
-
-
open cur_0
-
-
FETCH NEXT FROM cur_0 into @WO, @ComponentItemCode, @SubWeight, @Lot
-
-
While @@Fetch_Status = 0
-
Begin
-
Set @cnt = @cnt +1
-
-
--first time through add the work order
-
If @cnt = 1
-
Begin
-
Set @cimOut = @cimOut + '
-
' + @WO
-
End
-
--each time add the ingredient issue lines
-
Set @cimOut = @cimOut + '
-
' + @ComponentItemCode + '
-
' + convert(varchar(25),@SubWeight) + ' - - - - ' + @Lot
-
-
-
FETCH NEXT FROM cur_0 into @WO, @ComponentItemCode, @SubWeight, @Lot
-
End
-
-
close cur_0
-
deallocate cur_0
-
-
--Add end line to string
-
Set @cimOut = @cimOut + '
-
@@end'
-
-
--If we had any data to output, use OLE automation to put it into a timestamped file on the
-
--server and write out a history file with the WorkOrder, ReportID and the date it was processed
-
If @cnt > 0
-
Begin
-
--declare variables necessary for processing the CIM output into a file using OLE
-
DECLARE @FS int, @OLEResult int, @FileID int, @FileName varchar(255)
-
-
--Generate a filename with a fixed length timestamp
-
Set @FileName = '\\servername\d$\CIM\wois_cim_' + convert(char(8),GETDATE(),112)+replace(convert(char(12),getdate(),114),':','') + '.cim'
-
-
EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUT
-
IF @OLEResult <> 0 PRINT 'Scripting.FileSystemObject'
-
-
--Open the file
-
execute @OLEResult = sp_OAMethod @FS, 'OpenTextFile', @FileID OUT, @FileName, 8, 1
-
IF @OLEResult <> 0 PRINT 'OpenTextFile'
-
-
--Write the Text
-
execute @OLEResult = sp_OAMethod @FileID, 'WriteLine', Null, @cimOut
-
IF @OLEResult <> 0 PRINT 'WriteLine'
-
-
--cleanup OLE objects
-
EXECUTE @OLEResult = sp_OADestroy @FileID
-
EXECUTE @OLEResult = sp_OADestroy @FS
-
-
--write to history log
-
Insert tblCIMhist
-
Select RunID, ReportID, getdate()
-
From inserted
-
End
-
1 2540
Not sure if it will help but you can try to catch an error: - EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUT
-
If @@Error <> 0
-
PRINT ‘Error Scripting.FileSystemObject'
-
ELSE
-
IF @OLEResult <> 0 PRINT 'Scripting.FileSystemObject'
Good Luck.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
|
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...
| |