Skip to Content
Professional SQL Server™ 2005 Programming
book

Professional SQL Server™ 2005 Programming

by Robert Vieira
December 2006
Beginner
912 pages
24h 44m
English
Wrox
Content preview from Professional SQL Server™ 2005 Programming

13.5. INSTEAD OF Triggers

While it can work against tables, the primary purpose of an INSTEAD OF trigger is usually to allow updates to views in places where it was previously not possible.

Essentially, an INSTEAD OF trigger is a block of code we can use as something of an interceptor for anything that anyone tries to do to our table or view. We can either elect to go ahead and do whatever the user requests or, if we choose, we can go so far as doing something that is entirely different.

As with FOR/AFTER triggers, INSTEAD OF triggers come in three different flavors — INSERT, UPDATE, and DELETE. Unlike FOR/AFTER triggers, however, you can only have one trigger per table or view for each of the different flavors (one each for INSERT, UPDATE, DELETE).

If we're going to explore these, we need to get some appropriate sample tables out there. To that end, let's take the follow four tables (you can change the script to use an existing database if you wish).

CREATE TABLE dbo.Customers
    (
    CustomerID varchar(5) NOT NULL PRIMARY KEY ,
    Name varchar(40) NOT NULL
    )

CREATE TABLE dbo.Orders
     (
     OrderID int IDENTITY NOT NULL PRIMARY KEY,
     CustomerID varchar(5) NOT NULL
         REFERENCES Customers(CustomerID),
     OrderDate datetime NOT NULL
     )

CREATE TABLE dbo.Products
( ProductID int IDENTITY NOT NULL PRIMARY KEY, Name varchar(40) NOT NULL, UnitPrice money NOT NULL ) CREATE TABLE dbo.OrderItems ( OrderID int NOT NULL REFERENCES dbo.Orders(OrderID), ProductID int NOT NULL REFERENCES dbo.Products(ProductID), ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Professional Microsoft® SQL Server® 2008 Programming

Professional Microsoft® SQL Server® 2008 Programming

Rob Vieira, Mahsa Nakhjiri

Publisher Resources

ISBN: 9780764584343Purchase book