简单的存储过程

今天刚刚学习了单表操作的存储过程,自己写一篇文章总结下今天刚学的,顺便在加深点印象!

 

 1--创建数据库--

 

ContractedBlock.gifExpandedBlockStart.gifCode
create database SingleTableOper
on
(
    Name
='SingleTableOper_data',
    FileName
='D:\SingleTableOper_data.mdf'
)
log on
(
    Name
='SingleTableOper_log',
    FileName
='D:\SingleTableOper_log.ldf'
)
use SingleTableOper

 

2--创建数据表--

 

ContractedBlock.gifExpandedBlockStart.gifCode
create table UserInfos
(
    userId 
int identity(1001,1primary key  not null,
    userName 
nvarchar(20not null,
    userpwd 
nvarchar(20not null,
    age 
int not null,
    sex 
nvarchar(10not null,
    email 
nvarchar(50not null
)

insert into UserInfos values('张三','123zs',23,'','zhangsan@163.com')
insert into UserInfos values('李四','123',24,'','lisi@163.com')

select * from UserInfos

 

3--查询---

 

ContractedBlock.gifExpandedBlockStart.gifCode
create proc SelectProc
(
    
@condition nvarchar(100)
)
as
begin 
    
declare @sqls nvarchar(500)
    
set @sqls='select * from UserInfos where 1=1'
    
if @condition!=''
    
begin
        
set @sqls=@sqls+@condition;
    
end 
end
exec sp_executesql @sqls


exec SelectProc 'and userName=''张三'''

 

4--添加---

 

ContractedBlock.gifExpandedBlockStart.gifCode
create proc InsertProc
(
    
@userName nvarchar(20),
    
@userpwd nvarchar(20),
    
@age int,
    
@sex nvarchar(10),
    
@email nvarchar(50)    
)
as
begin 
    
insert into UserInfos values(@username,@userpwd,@age,@sex,@email)
end

exec InsertProc 'zhangsan','123',25'','zhangsan@qq.com'

 

5---修改---

 

 

ContractedBlock.gifExpandedBlockStart.gifCode
create proc UpdateProc
(
    
@id int,
    
@userName nvarchar(20),
    
@userpwd nvarchar(20),
    
@age int,
    
@sex nvarchar(10),
    
@email nvarchar(50)
)
as
begin 
    
update UserInfos set userName=@userName,userpwd=@userpwd,age=@age,sex=@sex,email=@email where userId=@id
end

 

 

6 --删除---

 

 

create proc DeleteProc
(
    
@userid int
)
as
begin 
    
delete from UserInfos where userid=@userid
end

DeleteProc 
1003

 

7--简单的分页存储过程----

 

 

ContractedBlock.gifExpandedBlockStart.gifCode
create proc FenYe
(
    
@pageindex int,
    
@pagesize int,
    
@pagecount int
)
as
declare @regionCount int
select top (@pagesize* from UserInfos where userid not in (select top (@pagesize*@pageindex) userid from Userinfos )
set @regionCount=(select count(*from UserInfos)
if (@regionCount%@pagesize)!=0
begin 
    
set @pagecount=@regionCount/@pagesize+1
end

begin 
    
set @pagecount=@regionCount/@pagesize
end

 

转载于:https://www.cnblogs.com/dingguowendgw/archive/2009/11/10/1599897.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值