TRIGGER EN SQL SERVER 2014

use Negocios2015
go
/*creando disparadores(trigger)*/
--ejemplo1:
create trigger tr_eviarmensaje_clientes
on Ventas.clientes
for insert
as
print 'Se registro satisfactoriamente el cliente'
go
--probando trigger
insert into Ventas.clientes
values('ATJOS','Alca Torres Jose','Jr. Junin 123','001',
'999789458')
go
--ejemplo2:
create trigger tr_transacciones_paises
on Ventas.paises
for insert, update, delete
as
print 'Se ejecuto el trigger para la tabla pais'
go
--probando trigger
delete from Ventas.paises
where idpais='021'
go
insert into Ventas.paises values('011','Ecuador')
go
update Ventas.paises Set NombrePais='Bolivia'
where Idpais='011'
go
--ejemplo3:
alter trigger RRHH.tr_cargos
on RRHH.Cargos
for insert, update, delete
as
print 'Se ejecuto el trigger para la tabla Cargos'
go
--borrar trigger
drop trigger RRHH.tr_cargos
go
--ejemplo4: tabla auditoria
create table aud_clientes(
idaudcliente int identity(1,1)primary key,
codcliente varchar(5)not null,
nomcliente varchar(40)not null,
fecha_creacion date
)
go
create trigger Ventas.tr_aud_clientes
on Ventas.clientes
for insert
as
begin
insert into aud_clientes(codcliente,nomcliente,fecha_creacion)
select IdCliente,NomCliente,GETDATE() from inserted
end
--probando
insert into Ventas.clientes values('DPMAR','Duran Perez Maria',
'Jr. Azangaro #454','001','1234568')
go
select * from aud_clientes
go
/*tabla auditoria completa*/
create table aud_tbcategoria(
idcategoria int identity(1,1)primary key,
codcate int not null,
nombre varchar(15)not null,
servidor varchar(25)not null,
usuario varchar(25)not null,
fecha_creacion date,
hora_creacion time)
go
--creando trigger
alter trigger Compras.tr_delete_categorias
on Compras.categorias
for delete
as
begin
insert into aud_tbcategoria(codcate,nombre,servidor,usuario,fecha_creacion,
hora_creacion)
select IdCategoria, NombreCategoria, @@SERVERNAME, CURRENT_USER, GETDATE(),
GETDATE() from deleted
end
--PROBANDO
delete from Compras.categorias where IdCategoria=13
go
select * from aud_tbcategoria
select * from Compras.categorias

Comentarios

Entradas populares

Ejercicios Consola Visual Basic.NET

Numero Capicua Visual Basic

Procedimientos Almacenados ORACLE