Triggers

The code for creating the dbo.SimpleOrders table doesn't check the order date value when inserting or updating the data. The following INSERT statement, for example, inserts an order with a pretty old and probably wrong date:

INSERT INTO dbo.SimpleOrders 
 (OrderId, OrderDate, Customer) 
VALUES 
 (3, '20100701', N'CustC'); 

You can check that the incorrect date is in the table with the following query:

SELECT o.OrderId, o.OrderDate, o.Customer 
FROM dbo.SimpleOrders AS o 
ORDER BY o.OrderId; 

Of course, it is possible to prevent inserting an order date too far in the past, or updating it to a too old value, with a check constraint. However, imagine that you don't want to just reject inserts and updates with the order date value in the past; ...

Get SQL Server 2017 Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.