0% 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.

Uploaded by

FioreAM
Copyright
© © All Rights Reserved
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% 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.

Uploaded by

FioreAM
Copyright
© © All Rights Reserved
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

SELECT * FROM tbldetalle ORDER BY ORDERID DESC;

INSERT tbldetalle
(orderid,productid,unitprice,quantity,discount) VALUES(11200,8,5,4,0);

/*
OrderID ProductID UnitPrice Quantity Discount
----------- ----------- -------------- ----------- -----------------
11200 8 5.0000 4 0.0

(1 filas afectadas)

1 filas han sido aadidas /actualizadas
*/

SELECT unitsinstock FROM tblproductos WHERE productid=8;

/*
unitsinstock
------------
10
*/

CREATE TABLE bak_cliente(codigo int, nombre varchar(60))
GO

CREATE TRIGGER tr_del_producto
ON tblproductos FOR DELETE AS
INSERT INTO BAK_CLIENTE
SELECT productid,productname FROM deleted
GO

DELETE tblproductos WHERE productid=8 ;

/*
1 filas van a eliminarse
*/

SELECT * FROM bak_cliente ;

/*
codigo nombre
----------- -----------------------------------------------
8 Northwoods Cranberry Sauce
*/

DELETE tblproductos WHERE productname like 'c%' ;

/*
8 filas van a eliminarse
*/

SELECT * FROM bak_cliente ;
Adm. Base de Datos II
2
Manejo de Triggers en SQL Server 2000 Pag: 3 / 4

/*
codigo nombre
----------- ---------------------------------------
8 Northwoods Cranberry Sauce
1 Chai
2 Chang
5 Chef Anton's Gumbo Mix
18 Carnarvon Tigers
38 Cte de Blaye
39 Chartreuse verte
48 Chocolade
60 Camembert Pierrot
*/

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) ;

/*
productid productname unitprice
----------- -------------------------- ------------
3 Aniseed Syrup 20.0000
21 Sir Rodney's Scones 20.0000
*/




Adm. Base de Datos II
4

You might also like