复制代码 代码如下:
--我们要建触发器的动态表
Create table Table_a
(
ID int identity(1,1),--自增ID
Content nvarchar(50),
UpdateIDForTrigger int
)
复制代码 代码如下:
Create TRIGGER [dbo].[Table_a_Ins]
ON [dbo].[Table_a]
AFTER INSERT
AS
BEGIN
declare @ID int
set @ID=(select ID from inserted)
--更新Table_a表中的UpdateIDForTrigger字段的值,为了能更明显的看出实时执行的效果
UPDATE Table_a
SET UpdateIDForTrigger = (@ID+10)--为了能看出不同,就直接将比ID大10的值作为变量赋值
WHERE ID = @ID;
END
复制代码 代码如下:
--给信息表添加数据
insert into Table_a(Content) values('信息一');
insert into Table_a(Content) values('信息二');
复制代码 代码如下:
select * from Table_a
复制代码 代码如下:
Alter TRIGGER [dbo].[Table_a_Ins]
ON [dbo].[Table_a]
AFTER INSERT
AS
BEGIN
select ID from inserted;
END
复制代码 代码如下:
Alter TRIGGER [dbo].[Table_a_Ins]
ON [dbo].[Table_a]
AFTER INSERT
AS
BEGIN
declare @ID int
declare cur_Insert cursor
for
select ID from inserted
open cur_Insert
fetch next from cur_Insert into @ID
while @@fetch_status=0
begin
UPDATE Table_a
SET UpdateIDForTrigger = (@ID+10)--为了能看出不同,就直接将比ID大10的值作为变量赋值
WHERE ID = @ID;
fetch next from cur_Insert into @ID
end
close cur_Insert
deallocate cur_Insert
END
复制代码 代码如下:
新闻热点
疑难解答