Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Triggers

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 out

With 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.
Go to Top of Page

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 here

2. Put a unique key constraint in your other table

Afrika
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-01-31 : 13:05:43
Please read the link in my sig and post what it asks for.

Let me ask, are you treating the trigger as a set based operation?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 01:49:24
ALTER TRIGGER trgSR_CheckAndAddSampleRequest_Mill1
ON NYS1ReHeat FOR UPDATE
AS
BEGIN
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
END
END


i Changed the insert trigger to update trigger, but the problem is, it will insert the row 2 times instead of 1
Go to Top of Page

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'


Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 02:13:03
hai i didnt get u
i am new to trigger

can u explain me little more better for my standard(very low)

is this cause the problem of mulitple insert.

with regards
-Amjath
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-01 : 02:16:19
You can refer to here
http://www.sqlteam.com/item.asp?ItemID=3850
http://www.sqlteam.com/item.asp?ItemID=6494

----------------------------------
'KH'


Go to Top of Page

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_Mill1
ON NYS1ReHeat FOR UPDATE
AS
BEGIN
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
-- END
END


----------------------------------
'KH'


Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 02:39:13
Thanx a lot

i went through ur code

i got some idea, i will send u the result little bit later
once again thank u for ur need full help

Go to Top of Page
   

- Advertisement -