存储过程调用示例

本文详细介绍了一个银行系统的数据库设计过程,包括数据库的建立、数据表的创建、规则与约束的设定及存储过程的实现等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.建立数据库

---------------------------------------

use master

go

if exists(select * from sysdatabases where name='bank')

drop database bank

go

create database bank

on

(

name='bank_data',

filename='D:/bank/bank_data.mdf',

size=10,

maxsize=100,

filegrowth=10%

)

log on

(

name='bank_log',

filename='D:/bank/bank_log.ldf',

size=1,

maxsize=10,

filegrowth=15%

)

go

2.创建数据表

---------------------------------------------------------

use bank

go 

--/*--创建用户信息表

create table userInfo

(

customerID  int  primary key,   --顾客编号

customerNAME  char(5)  not null,--开户名

pID  char(20) unique not null,  --身份证号

telephone char(15) not null,    --电话号码

address  varchar(15)            --地址

)

--*/

--/*

--银行卡信息表

create table cardInfo

(

cardID char(20) primary key,                    --卡号

curType char(10) not null,                      --货币种类

savingType char(4) not null,                    --存款类型

opDate datetime not null,                       --开户日期

openMoney money not null,                       --开户金额

balance money not null,                         --余额

passWord char(6) not null,                      --密码

isReportLoss char(2) not null,                  --是否挂失

customerID int not null references userinfo(customerID)  --顾客编号

)

--*/ 

3.创建规则、约束和默认值

--------------------------------------------------------------

use bank

go

-------------------------------------------------------------

--userInfo

--创建规则,约束身份证号

create rule pID_Rule

as len(@Value)=15 or len(@Value)=18

--------------

sp_bindrule 'pID_Rule','useraInfo.pID'

/*心得:规则的创建和绑定必须分开执行,才能成功*/

--------------

--创建规则,约束电话号码,比如:和-84517814

create rule Phone_Rule

as len(@value)<14

sp_bindrule 'Phone_Rule','userInfo.telephone'

---------------------------------------------------------

--cardInfo

alter table cardInfo add default 'RMB' for curType

------------------

create rule savType_Rule

as @value='活期' or @value='定期'

sp_bindrule 'savType_Rule','cardInfo.savingType'

-------------------

--开户日期为当前系统时间

alter table cardInfo add default getdate() for openDate

-------------------

--开户金额至少为元钱

create rule openMoney_Rule

as @value>=10.00

sp_bindrule 'openMoney_Rule','cardInfo.openMoney'

-------------------

--卡上余额至少为元钱

create rule balance_Rule

as @value>=10.00

sp_bindrule 'balance_Rule','cardInfo.balance'

-------------------

--密码默认为个

alter table cardInfo add default '888888' for passWord

-------------------

--是否挂失

create rule isReportLoss_Rule

as @value='' or @value=''

sp_bindrule 'isReportLoss_Rule','cardInfo.isReportLoss'

--默认为

alter table cardInfo add default '' for isReportLoss  

4.存储过程调用

 存储过程proc_openAccount调用存储过程proc_randCardID产生的随机号码

-----------------------------------------------------------------------------

use bank

go

--创建产生随机卡号的存储过程proc_randCardID

alter procedure proc_randCardID

@cardID char(20) output

as

begin

  select @cardID='1000 3576 ' + SUBSTRING(CAST (RAND() as char),3,4) + ' ' +SUBSTRING(CAST (RAND() as char),3,4)

  select '随机卡号为'=@cardID 

end

/*

SUBSTRING函数:

语法:SUBSTRING ( expression ,start , length )

返回值:返回字符表达式、二进制表达式、文本表达式或图像表达式的一部分。

参数:

expression:是字符串、二进制字符串、文本、图像、列或包含列的表达式。不要使用包含聚合函数的表达式。

start:指定子字符串开始位置的整数。start 可以为 bigint 类型

length:一个正整数,指定要返回的 expression 的字符数或字节数。如果 length 为负,则会返回错误。length 可以是 bigint 类型

/

*execute proc_randCardID 

@cardID=value

*/

*/

--------------------------------------------------------------------

--创建开户的存储过程proc_openAccount

create procedure proc_openAccount

@customerName char(5),@pID char(20),@telephone char(15),

@openMoney money,@savingType char(4),@address varchar(20)

as

declare @cardID char(20),@customerID int

select @customerID=(select max(customerID) from userInfo)+1

--userInfo表中插入数据

insert into userInfo

(

customerID,

customerName,

pID,

telephone,

address

)

values

(

@customerID,

@customerName,

@pID,

@telephone,

@address

)

--cardInfo表中插入数据

exec proc_randCardID @cardID output

--此处一定要将@cardID声明为output,否则将无法从存储过程proc_randCardID获得产生的随--机卡号

insert into cardInfo

(

cardID,

savingType,

openMoney,

balance,

customerID

)

values

(

@cardID,

@savingType,

@openMoney,

@openMoney,

@customerID

)

go

--测试数据

/*

exec  proc_openAccount 

'李博伦','334456889012675588','0851-4878789','1800','定期','河南洛阳'

exec  proc_openAccount

'刘东旭','140100198001013499','1230-4569781','3500','活期','山西太原市'

*/ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值