实验报告5 数据库编程存储过程

  • 格式:docx
  • 大小:15.12 KB
  • 文档页数:2

实验5:数据库编程——存储过程
一.实验目的
通过本实验使学生掌握存储过程的基本概念和创建、执行、删除方法。

二.实验类型验证型
三.实验学时2学时
四.实验原理及知识点
1.Transact-SQL编程
2.存储过程的创建和执行
3.存储过程的修改和删除
五.实验环境
1.硬件设备要求:PC及其联网环境;
2.软件设备要求:Windows操作系统;MS SQL Server数据库管理系统。

六.实验内容及步骤
利用存储过程实现下面的应用:从账户1转指定数额的款项到账户2中。

假设账户关系表为Account(Accountnum,total)
存储过程
create procedure pro_transfer
(@inAccount int,@outAccount int,@amount float)
as
declare@totalDepositOut float,
@totalDepositIn float,
@inAccountnum int;
begin
select@totalDepositOut=(select total from Account where accountnum =@outAccount);
if (@totalDepositOut is null)
begin
rollback;
return;
end
if (@totalDepositOut<@amount)
begin
rollback;
return;
end
select@inAccount=(select accountnum from Account where accountnum =@inAccount)
if(@inAccountnum is null)
begin
rollback;
return;
end
update Account set total=toal-@amount
where accountnum=@outAccount;
update Account set total=total+@amount
where accountnum=@inAccount;
commit;
end
七.实验总结
通过这次实验学会写简单的存储过程,了解了在sql server 2008中的存储过程的基本语法,发现存储过程的语法和pascal有很大的相似之处,不过sql server 2008的语法和书上的语法有很大的不同,只能自己去网上找资料,不过这锻炼了我的自学能力,不过就写一个存储过程还是有很多不够,还是要勤加练习。