SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trigger not firirng in sql server


Trigger not firirng in sql server

Author
Message
selvakumar.sms
selvakumar.sms
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 457
hi all

I have ON INSERT and UPDATE trigger for my table,if i update any column or inserting any rows in SSMS, trigger is working well, but i read data from file and insert in the table means trigger is not working, here i my trigger coding please give your suggestion.

ALTER TRIGGER [dbo].[tr_sync_GH_Account_Master_Transfer_Staging]
ON [dbo].[GH_Account_Master_Transfer_Staging]
FOR INSERT, UPDATE
AS
BEGIN
declare @seqno as int

SELECT @seqno = S.ExtractSequenceNo
FROM EPOSSDataExtractSequence S INNER JOIN EPOSSyncOrderMaster M
ON S.MasterCategory = M.MasterCategory
WHERE M.SyncTableName = 'GH_Account_Master_Transfer_Staging'


UPDATE GH_Account_Master_Transfer_Staging
SET SeqNo = @seqno + 1
FROM INSERTED
WHERE
GH_Account_Master_Transfer_Staging.SourceDocNo = INSERTED.SourceDocNo
AND GH_Account_Master_Transfer_Staging.SourceFiscalYear = INSERTED.SourceFiscalYear
AND GH_Account_Master_Transfer_Staging.SourceLocationCode = INSERTED.SourceLocationCode
END

--------------------------------------------------------

I am Learner -- SQL
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86408 Visits: 45232
How are you inserting from a file?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


selvakumar.sms
selvakumar.sms
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 457
no bulk insert , reading line by line and inserting in to the table using .net scheduler.

--------------------------------------------------------

I am Learner -- SQL
DiverKas
DiverKas
Old Hand
Old Hand (317 reputation)Old Hand (317 reputation)Old Hand (317 reputation)Old Hand (317 reputation)Old Hand (317 reputation)Old Hand (317 reputation)Old Hand (317 reputation)Old Hand (317 reputation)

Group: General Forum Members
Points: 317 Visits: 460
selvakumar.sms (10/25/2012)
hi all

I have ON INSERT and UPDATE trigger for my table,if i update any column or inserting any rows in SSMS, trigger is working well, but i read data from file and insert in the table means trigger is not working, here i my trigger coding please give your suggestion.

ALTER TRIGGER [dbo].[tr_sync_GH_Account_Master_Transfer_Staging]
ON [dbo].[GH_Account_Master_Transfer_Staging]
FOR INSERT, UPDATE
AS
BEGIN
declare @seqno as int

SELECT @seqno = S.ExtractSequenceNo
FROM EPOSSDataExtractSequence S INNER JOIN EPOSSyncOrderMaster M
ON S.MasterCategory = M.MasterCategory
WHERE M.SyncTableName = 'GH_Account_Master_Transfer_Staging'


UPDATE GH_Account_Master_Transfer_Staging
SET SeqNo = @seqno + 1
FROM INSERTED
WHERE
GH_Account_Master_Transfer_Staging.SourceDocNo = INSERTED.SourceDocNo
AND GH_Account_Master_Transfer_Staging.SourceFiscalYear = INSERTED.SourceFiscalYear
AND GH_Account_Master_Transfer_Staging.SourceLocationCode = INSERTED.SourceLocationCode
END


You DO realize that this will call the trigger recursively, since you are updating the table the update trigger is on? You would need to add checks for what column is getting updated to avoid that and you probably want an AFTER INSERT trigger.
Lowell
Lowell
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27978 Visits: 39922
bulk insert does not automatically fire triggers unless you specifically set the flag (FIRE_TRIGGERS ).

I'm not sure your trigger is not designed to handle multiple rows correctly.
if i insert 100 rows for example, all 100 rows will have the same value in SeqNo . is that what you want, or did you want it to be some sort of incrementing value?

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

selvakumar.sms
selvakumar.sms
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 457
hi all

still i cant find the solution. please help me out.


this part get the latest seqno from table EPOSSDataExtractSequence, this seqno update every 40 min, once the scheduler run and generate file.

declare @seqno as int

SELECT @seqno = S.ExtractSequenceNo
FROM EPOSSDataExtractSequence S INNER JOIN EPOSSyncOrderMaster M
ON S.MasterCategory = M.MasterCategory
WHERE M.SyncTableName = 'GH_Account_Master_Transfer_Staging'
print @seqno


this part update the latest sequence no from the table EPOSSDataExtractSequence


UPDATE GH_Account_Master_Transfer_Staging
SET SeqNo = @seqno + 1
FROM INSERTED
WHERE
GH_Account_Master_Transfer_Staging.SourceDocNo = INSERTED.SourceDocNo
AND GH_Account_Master_Transfer_Staging.SourceFiscalYear = INSERTED.SourceFiscalYear
AND GH_Account_Master_Transfer_Staging.SourceLocationCode = INSERTED.SourceLocationCode
END


please suggest some idea.

--------------------------------------------------------

I am Learner -- SQL
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search