| Author |
Topic |
|
Amjath
Yak Posting Veteran
66 Posts |
Posted - 2006-01-31 : 06:52:41
|
| hai,I have a insert trigger in table. by getting the inserted value i insert that value in another table. it works fine for manual entry, but if i insert 100 rows continuesly, in the best case it works fine, but a rare case it may insert duplicate records(insert two time the same record). how its possible.help me outWith Regards-Amjath |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-01-31 : 07:18:01
|
| Without a bit more infoprmation it's difficult to say.A bug in the trigger?Being called recursively?==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2006-01-31 : 07:27:57
|
| Amjath Welcome to SQLTeam,why not paste your trigger code here2. Put a unique key constraint in your other tableAfrika |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Amjath
Yak Posting Veteran
66 Posts |
Posted - 2006-02-01 : 01:49:24
|
| ALTER TRIGGER trgSR_CheckAndAddSampleRequest_Mill1ON NYS1ReHeat FOR UPDATE ASBEGIN DECLARE @nHeat AS INT, @nRollMillBarID AS INT, @sProduct AS VARCHAR(20), @nSRCount AS INT, @sLocation AS VARCHAR(20) SELECT @nHeat = Heat, @nRollMillBarID = LocSeq, @sProduct = FinalProd, @sLocation = Location FROM INSERTED IF (@sLocation = 'REHEAT') BEGIN IF NOT EXISTS (SELECT * FROM Physicals WHERE Heat = @nHeat AND SQLProduct = @sProduct) BEGIN SELECT @nSRCount = COUNT(*) FROM SampleRequest SR INNER JOIN NYS1Reheat Reheat ON SR.RollMillBarID = Reheat.LocSeq WHERE Reheat.Heat = @nHeat AND Reheat.FinalProd = @sProduct AND (SR.SampleRequest = 1 OR SR.SampleCut=1) IF (@nSRCount < 2) BEGIN INSERT INTO SampleRequest(RollMillBarID, SampleRequest, SampleCut, AutoRequest) VALUES(@nRollMillBarID, 1, 0, 1) END END ENDENDi Changed the insert trigger to update trigger, but the problem is, it will insert the row 2 times instead of 1 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-02-01 : 01:51:51
|
quote: SELECT @nHeat = Heat, @nRollMillBarID = LocSeq, @sProduct = FinalProd, @sLocation = Location FROM INSERTED
quote: Let me ask, are you treating the trigger as a set based operation?
as Brett's said. You have to treat inserted as a table that contains multiple records / rows.----------------------------------'KH' |
 |
|
|
Amjath
Yak Posting Veteran
66 Posts |
Posted - 2006-02-01 : 02:13:03
|
| hai i didnt get ui am new to triggercan u explain me little more better for my standard(very low)is this cause the problem of mulitple insert.with regards-Amjath |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-02-01 : 02:21:01
|
I have changed it to handle set operation. Compare this with your original code.ALTER TRIGGER trgSR_CheckAndAddSampleRequest_Mill1ON NYS1ReHeat FOR UPDATEASBEGIN DECLARE @nHeat AS INT, @nRollMillBarID AS INT, @sProduct AS VARCHAR(20), @nSRCount AS INT, @sLocation AS VARCHAR(20)-- SELECT @nHeat = Heat, @nRollMillBarID = LocSeq, @sProduct = FinalProd, @sLocation = Location FROM INSERTED-- REMOVED. This only handled one row not multiple row-- IF (@sLocation = 'REHEAT')-- BEGIN-- IF NOT EXISTS (SELECT * FROM Physicals WHERE Heat = @nHeat AND SQLProduct = @sProduct) IF NOT EXISTS (SELECT * FROM Physicals p inner join inserted i on p.Heat = i.Heat and p.SQLProduct = i.FinalProd) BEGIN-- SELECT @nSRCount = COUNT(*)-- FROM SampleRequest SR INNER JOIN NYS1Reheat Reheat -- ON SR.RollMillBarID = Reheat.LocSeq-- WHERE Reheat.Heat = @nHeat -- AND Reheat.FinalProd = @sProduct -- AND (SR.SampleRequest = 1 OR SR.SampleCut=1) SELECT @nSRCount = COUNT(*) FROM inserted i inner join NYS1Reheat Reheat on i.Heat = Reheat.Heat and i.FinalProd = Reheat.FinalProd inner join SampleRequest SR on SR.RollMillBarID = Reheat.LocSeq WHERE (SR.SampleRequest = 1 OR SR.SampleCut = 1) IF (@nSRCount < 2) BEGIN-- INSERT INTO SampleRequest(RollMillBarID, SampleRequest, SampleCut, AutoRequest) VALUES(@nRollMillBarID, 1, 0, 1) INSERT INTO SampleRequest(RollMillBarID, SampleRequest, SampleCut, AutoRequest) SELECT LocSeq, 1, 0, 1 FROM inserted END END-- ENDEND ----------------------------------'KH' |
 |
|
|
Amjath
Yak Posting Veteran
66 Posts |
Posted - 2006-02-01 : 02:39:13
|
| Thanx a loti went through ur codei got some idea, i will send u the result little bit lateronce again thank u for ur need full help |
 |
|
|
|