SQL 存储过程
SQL存储过程
建立
1 | create procedure lianxi66 |
引用
1 | declare @ok int, @ck float |
建立
1 | CREATE PROCEDURE lianxi3511 |
引用
1 | Declare @res int |
建立
1 | CREATE PROCEDURE lianxi2511 |
引用
1 | Declare @res int |
建立
1 | CREATE PROCEDURE lianxi1511 |
引用
1 | exec lianxi1511 '101101','101' |
习题
若输入的学号存在则删除
- 创建
1
2
3
4
5
6
7
8
9create procedure del_cno1
@cno char(20)
as
begin tran t1 --事件t1开始
delete from kcgx where 课程号[email protected]
if exists (select * from xkgx where 课程号[email protected])
rollback tran t1
else
commit tran t1 - 引用与验证
1
2
3exec del_cno1 '305'
select * from xkgx
select * from kcgx存储过程使用事务
实例1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Create Procedure MyProcedure
AS
Begin
Set NOCOUNT ON;
Set XACT_ABORT ON;
begin tran ok --开始一个事务OK
delete from rxqz where qz= 'rx015 ' --删除数据
save tran bcd --保存一个事务点命名为bcd
update sz set name='李丽s' where name= '李丽'--修改数据
if @@error<>0 --判断修改数据有没有出错
begin --如果出错
rollback tran bcd -- 回滚事务到BCD 的还原点
commit tran ok --提交事务
end
else --没有出错
commit tran ok --提交事务
End
1 | create proc 1 |