Manejo de Triggers en SQL Server 2000 Pag:: Raiserror @@rowcount
This document discusses triggers in SQL Server 2000. It shows how to create triggers that fire on insert, update, and delete operations on tables to perform various actions like logging changes or updating other tables. Examples are provided of creating triggers to log deleted rows to another table, update a stock quantity when an order is inserted, and prevent price decreases on updates.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
31 views4 pages
Manejo de Triggers en SQL Server 2000 Pag:: Raiserror @@rowcount
This document discusses triggers in SQL Server 2000. It shows how to create triggers that fire on insert, update, and delete operations on tables to perform various actions like logging changes or updating other tables. Examples are provided of creating triggers to log deleted rows to another table, update a stock quantity when an order is inserted, and prevent price decreases on updates.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4
Manejo de Triggers en SQL Server 2000 Pag: 1 / 4
TRIGGERS EN SQL SERVER 2000
USE NORTHWIND GO
IF EXISTS(SELECT NAME FROM sysobjects WHERE NAME='tblproductos' AND TYPE='U') DROP TABLE tblproductos GO
SELECT * INTO tblproductos FROM products;
IF EXISTS(SELECT NAME FROM sysobjects WHERE NAME='tbldetalle' AND TYPE='U') DROP TABLE tbldetalle GO
SELECT * INTO tbldetalle FROM "order details";
CREATE TRIGGER tr_add_tblproductos ON tblproductos FOR INSERT,UPDATE AS RAISERROR('%d filas han sido aadidas /actualizadas',0,1,@@rowcount) GO
CREATE TRIGGER tr_del_tblproductos ON tblproductos FOR DELETE AS RAISERROR('%d filas van a eliminarse', 0,1,@@rowcount) GO
DELETE tblproductos WHERE productid IN (4,6);
/* 2 filas van a eliminarse
(2 filas afectadas) */
SELECT * FROM tblproductos WHERE productid IN (4,6);
CREATE TRIGGER upd_stk_art ON tbldetalle FOR INSERT AS SELECT * FROM inserted UPDATE tblproductos SET unitsinstock=unitsinstock+quantity FROM inserted WHERE tblproductos.productid=inserted.productid GO
SELECT unitsinstock FROM tblproductos WHERE productid=8 ; /* unitsinstock ------------ 6 */
Adm. Base de Datos II 1 Manejo de Triggers en SQL Server 2000 Pag: 2 / 4
IF EXISTS(SELECT NAME FROM sysobjects WHERE NAME='tr_upd_precio_prod' AND TYPE='tr') DROP TRIGGER tr_upd_precio_prod GO
CREATE TRIGGER tr_upd_precio_prod ON tblproductos FOR UPDATE AS IF UPDATE(unitprice) BEGIN DECLARE @precio_ant money,@precio_new money SELECT @precio_ant=unitprice FROM deleted PRINT 'precio antiguo:'+convert(varchar(6),@precio_ant)
SELECT @precio_new=unitprice FROM inserted PRINT 'precio nuevo:'+convert(varchar(6),@precio_new)
IF @precio_new < @precio_ant BEGIN PRINT 'Se deshace la transaccion, precio nuevo no puede ser menor' ROLLBACK TRAN END ELSE PRINT 'Actualizacion correcta' END GO
SELECT * FROM tblproductos ORDER BY unitprice;
UPDATE tblproductos SET unitprice=20 WHERE productid IN (3,21);
/* 2 filas han sido aadidas /actualizadas precio antiguo:10.00 precio nuevo:20.00 Actualizacion correcta */
UPDATE tblproductos SET unitprice=10 WHERE productid IN (3,21);
Adm. Base de Datos II 3 Manejo de Triggers en SQL Server 2000 Pag: 4 / 4
/* 2 filas han sido aadidas /actualizadas precio antiguo:20.00 precio nuevo:10.00 Se deshace la transaccion, precio nuevo no puede ser menor */
SELECT productid,productname,unitprice FROM tblproductos WHERE productid in (3,21) ;