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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Multiple delete inside a trigger

Author  Topic 

jpatracar
Starting Member

11 Posts

Posted - 2009-01-27 : 19:24:42
Hello,
I've been trying to figure it out why a multiple delete is not being fired.
If i run this separatetly (not inside the trigger) works. I also tried running the multiple delete in a stored procedure but failed also.

Here's the code:

ALTER TRIGGER [tr_tblRecorrido_Puntos_DELETE]
ON tblRecorrido_Puntos
AFTER DELETE
AS
BEGIN
DECLARE @idRuta_Guid nvarchar(50)
DECLARE @idRamal_Guid nvarchar(50)
DECLARE @idRutaRamal int

--Variables
SET @idRutaRamal = (SELECT TOP 1 intId_Ruta_Ramal FROM deleted)
SELECT TOP 1 @idRuta_Guid = idRuta_Guid, @idRamal_Guid = idRamal_Guid
FROM tblRutaRamal_Generado WHERE intId_Ruta_Ramal = @idRutaRamal

--Made this in order to see that my variables weren't empty
INSERT INTO Prueba(str50_1, str50_2, int_1) VALUES(@idRuta_Guid, @idRamal_Guid, @idRutaRamal)

-- Borrar aquellos elementos que ya no pertencezcan a la colección.
DELETE tblItinerario FROM tblItinerario
WHERE idRuta_Guid = @idRuta_Guid AND idRamal_Guid = @idRamal_Guid AND
intPunto NOT IN (SELECT intPunto FROM tblRecorrido_Puntos WHERE intId_Ruta_Ramal = @idRutaRamal AND (intId_Tipo = 1 OR intId_Tipo = 2 OR intId_Tipo = 3))

END


Any ideas/comments are appreciated

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-27 : 19:58:33
Your trigger isn't coded properly to handle more than one row in a delete batch.

Check out my blog for why this is the case: http://weblogs.sqlteam.com/tarad/archive/2004/09/14/2077.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jpatracar
Starting Member

11 Posts

Posted - 2009-01-28 : 11:23:15
Thank you for your repply tkizer,
I checked your blog but still with the same issue: the trigger does not delete anything at all. It is like if the DELETE TRIGGER doesnt get fired after the n rows were deleted.

In my case the subquery checks for any elements that arent related after deletion..and those need to be deleted

Maybe in this particular case delete trigger is not an option?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-28 : 12:24:43
are you sure there are records in tblItinerario which satisfy the condition given?

i.e WHERE idRuta_Guid = @idRuta_Guid AND idRamal_Guid = @idRamal_Guid AND
intPunto NOT IN (SELECT intPunto FROM tblRecorrido_Puntos WHERE intId_Ruta_Ramal = @idRutaRamal AND (intId_Tipo = 1 OR intId_Tipo = 2 OR intId_Tipo = 3))
Go to Top of Page

jpatracar
Starting Member

11 Posts

Posted - 2009-01-28 : 13:10:30
quote:
Originally posted by visakh16

are you sure there are records in tblItinerario which satisfy the condition given?


Thank you for your repply visakh16,
Yes, there are records in such table that satisfy the condition given.
I made a stored procedure that i'm executing rightr after i delete/update table tblRecorrido_Puntos
Now it is working fine, but not as I wanted..

Maybe trigger for delete in this case doesn't apply such conditions?
Go to Top of Page
   

- Advertisement -