You are on page 1of 3

--ejemplo 1 de tipo after porque espera que se realize una eliminacion para hace

r un insert
------------------------------------------------use libreria2
Create table Historial_eliminacion(
fecha date,
descripcion varchar(100),
usuario varchar(20),
)
go
Create Trigger ELIMINAR_libro
on libros
after delete
as
Insert into Historial_eliminacion(fecha, descripcion, usuario)
values
(getdate(), 'Eliminacion de libro', user)
drop trigger ELIMINAR_libro
Select * from Historial_eliminacion
delete from libros
where id_libro='1'
drop table Historial_eliminacion
---------ejemplo 2 insert into : product contiene las lista de productos y la ca
ntidad
de existencias orden details los pedidos ,
cada vez q se hace un pedido y se llene , revaje al inventario las unidades en
stock
-------------------------------------------------------select * from Products
select * from [Order Details]
insert into [Order Details] (OrderID, ProductID, UnitPrice, Quantity, Discount)
values (10248, 1, 2.60, 30, 0)
select UnitsInStock from Products
where ProductID=1
go
create trigger quitar_invetario
on [Order Details]
after insert
as
Update P set p.unitsinstock=p.unitsinstock - i.quantity
from inserted as i inner join products as P on
i.ProductID=p.ProductID

----Ejemplo 3--------------select * from Customers


select * from Suppliers

Go
ALTER view Directorio
as
select CompanyName, ContactName, ContactTitle, Country,
'Cliente' as Tipo
from Customers
union all
select companyname, ContactName, ContactTitle, Country,
'Proveedor' as Tipo
from Suppliers
Create view Directorio
as
select CompanyName, ContactName, ContactTitle, Country
from Customers
union all
select companyname, ContactName, ContactTitle, Country
from Suppliers
select * from Suppliers where CompanyName='Sistemas'
insert into Directorio(CompanyName, ContactName, ContactTitle, Country, Tipo)
values ('sistemas','juan perez','ingeniero','puno','Cliente')
Go
Create Trigger Insertar_directorio
on Directorio
instead of Insert
as
insert into Customers (CustomerID,CompanyName, ContactName, ContactTitle, Cou
ntry)
select substring(CompanyName,1,5),CompanyName, ContactName, ContactTitle, Cou
ntry
from inserted where Tipo ='Cliente'
insert into Suppliers(CompanyName, ContactName, ContactTitle, Country)
select CompanyName, ContactName, ContactTitle, Country
from inserted where Tipo ='Proveedor'

EJM 4
EJM 5

TRANSACCIONES
create database banco
create table Cliente(
cliente_id int identity primary key,
nombre varchar(50) not null,
dinero int not null,
)
insert into Cliente values('juan', 3);
insert into Cliente values('Rosa', 3);
insert into Cliente values('Marta', 3);

insert into Cliente values('Pedro', 3);


insert into Cliente values('Rocio', 3);
insert into Cliente values('ddddddd',56);
insert into Cliente values('ddddddddd', 34);
insert into Cliente values('df', 678);
insert into Cliente values('uy',343);
insert into Cliente values('ert', 435);
insert into Cliente values('ffffffffffffff',5453);
ejm1 rolback
declare @Error int
begin tran
Update Cliente set Cliente.dinero = 1
where cliente_id = 1
insert into Cliente values(2,'gabriel',500)
set @Error = @@Error
if(@Error <> 0)
Begin
ROLLBACK tran
print 'Error en la operacin'
end
select* from Cliente
ejm 2 commin
--ejemplo 2
begin tran
delete from Cliente
where cliente_id = 1
Rollback
COMMIT
begin tran
insert Cliente values (12,'Maria',345)
select * from Cliente
Rollback
Commit

You might also like