Ver Mensaje Individual
  #5 (permalink)  
Antiguo 26/11/2010, 12:07
candrade
 
Fecha de Ingreso: julio-2010
Mensajes: 77
Antigüedad: 13 años, 9 meses
Puntos: 0
De acuerdo Respuesta: Trigger de multiples INSERTS

Listo problema solucionado!


Efectivamente iislas ... la tabla INSERTED puede guardar multiples registros.

Lo pense y me lo corroboraste con lo que me dijiste, por lo que desarre el siguiente TRIGGER:

USE [DBedms]
GO

ALTER TRIGGER Reporte2 ON Identificacion
FOR INSERT
AS
DECLARE @total INTEGER = (SELECT COUNT(*) FROM inserted)
DECLARE @c INTEGER = 0
BEGIN
SET NOCOUNT ON

WHILE @c <= @total
BEGIN
DECLARE @DefectId NVARCHAR(20) = (SELECT TOP 1 DefectId FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))
DECLARE @Serial NVARCHAR(20) = (SELECT TOP 1 Serial FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))
DECLARE @Date SMALLDATETIME = (SELECT TOP 1 IDate FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))
DECLARE @GlobalLevel NVARCHAR(100) = (SELECT TOP 1 IGlobalLevel FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))
DECLARE @ProductLevel NVARCHAR(100) = (SELECT TOP 1 IProductLevel FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))
DECLARE @ProductLevel2 NVARCHAR(100) = (SELECT TOP 1 IProductLevel2 FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))
DECLARE @LocalLevel NVARCHAR(100) = (SELECT TOP 1 ILocalLevel FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))
DECLARE @LocalLevel2 NVARCHAR(100) = (SELECT TOP 1 ILocalLevel2 FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))
DECLARE @LocalLevel3 NVARCHAR(100) = (SELECT TOP 1 ILocalLevel3 FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))
DECLARE @RMA BIT = (SELECT TOP 1 RMA FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))
DECLARE @Comments NVARCHAR(200) = (SELECT TOP 1 IComments FROM inserted WHERE DefectId IN (SELECT TOP (@c) DefectId FROM inserted ORDER BY DefectId ASC))

UPDATE Reporte
SET Serial = @Serial, IDate = @Date, IGlobalLevel = @GlobalLevel, IProductLevel = @ProductLevel,
IProductLevel2 = @ProductLevel2, ILocalLevel = @LocalLevel, ILocalLevel2 = @LocalLevel2,
ILocalLevel3 = @LocalLevel3, RMA = @RMA, IComments = @Comments
WHERE DefectId = @DefectId

SET @c = @c + 1
END

END
GO



Siempre habia odiado los "While" hasta hoy =D.