Trigger
Publicado por Carina (5 intervenciones) el 05/02/2007 17:08:55
Hola a todos:
Estoy tratando de generar una Tabla (Tabla B) con diferencias de valores de otra (Tabla A). No quiero usar vistas.
Entonces cree un Trigger en el que calculo la diferencia entre el ultimo valor y el nuevo (tabla virtual inserted). Pero no funciona ya que al querer saber el último valor encuentra el que estoy insertando (el Trigger es AFTER insert).
Entonces quiero usar un Trigger INSTEAD.
Dentro del Trigger, debo hacer la inserción de la Tabla A?, o igual me la hace el SQL.
Me da este error: ODBC: Msg 0, Level 19, State 1 SqlDumpExceptionHandler: Process 53 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process. Connection Broken
Les mando el trigger para que vean si tal vez estoy haciendo mal…
Ojalá puedan ayudarme, quiero hacerlo de la forma correcta (porque otra solución sería tomar el ultimo y calcular la diferencia con el penúltimo…)
ALTER TRIGGER [tg_datos] ON [Datos] FOR insert AS
declare
@id_estacion int,
@valor money,
@nueva_fecha datetime,
@nueva_hora datetime,
@nuevo_valor money
begin transaction
declare crs cursor for
select id_estacion, valor, fecha, hora, dato from inserted order by fecha desc, hora desc, valor desc
open crs
fetch next from crs into @id_estacion, nuevo_valor, @nueva_fecha, @nueva_hora, @dato
while @@fetch_status = 0
begin
select top 1 @valor=valor from datos where id_estacion=@id_estacion order by fecha desc, hora desc, valor desc
print '@id_estacion ( @id_estacion ) '+cast(@id_estacion as char)
print 'nuevo valor ( @nuevo_valor ) '+cast(@nuevo_valor as char)
print 'ultimo valor ( @valor ) '+cast(@valor as char)
set @nuevo_valor=@nuevo_valor-@valor
print 'resto ( @nuevo_valor ) '+cast(@nuevo_valor as char)
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Pluviometro]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[Pluviometro] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[ID_Estacion] [int] NULL,
[Fecha] [datetime] NULL ,
[Hora] [datetime] NULL ,
[diferencia] [float] NOT NULL
) ON [PRIMARY]
insert into Pluviometro (id_estacion, fecha, hora, diferencia) values (@id_estacion, @nueva_fecha, @nueva_hora, @nuevo_valor)
--si aqui inserto en datos da el error
print 'inserto: '+cast( @nuevo_valor as char )
fetch next from crs into @id_estacion, @nueva_fecha, @nueva_hora, @nuevo_valor
end
close crs
deallocate crs
commit transaction
Estoy tratando de generar una Tabla (Tabla B) con diferencias de valores de otra (Tabla A). No quiero usar vistas.
Entonces cree un Trigger en el que calculo la diferencia entre el ultimo valor y el nuevo (tabla virtual inserted). Pero no funciona ya que al querer saber el último valor encuentra el que estoy insertando (el Trigger es AFTER insert).
Entonces quiero usar un Trigger INSTEAD.
Dentro del Trigger, debo hacer la inserción de la Tabla A?, o igual me la hace el SQL.
Me da este error: ODBC: Msg 0, Level 19, State 1 SqlDumpExceptionHandler: Process 53 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process. Connection Broken
Les mando el trigger para que vean si tal vez estoy haciendo mal…
Ojalá puedan ayudarme, quiero hacerlo de la forma correcta (porque otra solución sería tomar el ultimo y calcular la diferencia con el penúltimo…)
ALTER TRIGGER [tg_datos] ON [Datos] FOR insert AS
declare
@id_estacion int,
@valor money,
@nueva_fecha datetime,
@nueva_hora datetime,
@nuevo_valor money
begin transaction
declare crs cursor for
select id_estacion, valor, fecha, hora, dato from inserted order by fecha desc, hora desc, valor desc
open crs
fetch next from crs into @id_estacion, nuevo_valor, @nueva_fecha, @nueva_hora, @dato
while @@fetch_status = 0
begin
select top 1 @valor=valor from datos where id_estacion=@id_estacion order by fecha desc, hora desc, valor desc
print '@id_estacion ( @id_estacion ) '+cast(@id_estacion as char)
print 'nuevo valor ( @nuevo_valor ) '+cast(@nuevo_valor as char)
print 'ultimo valor ( @valor ) '+cast(@valor as char)
set @nuevo_valor=@nuevo_valor-@valor
print 'resto ( @nuevo_valor ) '+cast(@nuevo_valor as char)
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Pluviometro]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[Pluviometro] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[ID_Estacion] [int] NULL,
[Fecha] [datetime] NULL ,
[Hora] [datetime] NULL ,
[diferencia] [float] NOT NULL
) ON [PRIMARY]
insert into Pluviometro (id_estacion, fecha, hora, diferencia) values (@id_estacion, @nueva_fecha, @nueva_hora, @nuevo_valor)
--si aqui inserto en datos da el error
print 'inserto: '+cast( @nuevo_valor as char )
fetch next from crs into @id_estacion, @nueva_fecha, @nueva_hora, @nuevo_valor
end
close crs
deallocate crs
commit transaction
Valora esta pregunta


0