sqlserver触发器

  • 格式:doc
  • 大小:34.00 KB
  • 文档页数:2

CREATE TRIGGER t_dalii ON [dbo].[员工明细表]
FOR INSERT
AS
declare @v_char1 varchar(20),@v_char2 varchar(20),@v_char3 int,@v_char4 varchar(20),@v_char5 datetime
begin
set @v_char1=(select 所属部门 from inserted)
if @v_char1<>null
begin
if exists(select 部门号 from 部门状态表 where 部门=@v_char1)
set @v_char2=(select 部门号 from 部门状态表 where 部门=@v_char1)
else
set @v_char2=((select max(部门号) from 部门状态表 )+1)
end
set @v_char3=(select 编号 from inserted)
set @v_char4=(select 姓名 from inserted)
set @v_char5=(select 入厂时间 from inserted)
insert into 部门人员表 (部门号,部门,编号,姓名,员工属性,进部门时间,是否在职,是否下岗 )
values(@v_char2,@v_char1,@v_char3,@v_char4,''职员'',@v_char5,''是'',''否'')
end
---------------------------------------------------------------
:create trigger tr_name
on table/view
{for ¦ after ¦ instead of } [update][,][insert][,][delete]
[with encryption]
as {batch ¦ if update (col_name) [{and ¦or} update (col_name)] }
说明:1 tr_name :触发器名称
2 on table/view :触发器所作用的表。

一个触发器只能作用于一个表
3 for 和after :同义
4 after 与instead of :sql 2000新增项目
afrer 与 instead of 的区别
After Instead of
在触发事件发生以后才被激活代替了相应的触发事件而被执行
只可以建立在表上既可以建立在表上也可以建立在视图上
5 insert、update、delete:激活触发器的三种操作,可以同时执行,也可选其一
6 if update (col_name):表明所作的操作对指定列是否有影响,有影响,则激活触发器。

此外,因为delete 操作只对行有影响,所以如果使用delete
操作就不能用这条语句了(虽然使用也不出错,但是不能激活触发器,没意义)。

7 触发器执行时用到的两个特殊表:deleted ,inserted
deleted 和inserted 可以说是一种特殊的临时表,是在进行激活触发器时由系统自动生成的,其结构与触发器作用的表结构是一样的,只是存放的数据有差异。

for update 修改时激发
for insert 插入数据时激发
for delete 删除数据时激发
deleted 有变动的数据的修改前数据集
inserted有变动的数据的修改后数据集
IF UPDATE (column) 和 IF (COLUMNS_UPDATED()) 的分别?
楼主moqijun(百分比) 2003-05-08 09:38:29 在 MS-SQL Server / 疑难问题提问
在触发器中,IF UPDATE (column) 和 IF (COLUMNS_UPDATED()) 有什么分别? 问题点数:20、1 楼
IF UPDATE (column)
Tests for an INSERT or UPDATE action to a specified column and is not used with DELETE operations. More than one column can be specified. Because the table name is specified in the ON clause, do not include the table name before the column name in an IF UPDATE clause. To test for an INSERT or UPDATE action for more than one column, specify a separate UPDATE(column) clause following the first one.
UPDATE(column) can be used anywhere inside the body of the trigger.
column
Is the name of the column to test for either an INSERT or UPDATE action. This column can be of any data type supported by SQL Server. For more information,
IF (COLUMNS_UPDATED())
Tests, in an INSERT or UPDATE trigger only, whether the mentioned column or columns were inserted or updated. COLUMNS_UPDATED returns a
varbinary bit pattern that indicates which columns in the table were inserted or updated. COLUMNS_UPDATED can be used anywhere inside the body of the trigger.
COLUMNS_UPDATED() 返回一个int值,相应位是1表示该列UPDATED。

我觉得比较难用,每次写trigger都要试,不如用IF UPDATE (column)。

下例在表 my_table 中创建名为 my_trig 的 INSERT 触发器,并测试列 b 是否受到任何INSERT 语句的影响。

CREATE TABLE my_table*
(a int NULL, b int NULL)
GO
CREATE TRIGGER my_trig
ON my_table
FOR INSERT
AS
IF UPDATE(b)
PRINT 'Column b Modified'
GO
下例使用 COLUMNS_UPDATED() 。

CREATE TRIGGER my_trig2
ON my_table
FOR INSERT
AS
IF ( COLUMNS_UPDATED() & 2 = 2 )
PRINT 'Column b Modified'
GO。