您的位置:首页 > 其它

ATM自动取款机(续)<完成>

2008-08-29 20:59 197 查看
2008-04-13

1 use bankDB
2 go
3
4 --select * from userInfo
5 --select * from cardInfo
6 --select * from transInfo
7 go
8
9
10 --插入测试数据
11 insert into userInfo (customerName,pId,telephone,address)
12 values('张三','123456789012345','010-67898978','北京海淀')
13 insert into cardInfo (cardID,savingType,openDate,openMoney,balance,pass,customerId)
14 values('1010 3576 1234 5678','活期','2008-01-09',1000.00,1000.00,'123456',1)
15
16
17 insert into userInfo (customerName,pId,telephone,address)
18 values('李四','12345678901234897x','0536-67898978','诸城')
19 insert into cardInfo (cardID,savingType,openDate,openMoney,balance,pass,customerId)
20 values('1010 3576 1212 1134','定期','2008-02-09',1.00,1.00,'123456',2)
21 go
22
23 --插入交易信息
24
25 --张三取款900元
26 set nocount on
27 begin transaction
28 declare @errSum int --错误号
29 set @errSum = 0 --错误号赋初值
30
31 declare @customerId varchar(10) --顾客编号
32 --查询顾客的编号
33 select @customerId = customerId from userInfo where customerName = '张三'
34 set @errSum = @errSum + @@error
35
36 declare @cardId varchar(19) --卡号
37
38 --查询卡号
39 select @cardId = cardId from cardInfo where customerId = @customerId
40 set @errSum = @errSum + @@error
41
42 /*取款时,1,密码要正确 2,卡没有挂失*/
43
44 --插入交易信息
45 insert into transInfo (transDate,cardId,transType,transMoney)
46 values(getdate(),@cardId,'支取','900')
47 set @errSum = @errSum + @@error
48 --更新卡号信息表的余额
49 update cardInfo set balance = balance - 900 where cardId = @cardId
50 set @errSum = @errSum + @@error
51
52 --如果有错误,就回滚事务,否则提交事务
53 if @errSum <> 0
54 begin
55 print '交易失败,帐户信息无更改'
56 rollback transaction
57 end
58 else
59 begin
60 print '交易成功,提交事务,交易信息写入数据库'
61 commit transaction
62 end
63 go
64
65 --李四存款5000元
66
67 set nocount on
68 begin transaction
69
70 declare @errSum int --错误号
71 set @errSum = 0
72
73 declare @customerId varchar(10) --顾客卡号
74 --查询顾客的编号
75 select @customerId = customerId from userInfo where customerName = '李四'
76 set @errSum = @errSum + @@error
77
78 declare @cardId varchar(19)--卡号
79 --查询卡号
80 select @cardId = cardId from cardInfo where customerId = @customerId
81 set @errSum = @errSum + @@error
82
83 --插入交易信息
84 insert into transInfo (transDate,cardId,transType,transMoney)
85 values(getdate(),@cardId,'存入','5000')
86 set @errSum = @errSum + @@error
87
88 --更新卡号信息表的余额
89 update cardInfo set balance = balance + 5000 where cardId = @cardId
90 set @errSum = @errSum + @@error
91
92 --如果有错误,就回滚事务,否则提交事务
93 if @errSum <> 0
94 begin
95 print '交易失败,帐户信息无更改'
96 rollback transaction
97 end
98 else
99 begin
100 print '交易成功,提交事务,交易信息写入数据库'
101 commit transaction
102 end
103 go
104
105 /*---常规业务模拟--*/
106 --修改密码
107
108 set nocount on
109 begin transaction
110 declare @errSum int --累加错误号
111 set @errSum = 0
112
113 declare @customerId varchar(10) --顾客编号
114 --查询顾客编号
115 select @customerId = customerId from userInfo where customerName = '张三'
116 set @errSum = @errSum + @@error
117
118 declare @cardId varchar(19) --顾客卡号
119 --查询顾客卡号
120 select @cardId = cardId from cardInfo where customerId = @customerId
121 set @errSum = @errSum + @@error
122 --更新密码
123 update cardInfo set pass = '123456' where cardId = @cardId
124 set @errSum = @errSum + @@error
125
126 --如果有错误,就不修改,回滚事务,否则提交事务
127 if @errSum <> 0
128 begin
129 print '修改密码失败'
130 rollback transaction
131 end
132 else
133 begin
134 print '修改密码成功'
135 commit transaction
136 end
137 go
138
139 --可以写一个存储过程来修改密码(带参数的)
140 set nocount on
141 if exists(select * from sysobjects where name = 'proc_updatePass')
142 drop proc proc_updatePass
143 go
144
145 create proc proc_updatePass
146 @customerName varchar(10)
147 as
148
149 begin transaction
150 declare @errSum int --累加错误号
151 set @errSum = 0
152
153 --查询顾客的编号
154 declare @customerId varchar(10)
155 select @customerId = customerId from userInfo where customerName = @customerName
156 set @errSum = @errSum + @@error
157
158 --查询顾客的卡号
159 declare @cardId varchar(19)
160 select @cardId = cardId from cardInfo where customerId = @customerId
161 set @errSum = @errSum + @@error
162 --更新密码
163 update cardInfo set pass = '109456' where cardId = @cardId
164 set @errSum = @errSum + @@error
165
166 if @errSum <> 0
167 begin
168 print '修改密码失败'
169 rollback transaction
170 end
171 else
172 begin
173 print '修改密码成功'
174 commit transaction
175 end
176 go
177
178 --测试修改密码的存储过程
179
180 exec proc_updatePass '李四'
181 --挂失
182
183 set nocount on
184 begin transaction
185 declare @errSum int --累加错误号
186 set @errSum = 0
187 --顾客的编号
188 declare @customerId varchar(10)
189 select @customerId = customerId from userInfo where customerName = '李四'
190 set @errSum = @errSum + @@error
191
192 --顾客的卡号
193 declare @cardId varchar(19)
194 select @cardId = cardId from cardInfo where customerId = @customerId
195 set @errSum = @errSum + @@error
196
197 --更新挂失为'是'
198 update cardInfo set isReportLoss = '是' where cardId = @cardId
199 set @errSum = @errSum + @@error
200
201 --如果等于0 ,刚回滚事务
202 if @errSum <> 0
203 begin
204 print '挂失失败'
205 rollback transaction
206 end
207 else
208 begin
209 print '挂失成功'
210 commit transaction
211 end
212 go
213
214 --统计银行的资金流通余额和盈利结算
215
216 --银行流通余额
217
218 set nocount on
219 begin transaction
220 declare @errSum int --累加错误号
221 set @errSum = 0
222
223 declare @inMoney money --存入的钱数
224 select @inMoney = sum(transMoney) from transInfo where transType = '存入'
225 set @errSum = @errSum + @@error
226
227 declare @outMoney money --支取的钱数
228 select @outMoney = sum(transMoney) from transInfo where transType = '支取'
229 set @errSum = @errSum + @@error
230
231 --银行流通余额
232 print '银行流通余额总计为:'+ convert(varchar(20),(@inMoney - @outMoney))+ + 'RMB'
233
234 --盈利结算
235 declare @getSumMoney money
236 set @getSumMoney = @outMoney * 0.008 - @inMoney * 0.003
237 print '盈利结算为:'+convert(varchar(20),@getSumMoney) + 'RMB'
238
239 if @errSum <> 0
240 begin
241 print '计算失败'
242 rollback transaction
243 end
244 else
245 begin
246 print '计算成功'
247 commit transaction
248 end
249 go
250
251 --查询本周开户的卡号,显示该卡的相关信息
252 declare @weekday int
253 --当前日期的星期的第几天(周日是第一天(美国))
254 select @weekday = datepart(weekday,getdate())
255
256 declare @cardId varchar(50) --卡号
257
258 --查询是否存在本周开户的用户
259 if exists (select * from cardInfo where datediff(day,opendate,getdate()) < @weekday)
260 begin
261 select @cardId = cardId from cardInfo where datediff(day,openDate,getdate()) < @weekday
262 print '本周开户的卡号为 :' + @cardId
263
264 end
265 else
266 begin
267 print '本周没有开户的卡号'
268 end
269
270 --查询本月交易金额最高的卡号
271 select cardId as 卡号
272 from transInfo
273 where transMoney = (select distinct max(transMoney) from transInfo)
274 and datepart(mm,transDate) = datepart(mm,getdate())
275
276
277 --查询挂失帐号的客户信息
278 select * from userInfo where customerId =(select customerId from cardInfo where isReportLoss = '是')
279 select *from cardInfo
280
281 --催款提醒业务
282 select customerName as 客户姓名,telephone as 联系电话,balance as 余额
283 from userInfo inner join cardInfo on userInfo.customerId = cardInfo.customerId
284 where userInfo.customerId in
285 (select customerId from cardInfo where balance <=200)
286
287 --创建索引和视图
288
289 --给交易表的卡号cardId 字段创建重复索引,以便加速查询,填充因子为70%
290
291 if exists (select * from sysindexes where name = 'ix_transInfo_cardID')
292 drop index transInfo.ix_transInfo_cardID
293
294 create nonclustered index ix_transInfo_cardID
295 on transInfo(cardID)
296 with fillfactor = 75
297 go
298
299 --按指定索引查询张三的交易记录
300
301 --顾客的卡号
302 declare @customerId varchar(10)
303 select @customerId = customerId from userInfo where customerName = '张三'
304
305 --顾客的卡号
306 declare @cardId varchar(19)
307 select @cardId = cardId from cardInfo where customerId = @customerId
308
309 --按照索引查询
310 select * from transInfo
311 with(index=ix_transInfo_cardID)
312 where cardId = @cardId
313
314 --创建视图
315
316 --用户表的视图
317
318 if exists(select * from sysobjects where name = 'view_userInfo')
319 drop view view_userInfo
320 go
321
322 create view view_userInfo
323 as
324 select customerId as 客户编号,customerName as 客户姓名,pId as 身份证号,telephone as 电话,address as 地址
325 from userInfo
326 go
327 --卡号表的视图
328 if exists(select * from sysobjects where name = 'view_cardInfo')
329 drop view view_cardInfo
330 go
331
332 create view view_cardInfo
333 as
334 select cardID as 卡号,curType as 货币类型,savingType as 存款类型,openDate as 开户日期,
335 openMoney as 开户金额,balance as 余额,pass as 密码,IsReportLoss as 是否挂失,customerId as 客户编号
336 from cardInfo
337 go
338
339 --交易表的视图
340 if exists(select * from sysobjects where name = 'view_transInfo')
341 drop view view_transInfo
342 go
343
344 create view view_transInfo
345 as
346 select transDate as 交易日期,cardId as 卡号,transType as 交易类型,transMoney as 交易金额,remark as 备注
347 from transInfo
348 go
349
350 select * from view_cardInfo
351 go
352
353
354 --创建触发器
355
356 if exists (select * from sysobjects where name = 'trig_trans')
357 drop trigger trig_trans
358 go
359
360 --创建交易表的Insert 触发器
361 create trigger trig_trans
362 on transInfo
363 for insert
364 as
365 declare @transType varchar(4) --交易类型
366 declare @outMoney money --支取的钱数
367 declare @myCardId varchar(20) --卡号
368
369
370 --从插入表查询插入的数据
371 select @transType=transType,@outMoney=transMoney,@myCardId=cardId
372 from inserted
373
374 --交易类型为 "支取"
375 if(@transType = '支取')
376 begin
377 --帐户的余额
378 declare @balance money
379 select @balance = balance from cardInfo where cardId = @myCardId
380
381 --余额大于取款的金额,才能进行交易
382 if(@balance-@outMoney >=1)
383 begin
384
385 update cardInfo set balance = balance - @outMoney where cardId = @myCardId
386 print '交易成功'
387 print @balance-@outMoney
388 end
389 else --余额小于取款的金额时,提示错误
390 begin
391 raiserror('交易失败,余额不足',16,1)
392 rollback tran
393 end
394 end
395 else --交易类型为 '存入'
396 begin
397 update cardInfo set balance = balance + @outMoney where cardId = @myCardId
398 print @balance + @outMoney
399 end
400
401 go
402 select * from userInfo
403
404
405 --测试交易表的Insert 触发器
406
407 declare @card char(19)
408 select @card = cardId from cardInfo inner join userInfo on
409 cardInfo.customerId = userInfo.customerId where customerName = '张三'
410
411 insert into transInfo (transType,cardId,transMoney) values ('支取',@card,1000)
412 go
413
414 declare @card char(19)
415 select @card = cardId from cardInfo inner join userInfo on
416 cardInfo.customerId = userInfo.customerId where customerName = '李四'
417 insert into transInfo (transType,cardId,transMoney) values ('存入',@card,200)
418
419 go
420 select * from transInfo
421 select * from cardInfo
422
423 go
424
425 --创建存储过程
426 if exists (select * from sysobjects where name = 'proc_takeMoney')
427 drop proc proc_takeMoney
428 go
429
430 --存取款的存储过程
431 create procedure proc_takeMoney
432 @card varchar(19),--卡号
433 @money money,--交易金额
434 @type varchar(4),--交易类型
435 @inputPass varchar(6)= ''--帐户密码
436 as
437 declare @pass varchar(6) --保存表中查询的密码
438 declare @isReportLoss char(2) --看帐户是否挂失
439
440 select @pass = pass,@isReportLoss = isReportLoss from cardInfo where cardId = @card
441
442 if(@isReportLoss = '否')--如果没有挂失,才能进行存取款交易
443 begin
444 if(@type = '支取')--交易类型为 "支取" 时,需要进行密码的判断
445 begin
446 if(@pass = @inputPass) --如果密码输入正确,进行交易,然后自动调用触发器,以更新相应的记录
447 begin
448 insert into transInfo (cardId,transType,transMoney)values (@card,@type,@money)
449 print '支取成功'
450 end
451 else
452 print '密码不正确' --密码不正确时,输入提示错误
453 end
454 else --交易类型为 "存入" 时
455 begin
456 insert into transInfo (cardId,transType,transMoney)values (@card,@type,@money)
457 print '存款成功'
458 end
459 end
460 else
461 begin
462 print '不能进行交易,次卡已经挂失'
463 end
464
465 go
466 --测试取款
467 declare @card varchar(19)--要取款帐户的卡号
468
469 select @card = cardId from cardInfo inner join userInfo on
470 cardInfo.customerId = userInfo.customerId where customerName = '张三'
471
472 exec proc_takeMoney @card,50,'支取','123456'
473 go
474 --测试存款
475 declare @card varchar(19)--要存款帐户的卡号
476
477 select @card = cardId from cardInfo inner join userInfo on
478 cardInfo.customerId = userInfo.customerId where customerName = '李四'
479
480 exec proc_takeMoney @card,50,'存入'
481 go
482
483 select * from cardInfo
484 select * from userInfo
485 go
486
487 --产生随机卡号的存储过程
488 if exists (select * from sysobjects where name = 'proc_randCardId')
489 drop proc proc_randCardId
490 go
491
492 create proc proc_randCardId
493 @randCardId varchar(19) output
494 as
495 declare @randNum numeric(15,8)--存放产生的随机卡号
496
497 declare @temp varchar(10) --临时变量,存放转换成String类型的随机卡号
498
499 set @randNum = rand((datepart(yy,getdate())*100000)+ (datepart(ss,getdate())*1000)+(datepart(ms,getdate())))
500
501 set @temp = convert(varchar(10),@randNum)
502 set @randCardId = '1010 3576 '+ substring(@temp,3,4) + ' '+substring(@temp,7,4)
503 go
504 --测试产生随机卡号
505 declare @myCardId varchar(19)
506 exec proc_randCardId @myCardId output
507 print '产生的随机卡号为 :'+@myCardId
508 go
509
510 --开户的存储过程
511 if exists (select * from sysobjects where name = 'proc_openAccount')
512 drop proc proc_openAccount
513 go
514
515 set nocount on
516 go
517 create proc proc_openAccount
518
519 @customerName varchar(8), --顾客姓名
520 @PID varchar(18), --身份证
521 @telephone varchar(13), --电话
522 @openMoney money, --开户金额
523 @savingType varchar(8), --存储类型
524 @pass char(6), --密码
525 @address varchar(50) = ''--地址
526 as
527 --卡号
528 declare @myCardId varchar(19)
529 --累加错误号
530 declare @errSum int
531 set @errSum = 0
532
533 --开始事务
534 begin tran
535 --执行产生随机卡号的存储过程
536 exec proc_randCardId @myCardId output
537
538 --如果产生的随机卡号已经存在,就重新产生一下个随机卡号
539 while exists(select * from cardInfo where cardId = @myCardId)
540 exec proc_randCardId @myCardId output
541
542 --用户表中插入信息
543 insert into userInfo (customerName,Pid,telephone,address)
544 values(@customerName,@PID,@telephone,@address)
545 set @errSum = @errSum + @@error
546
547 --顾客的编号
548 declare @cur_customerId varchar(19)
549 --查询顾客的编号
550 select @cur_customerId = customerId from userInfo where Pid = @pid
551 set @errSum = @errSum + @@error
552
553 insert into cardInfo (cardId,savingType,openMoney,balance,pass,customerId)
554 values(@mycardId,@savingType,@openMoney,@openMoney,@pass,@cur_customerId)
555 set @errSum = @errSum + @@error
556
557 declare @openDate datetime
558 select @openDate = openDate from cardInfo where cardId = @myCardId
559 --set @errSum = @errSum + @@error
560
561 if @errSum <>0
562 begin
563 print '开户失败'
564 rollback tran
565 end
566 else
567 begin
568
569 print '尊敬的用户,开户成功!系统为您产生的随机卡号为:' + @myCardId + '开户日期'+ convert(varchar(20),@openDate,111 )+'开户金额'+ convert(varchar(20),@openMoney)
570 commit tran
571 end
572 go
573
574 --测试开户的存储过程
575 exec proc_openAccount '王五','334456889012678','2222-56789678',1000,'活期','123456','河南新乡'
576 go
577
578 exec proc_openAccount '赵二','213445678912342222','010-4987654',1,'定期','123455','北京'
579 go
580
581
582 --创建转帐的事务
583 if exists (select * from sysobjects where name = 'proc_transfer')
584 drop proc proc_transfer
585 go
586
587 create proc proc_transfer
588 @card1 varchar(19),--要支取的卡号
589 @card2 varchar(19),--要存入的卡号
590 @outmoney money --转帐的金额
591 as
592 begin tran
593 set nocount on
594 declare @errSum int
595 set @errSum = 0
596 print '开始转账,请稍后'
597 --往交易表中插入支取的信息
598 insert into transInfo (transType,transMoney,cardid)values('支取',@outmoney,@card1)
599 set @errSum = @errSum + @@error
600 --往交易表中插入存入的信息
601 insert into transInfo (transType,transMoney,cardId)values('存入',@outmoney,@card2)
602 set @errSum = @errSum + @@error
603
604 --更新@card1用户的余额
605 update cardInfo set balance = balance - @outmoney where cardId = @card1
606 set @errSum = @errSum + @@error
607
608 print '交易成功,交易金额:'+ convert(varchar(10),@outmoney)
609 print '卡号'+@card1
610
611 declare @balance1 money--支取用户的余额
612 select @balance1 = balance from cardInfo where cardId = @card1
613 print '余额'+convert(varchar(10),@balance1)
614
615 --更新@card2用户的余额
616 update cardInfo set balance = balance + @outmoney where cardId = @card2
617 set @errSum = @errSum + @@error
618
619 print '交易成功,交易金额:'+convert(varchar(10), @outmoney)
620 print '卡号'+@card2
621
622 declare @balance2 money --存入帐户的余额
623 select @balance2 = balance from cardInfo where cardId = @card2
624 print '余额'+convert(varchar(10),@balance2)
625
626 if(@errSum <> 0)
627 begin
628 print '转帐失败'
629 rollback tran
630 end
631 else
632 begin
633
634 print '转帐成功'
635 commit tran
636 end
637
638 go
639 --测试数据
640 set nocount on
641 declare @card1 varchar(19)
642 declare @card2 varchar(19)
643 select @card1 = cardId from cardInfo inner join userInfo on
644 cardInfo.customerId = userInfo.customerId where customerName = '李四'
645
646 select @card2 = cardId from cardInfo inner join userInfo on
647 cardInfo.customerId = userInfo.customerId where customerName = '张三'
648
649 exec proc_transfer @card1,@card2,100
650 select *from cardInfo
651 select *from userInfo
652
653
654 --创建登录帐号和数据库用户
655 use master
656 go
657 --添加登录
658 exec sp_addlogin 'sysAdministrator' ,'1234'
659 go
660
661 use bankDB
662 go
663 --添加数据库用户
664 exec sp_grantdbaccess 'sysAdministrator','sysAdminDBUser'
665 go
666
667 --对数据库用户授权
668 grant select ,insert ,update,delete on transInfo to sysAdminDBUser
669 go
670
671
672
673
674
675
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: