经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库运维 » MS SQL Server » 查看文章
ATM-简单SQL查询
来源:cnblogs  作者:RemMai  时间:2018/9/26 17:50:45  对本文有异议
  1.  
  1. use master
  2. go
  3. if exists(select * from sysDatabases where name = 'BankDB')
  4. drop database BankDB
  5. go
  6. create database BankDB
  7. go
  8. use BankDB
  9. go
  10. --建用户信息表
  11. if exists(select * from sysObjects where name = 'Xxl_UserInfo')
  12. drop table Xxl_UserInfo
  13. go
  14. create table Xxl_UserInfo
  15. (
  16. Xxl_User_Id int not null primary key identity ,
  17. Xxl_User_Name nvarchar(20) not null ,
  18. Xxl_User_Sex bit not null check(Xxl_User_Sex in (0,1)),
  19. Xxl_User_IDcard char(18) not null unique ,
  20. Xxl_User_Moblie char(11) not null check(Xxl_User_Moblie like '1[3579][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
  21. Xxl_User_Address nvarchar(50) not null
  22. )
  23. go
  24. --建用户卡信息表
  25. if exists(select * from sysObjects where name = 'Xxl_CardInfo')
  26. drop table Xxl_CardInfo
  27. go
  28. create table Xxl_CardInfo
  29. (
  30. Xxl_Card_No char(16) not null primary key check(Xxl_Card_No like '66668888[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]') ,
  31. Xxl_Card_pwd char(6) not null default('666888') ,
  32. From_Xxl_User_Id int not null references Xxl_UserInfo(Xxl_User_Id),
  33. Xxl_Card_Date DateTime not null default(getdate()) ,
  34. Xxl_Card_Balance decimal(18,2) not null check(Xxl_Card_Balance >= 0) ,
  35. Xxl_Card_State int not null check(Xxl_Card_State in (0,1,2)),
  36. Xxl_Card_Text nvarchar(50)
  37. )
  38. go
  39. --建交易信息表
  40. if exists(select * from sysObjects where name = 'Xxl_TransInfo')
  41. drop table Xxl_TransInfo
  42. go
  43. create table Xxl_TransInfo
  44. (
  45. Xxl_Trans_FlowNum int not null identity primary key ,
  46. From_Xxl_Card_No char(16) not null references Xxl_CardInfo(Xxl_Card_No) ,
  47. Xxl_Trans_Type int not null check(Xxl_Trans_Type in (1,2)) ,
  48. Xxl_Trans_Quota decimal(18,2) not null check(Xxl_Trans_Quota > 0) ,
  49. Xxl_Trans_Date DateTime not null default(getdate()) ,
  50. Xxl_Trans_ed_Balance decimal(18,2) not null check(Xxl_Trans_ed_Balance >= 0) ,
  51. Xxl_Trans_Text varchar(50) not null
  52. )
  53. go
  54. ------添加用户信息
  55. insert Xxl_UserInfo values('徐小龙','1','42028120000114125X','13071226588','湖北武汉')
  56. insert Xxl_UserInfo values('张小杨','0','42028119980515543X','13045114154','湖北武汉')
  57. insert Xxl_UserInfo values('吴小心','0','42028120001202114X','13071557444','湖北武汉')
  58. ------添加用户卡信息
  59. insert Xxl_CardInfo values('6666888845125214','666888','1','2006-2-12','600','0','使用')
  60. insert Xxl_CardInfo values('6666888865896548','666888','1','2007-2-20','3000','0','使用')
  61. insert Xxl_CardInfo values('6666888812454852','666888','2','2016-6-12','6300','0','使用')
  62. insert Xxl_CardInfo values('6666888852145698','666888','3','2018-3-24','500','0','使用')
  63. ------添加交易信息
  64. insert Xxl_TransInfo values('6666888845125214','1','300','2016-3-12','300','存入300元')
  65. insert Xxl_TransInfo values('6666888845125214','1','300','2017-5-3','600','存入300元')
  66. insert Xxl_TransInfo values('6666888865896548','1','6000','2013-9-1','6000','存入6000元')
  67. insert Xxl_TransInfo values('6666888865896548','2','3000','2014-9-1','3000','转账3000元给6666888812454852')
  68. insert Xxl_TransInfo values('6666888812454852','1','3000','2017-3-6','3000','6666888865896548转入的3000元')
  69. insert Xxl_TransInfo values('6666888812454852','1','3300','2017-12-1','6300','存入3300元')
  70. insert Xxl_TransInfo values('6666888852145698','1','3000','2018-6-3','3000','存入3000元')
  71. insert Xxl_TransInfo values('6666888852145698','2','2500','2018-7-3','500','取出2500元')
  72. ------备份交易信息表
  73. select * into Xxl_TransInfo_BAK from Xxl_TransInfo
  74. --------查询各表数据
  75. --select * from Xxl_UserInfo
  76. --select * from Xxl_CardInfo
  77. --select * from Xxl_TransInfo
  78. --select * from Xxl_TransInfo_BAK
  79. ----------------------------------------创建函数----------------------------------------
  80. --加逗号的函数
  81. if exists(select * from sysObjects where name='function_JiaDouhao')
  82. drop function function_JiaDouhao
  83. go
  84. create function function_JiaDouhao( @Money decimal(18,2))
  85. returns varchar(50) as
  86. begin
  87. declare @a varchar(50)= left(@Money,len(@Money)-3)
  88. declare @b varchar(50)= right(@Money,3)
  89. while (len(@a)>3)
  90. begin
  91. select @b = ','+right(@a,3)+@b
  92. select @a = left(@a,len(@a)-3)
  93. end
  94. return @a+@b
  95. end
  96. go
  97. ------------------------------------------结束------------------------------------------
  98. ----------------------------------------创建视图----------------------------------------
  99. --用户信息视图
  100. if exists(select * from sysObjects where name ='vw_UserInfo')
  101. drop view vw_UserInfo
  102. go
  103. create view vw_UserInfo
  104. as
  105. select
  106. Xxl_User_Id 编号,
  107. Xxl_User_Name 姓名,
  108. case Xxl_User_Sex
  109. when 0 then ''
  110. when 1 then ''
  111. end 性别,
  112. Xxl_User_IDcard 身份证,
  113. Xxl_User_Moblie 联系电话,
  114. Xxl_User_Address 籍贯
  115. from Xxl_UserInfo
  116. go
  117. --使用视图
  118. --select * from vw_UserInfo
  119. --卡信息视图
  120. if exists(select * from sysObjects where name='vw_CardInfo')
  121. drop view vw_CardInfo
  122. go
  123. create view vw_CardInfo
  124. as
  125. select
  126. Xxl_Card_No 卡号,
  127. Xxl_User_Name 姓名,
  128. Xxl_Card_Balance 余额,
  129. Xxl_Card_Date 开卡日期,
  130. case Xxl_Card_State
  131. when 0 then '正常'
  132. when 1 then '冻结'
  133. when 2 then '注销'
  134. end 状态,
  135. dbo.function_JiaDouhao(Xxl_Card_Balance) 货币表示
  136. from Xxl_UserInfo UserInfo inner join Xxl_CardInfo CardInfo on UserInfo.Xxl_User_Id = CardInfo.From_Xxl_User_Id
  137. go
  138. --使用视图
  139. --select * from vw_CardInfo
  140. --交易记录视图
  141. if exists(select * from sysObjects where name='vw_TransInfo')
  142. drop view vw_TransInfo
  143. go
  144. create view vw_TransInfo
  145. as
  146. select ----卡号,交易日期,交易类型,交易金额,余额,描述
  147. Xxl_Card_No 卡号,
  148. Xxl_Trans_Date 交易日期,
  149. case Xxl_Trans_Type
  150. when 1 then '存入'
  151. when 2 then '支取'
  152. end 交易类型,
  153. case Xxl_Trans_Type
  154. when 1 then '+'+convert(varchar(20),Xxl_Trans_Quota)
  155. when 2 then '-'+convert(varchar(20),Xxl_Trans_Quota)
  156. end 交易金额,
  157. Xxl_Trans_ed_Balance 余额,
  158. Xxl_Trans_Text 描述
  159. from Xxl_CardInfo CardInfo inner join Xxl_TransInfo TransInfo on CardInfo.Xxl_Card_No = TransInfo.From_Xxl_Card_No
  160. go
  161. --使用视图
  162. --select * from vw_TransInfo
  163. --------------------------------------------结束--------------------------------------------
  164. ----------------------------------------创建存储过程----------------------------------------
  165. --1 查询余额
  166. if exists(select * from sysObjects where name='p_SelectBalance')
  167. drop proc p_SelectBalance
  168. go
  169. create proc p_SelectBalance
  170. @CardNo char(16)
  171. as
  172. select 货币表示 as 余额 from vw_CardInfo where 卡号 = @CardNo
  173. go
  174. --exec p_SelectBalance '6666888845125214'
  175. --2 查询某两日期之间交易记录
  176. if exists(select * from sysObjects where name='p_SelectStart_StopDate')
  177. drop proc p_SelectStart_StopDate
  178. go
  179. create proc p_SelectStart_StopDate
  180. @CardNo char(16),
  181. @StartDate datetime,
  182. @StopDate datetime
  183. as
  184. select * from vw_TransInfo where 卡号 = @CardNo and 交易日期 >= @StartDate and 交易日期 < dateadd(dd,1,@StopDate)
  185. go
  186. --exec p_SelectStart_StopDate '6666888845125214','1990-1-1','2018-9-9'
  187.  
  188. --3 修改密码功能
  189. if exists(select * from sysObjects where name='p_Update_Pwd')
  190. drop proc p_Update_Pwd
  191. go
  192. create proc p_Update_Pwd
  193. @CardNo char(16),
  194. @CardPwdStart char(6),
  195. @CardPwdStop char(6)
  196. as
  197. update Xxl_CardInfo set Xxl_Card_pwd=@CardPwdStop where Xxl_Card_No = @CardNo and Xxl_Card_pwd = @CardPwdStart
  198. go
  199. --exec p_Update_Pwd '6666888845125214','666888','548888'
  200. --4 存款功能(备份)
  201. if exists(select * from sysObjects where name='p_SeveMoney')
  202. drop proc p_SeveMoney
  203. go
  204. create proc p_SeveMoney
  205. @CardNo char(16),
  206. @Quota decimal(18,2)
  207. as
  208. if @Quota < 0
  209. begin
  210. begin tran
  211. declare @err int = 0
  212. declare @startBalance decimal(18,2) = 0
  213. select @startBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @CardNo
  214. insert Xxl_TransInfo values(@CardNo,'1',@Quota,getdate(),(@startBalance + @Quota),('存入' + convert(varchar(50), @Quota) + ''))
  215. select @err = @@ERROR + @err
  216. update Xxl_CardInfo set Xxl_Card_Balance = (@startBalance + @Quota) where Xxl_Card_No = @CardNo
  217. select @err = @@ERROR + @err
  218. if @err = 0
  219. begin
  220. print '操作成功'
  221. commit tran
  222. return 0
  223. end
  224. begin
  225. print '未知错误!'
  226. rollback tran
  227. return -1
  228. end
  229. end
  230. else
  231. begin
  232. print '输入金额有误!'
  233. return -1
  234. end
  235. go
  236. --5 取款功能(备份)
  237. if exists(select * from sysObjects where name='p_GetMoney')
  238. drop proc p_GetMoney
  239. go
  240. create proc p_GetMoney
  241. @CardNo char(16),
  242. @Quota decimal(18,2)
  243. as
  244. if @Quota < 0
  245. begin
  246. declare @startBalance decimal(18,2)
  247. select @startBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @CardNo
  248. if @startBalance < @Quota
  249. begin
  250. begin tran
  251. declare @err int = 0
  252. insert Xxl_TransInfo values(@CardNo,'2',@Quota,getdate(),(@startBalance - @Quota),('取出' + convert(varchar(50), @Quota) + ''))
  253. select @err = @@ERROR + @err
  254. update Xxl_CardInfo set Xxl_Card_Balance = (@startBalance - @Quota) where Xxl_Card_No = @CardNo
  255. select @err = @@ERROR + @err
  256. if @err = 0
  257. begin
  258. print '操作成功'
  259. commit tran
  260. return 0
  261. end
  262. else
  263. begin
  264. print '未知错误!'
  265. rollback tran
  266. return -1
  267. end
  268. end
  269. else
  270. begin
  271. print '余额不足!'
  272. return -1
  273. end
  274. end
  275. else
  276. begin
  277. print '输入金额有误!'
  278. return -1
  279. end
  280. go
  281. --6 转帐功能(备份)
  282. if exists(select * from sysObjects where name='p_TeansferMoney')
  283. drop proc p_TeansferMoney
  284. go
  285. create proc p_TeansferMoney
  286. @FromCardNo char(16),
  287. @ToCardNo char(16),
  288. @Quota decimal(18,2)
  289. as
  290. if @FromCardNo = @ToCardNo
  291. begin
  292. if (select count(*) from Xxl_CardInfo where Xxl_Card_No = @ToCardNo) =1
  293. begin
  294. if @Quota < 0
  295. begin
  296. declare @FromStartBalance decimal(18,2) = 0 -- 转出前
  297. select @FromStartBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @FromCardNo
  298. if @FromStartBalance < @Quota
  299. begin
  300. begin tran
  301. declare @err int = 0
  302. declare @ToStartBalance decimal(18,2) = 0 --转入前
  303. select @ToStartBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @ToCardNo
  304. insert Xxl_TransInfo values(@FromCardNo,'1',@Quota,getdate(),(@FromStartBalance - @Quota), '转出' + convert(varchar(50), @Quota) + '元给'+@ToCardNo)
  305. select @err = @@ERROR + @err
  306. insert Xxl_TransInfo values(@ToCardNo,'2',@Quota,getdate(),(@ToStartBalance + @Quota),('' +@FromCardNo+ '转入'+ convert(varchar(50), @Quota) + ''))
  307. select @err = @@ERROR + @err
  308. update Xxl_CardInfo set Xxl_Card_Balance = (@FromStartBalance - @Quota) where Xxl_Card_No = @FromCardNo
  309. select @err = @@ERROR + @err
  310. update Xxl_CardInfo set Xxl_Card_Balance = (@ToStartBalance + @Quota) where Xxl_Card_No = @ToCardNo
  311. select @err = @@ERROR + @err
  312. if @err = 0
  313. begin
  314. print '操作成功!'
  315. commit tran
  316. return 0
  317. end
  318. else
  319. begin
  320. print '未知错误!'
  321. rollback tran
  322. return -1
  323. end
  324. end
  325. else
  326. begin
  327. print '余额不足!'
  328. return -1
  329. end
  330. end
  331. else
  332. begin
  333. print '输入金额有误!'
  334. return -1
  335. end
  336. end
  337. else
  338. begin
  339. print '转账账户不存在!'
  340. return -1
  341. end
  342. end
  343. else
  344. begin
  345. print '转账账户不可以为自己!'
  346. return -1
  347. end
  348. go
  349. --exec p_TeansferMoney '6666888812454852','6666888845125214',300.00
  350. --7 随机产生卡号(卡号格式为:8228 6688 XXXX XXXX 注:随机产生的卡号已经存在的不能用
  351.  
  352. if exists(select * from sysObjects where name='P_GenerateBankcard')
  353. drop proc P_GenerateBankcard
  354. go
  355. create proc P_GenerateBankcard
  356. @Card varchar(16) output
  357. as
  358. declare @ID varchar(20)
  359. select @ID = substring(convert( varchar(20) ,convert( Dec(10,10) ,Rand())), 3,8)
  360. select @Card = convert (varchar(8), '66668888')+@ID
  361. while(select count(*) from Xxl_CardInfo where Xxl_Card_No = @Card) = 1
  362. begin
  363. select @ID = substring(convert( varchar(20) ,convert( Dec(10,10) ,Rand())),3,8)
  364. select @Card = convert (varchar(8), '66668888')+@ID
  365. end
  366. go
  367. declare @Card varchar(16)
  368. exec P_GenerateBankcard @Card output
  369. select @Card as 卡号
  370. --8 开户功能
  371. if exists(select * from sysobjects where name = 'P_AccountOpening')
  372. drop proc P_AccountOpening
  373. go
  374.  
  375. create proc P_AccountOpening
  376. @Name nvarchar(20),
  377. @Sex bit,
  378. @IdCard char(18),
  379. @Moblie char(11),
  380. @Address nvarchar(50),
  381. @Pwd char(6)
  382. as
  383. if (select count(*) from Xxl_UserInfo where Xxl_User_IDcard =@IdCard) = 1
  384. begin
  385. begin tran
  386. declare @UserID int
  387. declare @err int = 0
  388. insert Xxl_UserInfo values(@Name,@Sex,@IdCard,@Moblie,@Address)
  389. select @err = @@ERROR + @err
  390. declare @Card varchar(16) = ''
  391. exec P_GenerateBankcard @Card output
  392. select @UserID = Xxl_User_Id from Xxl_UserInfo where Xxl_User_IDcard = @IdCard
  393. insert Xxl_CardInfo values(@Card,@Pwd,@UserId,getdate(),'0','0','使用')
  394. select @err = @@ERROR + @err
  395. if(@err = 0)
  396. begin
  397. print '开户成功!'
  398. commit tran
  399. return 0
  400. end
  401. else
  402. begin
  403. print '未知错误!'
  404. rollback tran
  405. return -1
  406. end
  407. end
  408. else
  409. begin
  410. print '同一个身份证只可开一个户!'
  411. return -1
  412. end
  413. go
  414. --select * from Xxl_UserInfo
  415. --select * from Xxl_CardInfo
  416. --exec P_AccountOpening '徐小龙','1','420281200001141255','13071226588','湖北武汉'
  417. --select * from Xxl_UserInfo
  418. --9 解冻功能
  419. if exists(select * from sysobjects where name = 'P_ThawAccount')
  420. drop proc P_ThawAccount
  421. go
  422. create proc P_ThawAccount
  423. @count int
  424. as
  425. if @count = 0
  426. begin
  427. update Xxl_CardInfo set Xxl_Card_State = 0
  428. print '解除冻结成功!'
  429. return 0
  430. end
  431. go
  432. --10 根据用户身份证,查询该用户下所有的银行卡信息
  433. if exists(select * from sysobjects where name = 'P_SelectCard')
  434. drop proc P_SelectCard
  435. go
  436. create proc P_SelectCard
  437. @IdCard nchar(18)
  438. as
  439. declare @ID varchar(20)
  440. select @ID = Xxl_User_Id from Xxl_UserInfo where Xxl_User_IDcard = @IdCard
  441. select * from Xxl_CardInfo where From_Xxl_User_Id = @ID
  442. go
  443. --exec P_SelectCard '42028120000114125X'
  444. --------------------------------------------结束--------------------------------------------
  445.  
  446. --select * from Xxl_CardInfo
  447. --select * from Xxl_UserInfo
  448. --select Xxl_User_Id from Xxl_UserInfo where Xxl_User_IDcard = '42028120000114125X'

 

use master 
go
if exists(select * from sysDatabases where name = 'BankDB')
drop database BankDB
go
create database BankDB
go
use BankDB
go
--建用户信息表
if exists(select * from sysObjects where name = 'Xxl_UserInfo')
drop table Xxl_UserInfo
go
create table Xxl_UserInfo
(
    Xxl_User_Id         int             not null    primary key identity ,
    Xxl_User_Name       nvarchar(20)    not null    ,
    Xxl_User_Sex        bit             not null    check(Xxl_User_Sex in (0,1)),
    Xxl_User_IDcard     char(18)        not null    unique ,
    Xxl_User_Moblie     char(11)        not null    check(Xxl_User_Moblie like '1[3579][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
    Xxl_User_Address    nvarchar(50)    not null 
)
go
--建用户卡信息表
if exists(select * from sysObjects where name = 'Xxl_CardInfo')
drop table Xxl_CardInfo
go
create table Xxl_CardInfo
(
    Xxl_Card_No       char(16)         not null    primary key check(Xxl_Card_No like '66668888[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]') ,
    Xxl_Card_pwd      char(6)          not null    default('666888') ,
    From_Xxl_User_Id    int              not null    references Xxl_UserInfo(Xxl_User_Id),
    Xxl_Card_Date      DateTime     not null    default(getdate()) ,
    Xxl_Card_Balance      decimal(18,2)    not null    check(Xxl_Card_Balance >= 0) ,
    Xxl_Card_State     int              not null    check(Xxl_Card_State in (0,1,2)),
    Xxl_Card_Text         nvarchar(50)
)
go
--建交易信息表
if exists(select * from sysObjects where name = 'Xxl_TransInfo')
drop table Xxl_TransInfo
go
create table Xxl_TransInfo
(
    Xxl_Trans_FlowNum      int             not null    identity primary key    ,
    From_Xxl_Card_No       char(16)        not null    references Xxl_CardInfo(Xxl_Card_No) ,
    Xxl_Trans_Type             int             not null    check(Xxl_Trans_Type in (1,2)) ,
    Xxl_Trans_Quota            decimal(18,2)   not null    check(Xxl_Trans_Quota > 0) ,
    Xxl_Trans_Date        DateTime        not null    default(getdate()) ,
    Xxl_Trans_ed_Balance      decimal(18,2)   not null    check(Xxl_Trans_ed_Balance >= 0) ,
    Xxl_Trans_Text            varchar(50)      not null
)
go
------添加用户信息
insert Xxl_UserInfo values('徐小龙','1','42028120000114125X','13071226588','湖北武汉')
insert Xxl_UserInfo values('张小杨','0','42028119980515543X','13045114154','湖北武汉')
insert Xxl_UserInfo values('吴小心','0','42028120001202114X','13071557444','湖北武汉')
------添加用户卡信息
insert Xxl_CardInfo values('6666888845125214','666888','1','2006-2-12','600','0','使用')
insert Xxl_CardInfo values('6666888865896548','666888','1','2007-2-20','3000','0','使用')
insert Xxl_CardInfo values('6666888812454852','666888','2','2016-6-12','6300','0','使用')
insert Xxl_CardInfo values('6666888852145698','666888','3','2018-3-24','500','0','使用')    
------添加交易信息
insert Xxl_TransInfo values('6666888845125214','1','300','2016-3-12','300','存入300元')
insert Xxl_TransInfo values('6666888845125214','1','300','2017-5-3','600','存入300元')
insert Xxl_TransInfo values('6666888865896548','1','6000','2013-9-1','6000','存入6000元')
insert Xxl_TransInfo values('6666888865896548','2','3000','2014-9-1','3000','转账3000元给6666888812454852')
insert Xxl_TransInfo values('6666888812454852','1','3000','2017-3-6','3000','6666888865896548转入的3000元')
insert Xxl_TransInfo values('6666888812454852','1','3300','2017-12-1','6300','存入3300元')
insert Xxl_TransInfo values('6666888852145698','1','3000','2018-6-3','3000','存入3000元')
insert Xxl_TransInfo values('6666888852145698','2','2500','2018-7-3','500','取出2500元')
------备份交易信息表
select * into Xxl_TransInfo_BAK from Xxl_TransInfo
--------查询各表数据
--select * from Xxl_UserInfo
--select * from Xxl_CardInfo
--select * from Xxl_TransInfo
--select * from Xxl_TransInfo_BAK
----------------------------------------创建函数----------------------------------------
--加逗号的函数
if exists(select * from sysObjects where name='function_JiaDouhao')
    drop function function_JiaDouhao
go
create function function_JiaDouhao( @Money decimal(18,2))
    returns varchar(50) as
    begin
        declare @a varchar(50)= left(@Money,len(@Money)-3)
        declare @b varchar(50)= right(@Money,3)
        while (len(@a)>3)
            begin
                select @b = ','+right(@a,3)+@b
                select @a = left(@a,len(@a)-3)
            end 
        return @a+@b
    end
go
------------------------------------------结束------------------------------------------
----------------------------------------创建视图----------------------------------------
--用户信息视图
if exists(select * from sysObjects where name    ='vw_UserInfo')
    drop view vw_UserInfo
go
create view vw_UserInfo 
    as                    
    select    
        Xxl_User_Id                编号,
        Xxl_User_Name            姓名,
        case Xxl_User_Sex 
            when 0 then ''
            when 1 then ''
            end                    性别,
        Xxl_User_IDcard            身份证,
        Xxl_User_Moblie            联系电话,
        Xxl_User_Address        籍贯
        from Xxl_UserInfo 
go
--使用视图
--select * from vw_UserInfo
--卡信息视图
if exists(select * from sysObjects where name='vw_CardInfo')
    drop view vw_CardInfo
go
create view vw_CardInfo 
    as                    
    select    
        Xxl_Card_No                                    卡号,
        Xxl_User_Name                                姓名,
        Xxl_Card_Balance                            余额,
        Xxl_Card_Date                                开卡日期,
        case Xxl_Card_State
            when 0 then '正常'
            when 1 then '冻结'
            when 2 then '注销'
        end                                            状态,
        dbo.function_JiaDouhao(Xxl_Card_Balance)    货币表示
        from Xxl_UserInfo UserInfo inner join Xxl_CardInfo CardInfo on UserInfo.Xxl_User_Id = CardInfo.From_Xxl_User_Id
go
--使用视图
--select * from vw_CardInfo
--交易记录视图
if exists(select * from sysObjects where name='vw_TransInfo')
    drop view vw_TransInfo
go
create view vw_TransInfo 
    as                    
    select    ----卡号,交易日期,交易类型,交易金额,余额,描述
        Xxl_Card_No                卡号,
        Xxl_Trans_Date            交易日期,
        case Xxl_Trans_Type
            when 1 then '存入'
            when 2 then    '支取'
        end                     交易类型,
        case Xxl_Trans_Type
            when 1 then '+'+convert(varchar(20),Xxl_Trans_Quota)
            when 2 then '-'+convert(varchar(20),Xxl_Trans_Quota)
            end                    交易金额,
        Xxl_Trans_ed_Balance    余额,
        Xxl_Trans_Text            描述
        from Xxl_CardInfo CardInfo inner join Xxl_TransInfo TransInfo on CardInfo.Xxl_Card_No = TransInfo.From_Xxl_Card_No
go
--使用视图
--select * from vw_TransInfo
--------------------------------------------结束--------------------------------------------
----------------------------------------创建存储过程----------------------------------------
--1、    查询余额
if exists(select * from sysObjects where name='p_SelectBalance')
    drop proc p_SelectBalance
go
create proc p_SelectBalance
    @CardNo char(16)
as
    select 货币表示 as 余额 from vw_CardInfo where 卡号 = @CardNo
go
--exec p_SelectBalance '6666888845125214'
--2、    查询某两日期之间交易记录
if exists(select * from sysObjects where name='p_SelectStart_StopDate')
    drop proc p_SelectStart_StopDate
go
create proc p_SelectStart_StopDate
    @CardNo char(16),
    @StartDate datetime,
    @StopDate datetime
as
    select * from vw_TransInfo where 卡号 = @CardNo and 交易日期 >= @StartDate and 交易日期 < dateadd(dd,1,@StopDate)
go
--exec p_SelectStart_StopDate '6666888845125214','1990-1-1','2018-9-9'

--3、    修改密码功能
if exists(select * from sysObjects where name='p_Update_Pwd')
    drop proc p_Update_Pwd
go
create proc p_Update_Pwd
    @CardNo char(16),
    @CardPwdStart char(6),
    @CardPwdStop char(6)
as
    update Xxl_CardInfo set Xxl_Card_pwd=@CardPwdStop where Xxl_Card_No = @CardNo and Xxl_Card_pwd = @CardPwdStart
go
--exec p_Update_Pwd '6666888845125214','666888','548888'
--4、    存款功能(备份)
if exists(select * from sysObjects where name='p_SeveMoney')
    drop proc p_SeveMoney
go
create proc p_SeveMoney
    @CardNo char(16),
    @Quota decimal(18,2)
as
    if @Quota < 0
    begin
        begin tran
        declare @err int = 0
        declare @startBalance decimal(18,2) = 0
        select @startBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @CardNo
        insert Xxl_TransInfo values(@CardNo,'1',@Quota,getdate(),(@startBalance + @Quota),('存入' + convert(varchar(50), @Quota) + ''))
        select @err = @@ERROR + @err
        update Xxl_CardInfo set Xxl_Card_Balance = (@startBalance + @Quota) where Xxl_Card_No = @CardNo
        select @err = @@ERROR + @err
        if @err = 0
        begin
            print '操作成功'
            commit tran
            return 0
        end
        begin
            print '未知错误!'
            rollback tran
            return -1
        end
    end
    else
    begin
        print '输入金额有误!'
        return -1
    end
go
--5、    取款功能(备份)
if exists(select * from sysObjects where name='p_GetMoney')
    drop proc p_GetMoney
go
create proc p_GetMoney
    @CardNo char(16),
    @Quota decimal(18,2)
as
    if @Quota < 0
    begin
        declare @startBalance decimal(18,2)
        select @startBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @CardNo
        if @startBalance < @Quota
        begin
            begin tran
            declare @err int = 0
            insert Xxl_TransInfo values(@CardNo,'2',@Quota,getdate(),(@startBalance - @Quota),('取出' +  convert(varchar(50), @Quota) + ''))
            select @err = @@ERROR + @err
            update Xxl_CardInfo set Xxl_Card_Balance = (@startBalance - @Quota) where Xxl_Card_No = @CardNo
            select @err = @@ERROR + @err
            if @err = 0
            begin
                print '操作成功'
                commit tran
                return 0
            end
            else
            begin
                print '未知错误!'
                rollback tran
                return -1
            end
        end
        else
        begin
            print '余额不足!'
            return -1
        end
    end
    else
    begin
        print '输入金额有误!'
        return -1
    end
go
--6、    转帐功能(备份)
if exists(select * from sysObjects where name='p_TeansferMoney')
    drop proc p_TeansferMoney
go
create proc p_TeansferMoney
    @FromCardNo char(16),
    @ToCardNo char(16),
    @Quota decimal(18,2)
as
    if @FromCardNo = @ToCardNo
    begin
        if (select count(*) from Xxl_CardInfo where Xxl_Card_No = @ToCardNo) =1
        begin
            if @Quota < 0
            begin
                declare @FromStartBalance decimal(18,2) = 0 -- 转出前
                select @FromStartBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @FromCardNo
                if @FromStartBalance < @Quota
                begin
                    begin tran
                    declare @err int = 0
                    declare @ToStartBalance decimal(18,2) = 0    --转入前
                    select @ToStartBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @ToCardNo
                    insert Xxl_TransInfo values(@FromCardNo,'1',@Quota,getdate(),(@FromStartBalance - @Quota), '转出' + convert(varchar(50), @Quota) + '元给'+@ToCardNo)
                    select @err = @@ERROR + @err
                    insert Xxl_TransInfo values(@ToCardNo,'2',@Quota,getdate(),(@ToStartBalance + @Quota),('' +@FromCardNo+ '转入'+ convert(varchar(50), @Quota) + ''))
                    select @err = @@ERROR + @err
                    update Xxl_CardInfo set Xxl_Card_Balance = (@FromStartBalance - @Quota) where Xxl_Card_No = @FromCardNo
                    select @err = @@ERROR + @err
                    update Xxl_CardInfo set Xxl_Card_Balance = (@ToStartBalance + @Quota) where Xxl_Card_No = @ToCardNo
                    select @err = @@ERROR + @err
                    if @err = 0
                    begin
                        print '操作成功!'
                        commit tran
                        return 0
                    end
                    else
                    begin
                        print '未知错误!'
                        rollback tran
                        return -1
                    end
                end
                else
                begin
                    print '余额不足!'
                    return -1
                end
            
            end
            else
            begin
                print '输入金额有误!'
                return -1
            end
        end
        else
        begin
            print '转账账户不存在!'
            return -1
        end
    end
    else
    begin
        print '转账账户不可以为自己!'
        return -1
    end
go
--exec p_TeansferMoney '6666888812454852','6666888845125214',300.00
--7、    随机产生卡号(卡号格式为:8228 6688 XXXX XXXX) 注:随机产生的卡号已经存在的不能用 

if exists(select * from sysObjects where name='P_GenerateBankcard')
    drop proc P_GenerateBankcard
go
create proc P_GenerateBankcard
    @Card varchar(16) output
as 
    declare @ID  varchar(20)
    select @ID =  substring(convert( varchar(20) ,convert( Dec(10,10) ,Rand())), 3,8)
    select @Card  = convert (varchar(8), '66668888')+@ID
    while(select count(*) from Xxl_CardInfo where Xxl_Card_No = @Card) = 1
    begin
        select @ID =  substring(convert( varchar(20) ,convert( Dec(10,10) ,Rand())),3,8)
        select @Card  = convert (varchar(8), '66668888')+@ID
    end
go
declare @Card varchar(16)
exec P_GenerateBankcard @Card output
select @Card as 卡号
--8、    开户功能
if exists(select * from sysobjects where name = 'P_AccountOpening')
    drop proc P_AccountOpening
go

create proc P_AccountOpening
    @Name nvarchar(20),
    @Sex bit,
    @IdCard char(18),
    @Moblie char(11),
    @Address nvarchar(50),
    @Pwd char(6)
as
    if (select count(*) from Xxl_UserInfo where Xxl_User_IDcard =@IdCard) = 1
    begin
        begin tran
        declare @UserID int
        declare @err int = 0
        insert Xxl_UserInfo values(@Name,@Sex,@IdCard,@Moblie,@Address)
        select @err =  @@ERROR + @err
        declare @Card varchar(16) = ''
        exec P_GenerateBankcard @Card output
        select @UserID = Xxl_User_Id from Xxl_UserInfo where Xxl_User_IDcard = @IdCard
        insert Xxl_CardInfo values(@Card,@Pwd,@UserId,getdate(),'0','0','使用')
        select @err =  @@ERROR + @err
        if(@err = 0)
        begin
            print '开户成功!'
            commit tran
            return 0
        end 
        else 
        begin
            print '未知错误!'
            rollback tran
            return -1
        end
    end
    else
    begin
        print '同一个身份证只可开一个户!'
        return -1
    end
go
--select * from Xxl_UserInfo
--select * from Xxl_CardInfo
--exec P_AccountOpening '徐小龙','1','420281200001141255','13071226588','湖北武汉' 
--select * from Xxl_UserInfo
--9、    解冻功能
if exists(select * from sysobjects where name = 'P_ThawAccount')
    drop proc P_ThawAccount
go
create proc P_ThawAccount
    @count int
as
    if @count = 0
        begin
            update Xxl_CardInfo set Xxl_Card_State = 0
            print '解除冻结成功!'
            return 0
        end 
go
--10、    根据用户身份证,查询该用户下所有的银行卡信息
if exists(select * from sysobjects where name = 'P_SelectCard')
    drop proc P_SelectCard
go
create proc P_SelectCard
    @IdCard nchar(18)
as
    declare @ID varchar(20)
    select @ID = Xxl_User_Id from Xxl_UserInfo where Xxl_User_IDcard = @IdCard
    select * from Xxl_CardInfo where From_Xxl_User_Id = @ID
go
--exec P_SelectCard '42028120000114125X'
--------------------------------------------结束--------------------------------------------

--select * from Xxl_CardInfo
--select * from Xxl_UserInfo
--select Xxl_User_Id from Xxl_UserInfo where Xxl_User_IDcard = '42028120000114125X'
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号