Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

trigger After

CREATE DATABASE productdb;
GO
--create a table called Products
USE productdb;
CREATE TABLE Products
(
    Id INT IDENTITY PRIMARY KEY,
    ProductName NVARCHAR(30) NOT NULL,
    Manufacturer NVARCHAR(20) NOT NULL,
    ProductCount INT DEFAULT 0,
    Price MONEY NOT NULL
);

--create a trigger called Products_INSERT_UPDATE
CREATE TRIGGER Products_INSERT_UPDATE
ON Products
AFTER INSERT, UPDATE
AS
UPDATE Products
SET Price = Price + Price * 0.38
WHERE Id = (SELECT Id FROM inserted)

--one example
ISERT INTO Products(ProductName,Manufacturer,ProductCount,Price)
VALUES ('IPhone X',2,'Apple',68000)
SELECT * FROM Products
--answer will be Price = 9384000

--Turn off trigger
DISABLE TRIGGER Products_INSERT_UPDATE ON Products

--Turn on trigger
ENABLE TRIGGER Products_INSERT_UPDATE ON Products

--Delete trigger
DROP TRIGGER Products_INSERT_UPDATE
 
PREVIOUS NEXT
Tagged: #trigger #After
ADD COMMENT
Topic
Name
9+9 =