Sql server database trigger operating summary 01
2008-05-09 14:53
344 查看
1.Creating a trigger:
Syntax:
CREATE TRIGGER trigger_name ON {table_name|view_name}
[WITH ENCRYPTION]
{ { {FOR|AFTER|INSTEAD OF} { [DELETE][,][INSERT][,][UPDATE] } [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]
}
}
CREATE TRIGGER C_insert ON hp_Camera FOR INSERT
AS
DECLARE @area_val NVARCHAR(50),@latitude_val FLOAT
SELECT @area_val=Area,@latitude_val=Latitude FROM INSERTED
IF(LEFT(@area_val,6)!=N'黄埔区')
BEGIN
ROLLBACK TRANSACTION
RAISERROR('输入的区信息不是黄埔区',16,1,@area_val)
END
IF(@latitude_val=0.0)
BEGIN
ROLLBACK TRANSACTION
RAISERROR('纬度值不能为零',16,1,30)
END
2.When you inserted different data to hp_camera table,the above C_insert trigger will be triggered:
a.With the '黄埔区' invalid data,got the following figure:
b.With the 0 invalid data,got the following figure:
c.With the correct data,got the following figure:
Syntax:
CREATE TRIGGER trigger_name ON {table_name|view_name}
[WITH ENCRYPTION]
{ { {FOR|AFTER|INSTEAD OF} { [DELETE][,][INSERT][,][UPDATE] } [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]
}
}
CREATE TRIGGER C_insert ON hp_Camera FOR INSERT
AS
DECLARE @area_val NVARCHAR(50),@latitude_val FLOAT
SELECT @area_val=Area,@latitude_val=Latitude FROM INSERTED
IF(LEFT(@area_val,6)!=N'黄埔区')
BEGIN
ROLLBACK TRANSACTION
RAISERROR('输入的区信息不是黄埔区',16,1,@area_val)
END
IF(@latitude_val=0.0)
BEGIN
ROLLBACK TRANSACTION
RAISERROR('纬度值不能为零',16,1,30)
END
2.When you inserted different data to hp_camera table,the above C_insert trigger will be triggered:
a.With the '黄埔区' invalid data,got the following figure:
b.With the 0 invalid data,got the following figure:
c.With the correct data,got the following figure:
相关文章推荐
- Sql server database operating summary 01
- Sql server database cursor operating summary 01
- Sql server stored procedure operating summary 01
- Sql server database cursor operating summary 02
- Sql server database cursor operating summary 03
- sqljdbc4和sqljdbc.jar或No suitable driver found for jdbc:sqlserver......DatabaseName=sql_database01
- Sql server database stored procedure reference 01
- Sql server database syntax reference 01
- [bbk4979]第06集 - Chapter 04- Interacting with Oracle Database Server:SQL Statements in PL/SQL Programs(01)
- Sample script that deletes a SQL Server database
- Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database
- Sql server database:create scalar-valued function
- How to select the data type in SQLserver database such as varchar, nvarchar
- Sql server database:create table-valued function 02
- Sql server database syntax reference 02
- How to move the databases that are used by SharePoint Portal Server 2003 to a computer that is running SQL Server
- Paging Records Using SQL Server 2005 Database - ROW_NUMBER Function
- DB太大?一键帮你收缩所有DB文件大小(Shrink Files for All Databases in SQL Server)
- SQL Server Database Snapshot (转自论坛)