《SQL Server数据库实用案例教程》 实验10-存储过程练习 参考代码-不要先发学生.docx
提示:不要把答案先发给学生,实验结束后再公布答案。要让学生先自己思考做题,老师对学生不会的题目适当提示,一题题解决困惑。实验10存储过程练习参考代码/*继续使用实验创建的银行储蓄数据库,客户信息(customerinfo)表、账户信息(accountinfo)表、交易信息(transInfo)表三个表的表结构见表易信表-20。*/(1)题 存款存储过程-创建存储过程的语句create proc p_ck-定义存储过程zh char(19),je decimal(202),-两个输入参数mess varchar(50) outputas*不蛤U-1余柄-存储过套语法中必须有的关键字beginset nocount onifje> 1-判断存款大于元beginif exists(select accountID from accountinfowhere accountID = zh and accountstate ='正常')beginINSERT INTO transInfo(accountID, transType, transMoney)VALUES(zh;存入;je)-交易信息表transInfo插入存款记录update accountinfo set accountMoney = accountMoney + jeWHERE accountID = zh-更新卡余额select mess 廿存款成功!账号:'+zh+'存入:"ltrim(str(je)+元'endelseselect mess ='账号不存在,或已经挂失! endelseselect mess='存款金额不可以小于元,End龙象资源告理器0 0 LAPTOP-NMGPMIMMsql2008 (SQL S田一I系统数据库+ 数是座二花 日 U bank®数据库关系图囹口表因 视图田口同义词0 一1可编程性创建成功后在对象资源管理器中查看。星db°p_ck以下为执行存储过程的语句,不要与创建存储过程的语句一起执行-给账号1005存款50元declare ms varchar( 100exec p_ck U005 50 , ms output print msJ消息执行结果:存款成功!账号:1005存入:50元-执行存储过程前后分别查看两个表中数据select * from accountinfo where accountID = *1005*select * from transInfo where accountID = *1005*-执行存储过程之前两个表中数据口结果消息accountID savingType moneyType openDateopenMoney accountMoney password accountstate cus11005 -活期 人民币 2023-06-1510:45:25.273 40.0040 00666666 正常5transID transDateaccountID transType transMoney bz1transIDtransDate2023-05-06 00:00:00 000accountID 1005transType存入transMoney 40 00bzNULLI>Illi| 5132023-06-1511:27:25.0331005存入50 00null口结果3消息1| 5 I 2023-05-06 00:00:00 000 1005 存入 40.00 NULL1accountID1005savingType moneyType openDateopenMoney活期人民币2023-06-1510:45:25.273 40.00accountMoney 90 00Jpassword accountstate cus666666 正常5< >-执行存储过程之后两个表中数据(2)题生成账号存储过程/*随机产生十九位账号*/*第一种方式创建存储过程不定义变量*/create proc p_zh 1randZH chart 19 output asselectrandZH=,60138261050r+right('0000000'+LTRIM(str(ceiling(Rand()* 10000000)5)/*第二种方式创建存储过程定义变量*/create proc p_zh2randZH char( 19)output begindeclare ii intdeclare cc char(7)-生成随机数、取整-转换成字符串、去左空格-不足位左侧补、取右面位select ii=ceiling(RAND()* 10000000)set cc=LTRIM(str(ii)set cc=right(,0000000,+cc,7)SET randZH='60138261050r+ccEnd在对象资源管理器中“存储过程”上右击刷新对象资源言理器 蹒(。卜哥雪4X回品0 0 LAPTOP-NMGPMIMMsql2008 (SQL S aS L3数据库a二i系统数据库(±)一数据库快照0 IJ bank® 数据库关系图田 > 表囹口视图国LJ同义词日口可编程性存储过程®二系统存储过程国 M dbo.p ck® 目 dbo.p zhl坦用 dbo.p_zh2/*以下为测试生成账号存储过程语句,不要与创建存储过程的语句一起执行*/declare cc char( 19)exec p_zh 1 cc output select ccexec p_zh2 cc output select cc里结果金消息(无列名)(无列名)这是测试的一组结果,随机生成,每次不同(3)题开户存储过程- 查看三张表列名和表中数据select * from customerinfoselect * from accountinfoselect * from transInfo- 创建存储过程的语句create procedure p_khname varchar(8),cID varchar( 18),pho varcharf 11),add varchar( 100)=n,je decimal 20,2),type varchar(8),mess varchar( 100) outputASbeginDECLARE zh varchar(19),khbh int -定义变量存账号和客户编号EXECUTE p_zhl zh OUTPUT-调用存储过程获得随机账号while exists(SELECT * FROM accountinfoWHERE accountID=zh)账号重复则重新产生EXECUTE p_zhl zh OUTPUT-用身份证号判断用户是否存在,不存在则在customerinfo表增记录IF not exists(select :from customerinfo where cardID=cID)INSERT INTO customerInfo(customerID,customerName,cardID,phone,address ) select max(customerID)+1 ,name, cID, pho ,add from customerinfo-用身份证号查询客户编号select khbh = customerTD from customerinfo where cardTD = cID- -向账户信息表accountinfo增加记录INSERT INTOaccountInfo(accountID,savingType,openMoney,accountMoney,customerID)VALUES(zh,type,je,je,khbh)- 交易信息表插入记录,开户金额为第一笔存入金额INSERT INTO transInfo(accountID,transType,transMoney)VALUES(zh;存入;je)-输出开户成功信息select mess=,开户成功,系统为您产生的随机账号为了+zhEnd在对象资源管理器中“存储过程”上右击刷新/*以下为执行存储过程的语句,不要与创建存储过程的语句一起执行 为以下用户开一个新账户:李莹,身份证号:123456789,手机:78901,地址:江苏南通,开户金额:1001,存款类型:活期 */declare mess varchar(lOO)EXEC p_kh '李莹?123456789:78901;江苏南通:1 江"活期;mess output select mess13结果助消息(无列名)执行结果:11开户成功,系统为您产生的随机账号为-查看表中数据变化代码: select from customerlnfo select * from accountinfo select * from transInfo