Recent Articles

SQL Server 2000 Data Types
SQL Server requires that each variable and column in a table should be defined with respect to the type of data it will store.

Using DataTables More for Improving Performance
There are some features in the System.Data.DataTable class that a lot of developers don't utilize.

Open Source SQL Full Text Search Engine - Sphinx
I came across Sphinx via the MySQL Performance Blog (which has some good entries you might want to check out). It is an Open Source Full Text SQL Search Engine.

CLR vs T-SQL and Stored Procedures in SQL Server 2005
CLR or TSQL? That is the question.

MySQL Gets Telecom-Specific
Due to demand, MySQL AB announced the expansion of its telecom-specific services and consulting for MySQL software. Specifically, the company is promoting its MySQL Cluster, a high-end version of the standard database designed for fault-tolerant, mission-critical network and telecommunications applications at carrier-grade.


Low Rate eCommerce & Retail Plans
12.19.06


SQL Server Triggers

By Chris Kemp

Triggers are stored procedures which are fired when data is modified in an underlying table.

They can evaluate data being added to a table for validation purposes, or can make changes in that or other fields depending on the value of that data. You can use them even to execute a separate stored procedure, or to roll back a data modification or an entire transaction.

In earlier versions of SQL Server, triggers were used to maintain referential integrity. In current versions, constraints and foreign keys are used to accomplish much of those tasks, but triggers are still used to accomplish more complex tasks than that are available to the built in newer tools, such as complex column constraints, evaluation of tables in other databases, complicated defaults, or cascading routines involving multiple changes in multiple tables.

Triggers are created in the Enterprise Manager, or in the Query Analyzer through the object browser. There are also templates for triggers in the Query Analyzer (Edit|Insert Trigger). Triggers can be created with the following syntax:

Low Rate eCommerce & Retail Plans

CREATE TRIGGER trigger_name
ON { table | view }
[ WITH ENCRYPTION ]
{
      { { FOR | AFTER | INSTEAD OF } { [ INSERT ] [ , ] [ UPDATE ] }
            [ WITH APPEND ]
            [ NOT FOR REPLICATION ]
            AS
            [ { IF UPDATE ( column )
              [ { AND | OR } UPDATE ( column ) ]
                [ ... n ]
            | IF ( COLUMNS_UPDATED ( ) { bitwise_operator } updated_bitmask )
                { comparison_operator } column_bitmask [ ... n ]
            } ]
            sql_statement [ ... n ]
      }
}


There are two types of triggers: AFTER and INSTEAD OF. After triggers AFTER TRIGGERS fire after the data is changed, either by insert, delete, or update. If the data is inappropriate, as defined in the trigger, the modification can be rolled back to where it was before the data was modified. After triggers AFTER TRIGGERS cannot be placed on views, and cannot be used on more than one table. Also, the text, ntext, and image columns cannot be referenced in an after trigger. AFTER TRIGGERS.

After triggers AFTER TRIGGERS can be nested to 32 levels deep, and can be called recursively, again to 32 levels.

Instead of INSTEAD OF triggers make the validation before the modification. However, Instead of INSTEAD OF triggers CAN can be used on views. They do not allow recursion, and you can only have one Instead of INSTEAD OF trigger per table. And you cannot use an Instead of INSTEAD OF trigger with a cascade.

Resources:

- Information on Constraints and Triggers:
  This resource discusses about constraints and triggers
  in detail.

- Information: Create Trigger:
  This resource is useful in understanding the workings of
  triggers in detail.


About the Author:
Chris Kemp is a well known author in the field of Information Technology. His articles are very popular and well known in the various article banks across internet. His popular articles are about SQL Server, Database Design, IT Consulting and Software Development.

About SQLproNews
SQLproNews is a collection of up to date tutorials and insightful articles designed to help SQL users of any skill level implement successful SQL systems and practices. SQL Strategies and Tactics for Business

SQLproNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
SQLproNews.com WirelessProNews.com
CProgrammingTrends.com SysAdminNews.com




-- SQLProNews is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2006 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

advertising info | news headlines | free newsletters | comments/feedback | submit article


SQL Strategies and Tactics for Business SQLproNews News Archives About Us Feedback SQLproNews Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact