June 25, 2011 at 12:56 pm
I have tried hard to work with SqlBulkCopy & Insert Trigger but -- the insert trigger on the table that SqlBulkCopy is sending data to is not running. If I insert with a regular INSERT query the trigger fires...but if I do the insert with SqlBulkCopy it does not.
I already tried and set
Option 1) SqlBulkCopyOptions.FireTriggers
Option 2) SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.CheckConstraints
using (SqlBulkCopy bcp = new SqlBulkCopy("ConnectionString", SqlBulkCopyOptions.FireTriggers))
{
bcp.DestinationTableName = "MediaDetails_Dump"; //Table Name
bcp.ColumnMappings.Add(0, 0);
bcp.ColumnMappings.Add(1, 1);
bcp.WriteToServer(DataTable);
//bcp.Close();
}
CREATE TRIGGER trgAfterInsert ON [MediaDetails_Dump]
FOR INSERT
AS
insert into MediaDetails
(MediaId, LanguageId
)
select MM.MediaId, 1
from inserted I
inner join MediaMst MM
on I.MediaTitle = MM.MediaTitle
PRINT 'AFTER INSERT trigger fired.'
GO
What is the wrong with my code. I unable to understand. Please help me
June 25, 2011 at 5:16 pm
I would not use a trigger in this way. Avoid triggers where possible. Is it necessary here? Why not allow the bulk copy to complete and then call a stored procedure to load the new data into MediaDetails?
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
June 25, 2011 at 11:54 pm
Your options is welcome. I did the job that way. But I just want to know why it is not working?
June 26, 2011 at 12:12 pm
Shubhajyoti Ghosh (6/25/2011)
But I just want to know why it is not working?
Try setting the SqlBulkCopyOptions to FireTriggers in the SqlBulkCopy
constructor.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
June 26, 2011 at 12:22 pm
@Welsh Corgi
had u check my posted code. I already used SqlBulkCopyOptions.FireTriggers
Via Sql Profiler now I able to see Trigger is fired. But data is not inserted into destination table after bulk copy operation.
June 26, 2011 at 12:32 pm
I made a blunder. Problem with Test data. Now every thing is solved. 🙂
June 26, 2011 at 12:34 pm
Are you joining on the correct table in your trigger?
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
June 26, 2011 at 12:57 pm
Ya I join corrected table. but test with different data which is not available in Master Table . That is why join condition not satisfy. So Trigger fired but not insert data into table. 😉
Sorry my mistake. from yesterday onward I am just wasting my time for this blunder. What I say !!! 😛
June 26, 2011 at 1:05 pm
Now that you have determined the source of the problem, you should seriously consider avoiding a trigger unless absolutely necessary as was suggested.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply