Home > Backend Development > PHP Tutorial > How to use SQL Server 2005 INSTEAD-OF trigger_PHP tutorial

How to use SQL Server 2005 INSTEAD-OF trigger_PHP tutorial

WBOY
Release: 2016-07-13 17:03:12
Original
909 people have browsed it

A trigger is a databaseobject similar to a stored procedure, which responds to a request in a database environment. SQL Sever 2005 contains 3 trigger objects: AFTER, data definition language (DDL) and INSTEAD-OF.
AFTER trigger is a stored procedure, which occurs after the data operation statement, such as delete statement, etc. DDL is a new trigger in SQL Server 2005 that allows responding to object definition level events in the database engine (for example: DROP TABLE statement). INSTEAD-OF triggers are objects that can be executed in place of data manipulation statements in the database engine. For example: Attach an INSTEAD-OF INSERT trigger to the table to tell the database to execute this trigger.
 Reasons for using INSTEAD-OF triggers
INSTEAD-OF triggers are powerful objects in SQL Sever that allow developers to transfer the database engine to complete different work to meet development requirements. One example is to add an INSTEAD-OF trigger to a database table. When the table does not need to be modified, the contents of the table can be rolled back. Extra care must be taken when using this method because the INSTEAD-OF trigger must be active before any specified table modifications.
One of the more good reasons to use INSTEAD-OF triggers is view processing. After adding an INSTEAD-OF trigger to a view, you can create an updated view. Updatable views allow complete extraction of the database schema, so systems can be designed in this way without having to worry about OLTP database schema issues and replacing the data with a set of standard views.
 Example
To better illustrate the concept of updateable views, we provide an example. In this example, we design a product table (recording products) and a purchase table (recording purchases). Listing A contains a script to create a table. After running this script, you will get the table used in the example. Run the Listing B script to add data to the table.
Now that I have data in the tables, I can create some meaningful views of these tables. Please see Listing C.
This is a typical product level view. It joins two tables in the database to simplify the data. However, for data extraction, there are no advantages to using views. After attaching the INSTEAD-OF trigger to the view, the table is allowed to be modified, but I do not need to directly modify the data in the table. I use the code in Listing D to create an INSTEAD-OF trigger on the vw_ProductPurchases view.
Please note the declaration of this INSTEAD OF trigger. The default trigger created by SQL Server is an AFTER trigger, therefore, the INSTEAD OF clause must be specified in the trigger definition.
The first statement of the trigger is the "check" statement. In this example I use this statement to check the INSERTED table to ensure that the ProductID field is displayed, and to ensure that the other PurchasePrice or ProductPrice fields are displayed.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631002.htmlTechArticleA trigger is a database object similar to a stored procedure, which responds to a request in a database environment. SQL Sever 2005 contains 3 trigger objects: AFTER, Data Definition Language (DDL) and I...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template