gpt4 book ai didi

SQL Server触发器和事务用法示例

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 32 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章SQL Server触发器和事务用法示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了SQL Server触发器和事务用法。分享给大家供大家参考,具体如下:

新增和删除触发器 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
alter trigger tri_TC on t_c
   for INSERT , delete
as
begin
   set XACT_ABORT ON
   declare @INSERTCOUNT int ;
   declare @DELETECOUNT int ;
   declare @UPDATECOUNT int ;
   set @INSERTCOUNT = ( select COUNT (*) from inserted);
   set @DELETECOUNT = ( select COUNT (*) from deleted);
   set @UPDATECOUNT = ()
   if(@INSERTCOUNT > 0)
   begin
    insert into t_c2 select * from inserted;
   end
   else if(@DELETECOUNT > 0)
   begin
    delete t_c2 where exists( select temp .cid from deleted temp where temp .cid=t_c2.cid);
   end
end

更新触发器和事务 。

事务主要用在数据的保护,在多表更新时,事务保存所有事务下的更新语句就不会提交,数据也就不能更新成功 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
alter trigger tri_TC_Update on t_c
   for update
as
begin
   declare @delcount int ;
   set @delcount = ( select count (*) from deleted);
   if(@delcount > 0)
   begin
    begin transaction triUpdate --定义事务
    declare @cname varchar (100);
    select @cname = cname from inserted; --保存更新后的内容
    update t_c2 set cname = @cname where cid = ( select cid from deleted); --更新
    if (@@error <> 0)
    begin
     rollback transaction triUpdate; --事务回滚
    end
    else
    begin
     commit transaction triUpdate;  --事务提交
    end
   end
end

存储过程 。

?
1
2
3
4
5
6
7
8
if(exists( select name from sysobjects s where s. name = 'pro_fun' and s.type= 'p' ))
   drop procedure pro_fun
go
   create procedure pro_fun
as
   select * from table
go
exec pro_fun

游标 。

?
1
2
3
4
5
6
7
8
9
10
11
12
declare @qybh varchar (10)
declare cur cursor for
   select distinct qybh from PJ_EnterpriseInput
open cur
fetch next from cur into @qybh
while @@fetch_status = 0
  begin
   print(@qybh)
   fetch next from cur into @qybh
  end
close cur
deallocate cur

视图 。

?
1
2
3
4
alter view CreateView
as
  select qybh from CreateView
go

定义方法 。

?
1
2
3
4
5
6
7
8
9
10
11
alter function funName(@str1 varchar (10),@str2 varchar (10))
returns varchar (10)
as
begin
   declare @returnStr varchar (10)
   set @returnStr = 'false'
   if(@str1 > @str2)
     set @returnStr = 'true'
   return @returnStr
end
select dbo.funName(... , ...)

定义表变量 。

?
1
2
3
4
declare @qybhTable table (id varchar (32),qybh varchar (30))
insert into @qybhTable
select id,qybh from PJ_EnterpriseInput
select * from @qybhTable

case when then 条件统计时的使用 。

?
1
2
3
4
5
6
select
sum ( case when z.watchName= '注册监理工程师' then 1 else 0 end ),
sum ( case when z.watchName= 'xinza' then 1 else 0 end ),
sum ( case when z.watchName= '监理员' then 1 else 0 end )
from zu_corjl z
right join zu_corjltemp t on t.corID=z.corID

希望本文所述对大家SQL Server数据库程序设计有所帮助.

最后此篇关于SQL Server触发器和事务用法示例的文章就讲到这里了,如果你想了解更多关于SQL Server触发器和事务用法示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com