实验题目 | 实验四 SJK存储过程实验 | ||||
实验时间 | 2023.4.5 | 实验地点 | 软件工程基础实验室 | 实验课时 | 2 |
实验目的 | 掌握存储过程的编写与调用 | ||||
实验要求 | 掌握存储过程的编写;掌握存储过程的调用 | ||||
实验步骤 及内 | 1.使用事务方式往院系表(t_department)中添加院系信息。信息分别为:(1)04,电器工程系,伏特;(2)04,机械工程系,墨子。要求两条记录同时添加,若错误则回滚,最后查询当前院系表中的数据信息。注意要查看输出的结果信息和消息信息。 参考代码: use EMISE go begin tran mytran insert into t_department values('04','电器工程系','欧姆') insert into t_department values('04','机械工程系','墨子') if @@ERROR>0 rollback tran mytran else commit tran mytran select *from t_department
3.使用存储过程中定义事务实现银行转账业务处理。 use master go if DB_ID('bankdb') is not null drop database bankdb; go create database bankdb go use bankdb go create table account( id int identity(1,1) primary key, cardno char(20) unique not null, balance numeric(18,2) ) go insert into account values('01',100.0) insert into account values('02',200.0) insert into account values('03',300.0) go --创建存储过程 create procedure Sp_transfer_money @out_cardno char(20), @in_cardno char(20), @money numeric(18,2) as begin declare @remain numeric(18,2) select @remain=balance from account where cardno=@out_cardno if @money>0 if @remain>=@money --余额足够 begin begin tran t1 update account set balance=balance-@money where cardno=@out_cardno update account set balance=balance+@money where cardno=@in_cardno if @@error>0 begin ---wrong rollback tran t1 return 0 end else begin ---right commit tran t1 print '转账成功!' end end else ---余额少 begin print '余额不足' end else --- 转账金额小于0 print '转账金额应大于0!' end go exec Sp_transfer_money '01','02',50 4.在SQL Server中查看数据库中的锁。 (1)使用快捷键“Ctrl+2”查看锁的信息。 打开SQL Server2014的SSMS,在查询分析器中使用快捷键“Ctrl+2”,即可看到进程、锁已经对象等信息。 2)使用系统存储过程Sp_lock来查看数据库中的锁 在新建查询的编辑区内,键入“EXEC Sp_lock”,执行这个命令也可以查看当前数据库中的锁。 EXEC Sp_lock 5.使用表级锁。在 SQL Server 中模拟两个用户对表进行操作,其中一个用户查看学生表信息,并锁定 20ms;另一个用户也查看学生表,并要更新其中的数据。 (1)创建一个新的查询窗口,在编辑区窗口内键入如下代码,并保存为user1_lock.SQL。 use EMISE go begin tran t1 declare @SD_time varchar(8) select *from t_student with(holdlock) select @SD_time=convert(varchar,getdate(),8) print '用户1锁定时间为:'+@SD_time waitfor delay '00:00:20' select @SD_time=convert(varchar,getdate(),8) print '用户1锁定时间为:'+@SD_time commit tran t1 (2)创建一个新的查询窗口,在编辑区窗口键入如下代码,并保存为user2_update.SQL。 use EMISE go begin tran t2 declare @sd_time varchar(8) --select *from t_student with(holdlock) select @sd_time=convert(varchar,getdate(),8) print '用户2开始时间为:'+@sd_time select *from t_student select @sd_time=convert(varchar,getdate(),8) print '用户2执行查询的时间为:'+@sd_time update t_student set name='wang' where name='王庆子' select @sd_time=convert(varchar,getdate(),8) print '用户2更新数据的时间为:'+@sd_time commit tran t2 | ||||
实验结果及分析 | 经检验,实验基本达到预期效果。通过此次上机实验,使我更加熟悉了对SQL语句及其应用,第四次上机调试已经不感到陌生。在SQL语句调试的过程中虽错误总是千奇百怪,但根据老师教授的知识很快的得以纠正,完成了本次实验,通过本次实验,深刻的意识到自身的不足,也明白了理论与实践的差距,同时自己学过的知识也得以加深理解与巩固,感谢老师给予我们机会,使我们的知识更加牢固! |
03-10
05-03
2066
05-01
01-30
3948
12-07
3006
12-13
4431
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交