首页 > 其他 > 详细

增删改查存储过程

时间:2020-01-07 10:32:29      阅读:83      评论:0      收藏:0      [点我收藏+]
select * from UserInfo

---------------------------------添加存储过程------------------------------
if OBJECT_ID(P_add) is not null
drop proc P_add
go
create proc P_add
--参数
@UserName varchar(100),
@UserPwd varchar(100),
@UserSex int,
@UserDate datetime,
@msg varchar(max) out
as
--变量
declare @bcount int,@acount int
--赋值
set @bcount=(select COUNT(1) from UserInfo)--添加之前条数
insert into UserInfo values(@UserName,@UserPwd,@UserSex,@UserDate)
set @acount=(select COUNT(1) from UserInfo)--添加之后的条数
if    @acount>@bcount
set @msg=添加成功

-----------------------------------删除存储过程------------------------------
if OBJECT_ID(P_Delete) is not null
drop proc P_Delete
go
create proc P_Delete
@id int
as
delete from UserInfo where id in (@id)
-----------------------------------修改存储过程------------------------------
if OBJECT_ID(P_Update) is not null
drop proc P_Update
go
create proc P_Update
--参数
@UserName varchar(100),
@UserPwd varchar(100),
@UserSex int,
@UserDate datetime,
@Id int,
@msg varchar(max) out
as
update UserInfo set 
UserName=@UserName,
UserPwd=@UserPwd,
UserSex=@UserSex,
UserDate=@UserDate
where Id=@Id
set @msg=修改成功
------------------------------------批删除存储过程-------------------------------------
if OBJECT_ID(P_dels) is not null
drop proc P_dels
go

create proc P_dels 
@id varchar(max)--定义变量要删除数据的id    1 ,2 , 3
as 
declare @temp table (id int)--临时表
declare @index int --下标

set @index=CHARINDEX(,,@id)--查找逗号下标
print @index
while @index>1  --判断逗号下标
begin

insert into @temp values(LEFT(@id,@index-1))--LEFT(要截取的字符串,截取个数) 字符串下标从1开始
set @id=SUBSTRING(@id,@index+1,LEN(@id)-@index)
set @index=CHARINDEX(,,@id)--查找逗号下标

end
if @id<>‘‘ --最后一个
insert into @temp values(@id)

delete from UserInfo WHERE ID in(select id from @temp)


----------------------------------查询分页------------------------------
if OBJECT_ID(P_Show) is not null
drop proc P_Show
GO
CREATE proc P_Show
@pageIndex int,
@pageSize int,
@UserName varchar(50)=null,
@totalCount int out
as

declare @sql varchar(max),--sql语句
        @sqlWhere varchar(max),--查询条件
        @rid int --序列号

---初始化变量
set @sql=‘‘
set @sqlWhere=‘‘
set @rid=(@pageIndex-1)*@pageSize
set @totalCount=(select COUNT(1) from UserInfo)

if @UserName is not null
   begin 
   set @sqlWhere= and UserName like ‘‘%+@UserName+%‘‘‘
   set @totalCount=(select COUNT(1) from UserInfo where UserName like %+@UserName+%)
end

set @sql=select top +STR(@pageSize)+ * from 
(
 select *,ROW_NUMBER() over(order by UserDate) as rid from UserInfo where 1=1 +@sqlWhere+
) temp
where rid>+STR(@rid)
exec (@sql)

   

2020-01-07

增删改查存储过程

原文:https://www.cnblogs.com/2018cjx/p/12159650.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!