博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Trigger
阅读量:5366 次
发布时间:2019-06-15

本文共 2435 字,大约阅读时间需要 8 分钟。

Reference: Administrator's Guide to SQL Server 2005

A trigger is another set of T-SQL code similar to a stored procedure, but rather than being called by the user, it executes on certain conditions. The two kinds of conditions are when users alter data, called Data Manipulation Language (DML) operations, and when the structure of an object changes, called Data Definition Language (DDL) operations. In other words, this is code that runs when the user issues a statement that updates, edits, or deletes data, or runs a statement that changes the structure of the database objects.

For DML, the trigger types are INSERT, UPDATE, or DELETE statements against a table or view, and for DDL statements you can "fire" triggers for CREATE, ALTER, DROP, GRANT, DENY, REVOKE, or UPDATE STATISTICS statements. Triggers normally fire directly on the statement's call, but you can specify that they fire after the code on the table or view completes.

Another type of trigger, called an INSTEAD OF TRigger, fires when the statement is run but does not run the statement. When the statement is called, the code in the trigger runs instead. For instance, if you make an INSTEAD OF trigger on a table for a DELETE operation, when the user tries to delete data from the table, the trigger could print text on the screen that says "Do not delete this record. Archive it instead."

Triggers can become highly complex, and a logic-flow diagram is essential to track them properly when they get a few levels deep or when they implement complex functions.

Developers often use triggers to enforce business logic. In SQL Server 2005, you can also use the Common Language Runtime (CLR) feature to have entire programs run as a trigger. Your developers might, for instance, have a C# program act as a trigger to gather metrics from a Web service when a new order is entered.

Special settings are available to your developers to prevent recursive triggers from firing. If the developers are not careful, an INSERT to table A might cause a DELETE in table B, which might have an DELETE TRigger to perform an INSERT to table A, which in turn causes another DELETE to table B. That is a recursive trigger, and you normally want to prevent that kind of situation.

In any case, you can only "nest" triggers 32 levels deep. Triggers are also part of the 2,147,483,647 objects per database limit.

转载于:https://www.cnblogs.com/sandy_liao/archive/2010/12/15/1906399.html

你可能感兴趣的文章
感谢Leslie Ma
查看>>
几种排序方法
查看>>
查看数据库各表的信息
查看>>
第一阶段测试题
查看>>
第二轮冲刺第五天
查看>>
图片压缩
查看>>
Hadoop-2.6.5安装
查看>>
ES6思维导图
查看>>
第四周作业
查看>>
20151121
查看>>
线段重叠 (思维好题)
查看>>
Codeforces Round #413 C. Fountains (线段树的创建、查询、更新)
查看>>
SBuild 0.1.5 发布,基于 Scala 的构建系统
查看>>
WordPress 3.5 RC3 发布
查看>>
DOM扩展札记
查看>>
primitive assembly
查看>>
根据经纬度查询位置百度api
查看>>
浅谈localStorage的用法
查看>>
Ad Exchange基本接口和功能
查看>>
Angular ui-router的常用配置参数详解
查看>>