码迷,mamicode.com
首页 > 其他好文 > 详细

存储过程添加显示修改查询

时间:2020-09-02 18:13:27      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:upd   end   nbsp   arc   use   creat   修改查询   rdate   substr   

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)

 

存储过程添加显示修改查询

标签:upd   end   nbsp   arc   use   creat   修改查询   rdate   substr   

原文地址:https://www.cnblogs.com/lgc200421/p/13544361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!