Creare e ottimizzare i trigger in SQL Server
Florin Angelescu
Instructor
-- Attiverà un trigger di INSERT
INSERT INTO Orders [...];
-- Esegue la stored procedure
EXECUTE sp_DailyMaintenance;
Usati per:
Usate per:
INSERT o UPDATE usati per calcolare-- Usato nel corpo del trigger
[...]
UPDATE
SET TotalAmount = Price * Quantity
[...]
-- Definizione della colonna
[...]
TotalAmount AS Price * Quantity
[...]
CREATE TABLE [SalesWithPrice]
(
[OrderID] INT IDENTITY(1,1),
[Customer] NVARCHAR(50),
[Product] NVARCHAR(50),
[Price] DECIMAL(10,2),
[Currency] NVARCHAR(3),
[Quantity] INT,
[OrderDate] DATE DEFAULT (GETDATE()),
[TotalAmount] AS [Quantity] * [Price]
);
CREATE TRIGGER [SalesCalculateTotalAmount]
ON [SalesWithoutPrice]
AFTER INSERT
AS
UPDATE [sp]
SET [sp].[TotalAmount] = [sp].[Quantity] * [p].[Price]
FROM [SalesWithoutPrice] AS [sp]
INNER JOIN [Products] AS [p] ON [sp].Product = [p].[Product]
WHERE [sp].[TotalAmount] IS NULL;
Creare e ottimizzare i trigger in SQL Server