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

存储过程(增删改查)

时间:2020-06-18 21:33:46      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:eid   dad   arc   用户id   事务   end   creat   创建   output   

 **个人项目所建的表,可根据实际情况更改表数据

-----++++++++++++++++存储过程查询分页+++++++++++++++++++++++++++++++++++--
if(exists(select * from sys.objects where name=‘proc_UserShowPage‘))
drop proc proc_UserShowPage
go
create proc proc_UserShowPage
(
@where nvarchar(500),
@pageIndex int,
@pageSize int,
@totalCount int output
)
as
begin
declare @strcount nvarchar(500)=‘‘
declare @strsql nvarchar(500)=‘‘
declare @startRow int,@endRow int
set @startRow=(@pageIndex-1)*@pageSize
set @endRow=@startRow+@pageSize+1
set @strcount=‘select @totalCount=count(*) from
(select ROW_NUMBER() over(order by ID) as RId,* from tb_User
where 1=1 ‘+@where+‘) u join tb_User_Role ur on u.ID=ur.UserId join tb_Role r on ur.RoleID=r.ID ‘
set @strsql=‘select u.ID,u.UserId,u.LastTime,r.RoleName,u.CreateTime,u.Remark from
(select ROW_NUMBER() over(order by ID) as RId ,* from tb_User where 1=1 ‘+@where+‘) u join tb_User_Role ur on u.ID=ur.UserId join tb_Role r on ur.RoleID=r.ID
where RId > ‘+str(@startRow)+‘ and Rid< ‘+str(@endRow)+‘‘
exec(@strsql)
print(@strSql)
exec sp_executesql @strcount,N‘@totalCount int output‘,@totalCount output
end

declare @count int
exec proc_UserShowPage ‘‘,1,2,@count output
select count(*) from tb_User u join tb_User_Role ur on u.ID=ur.UserId join tb_Role r on ur.RoleID=r.ID

 

 

--**********存储过程删除**********************------------
if(exists(select * from sys.objects where name=‘proc_UserDelList‘))
drop proc proc_UserDelList
go
create proc proc_UserDelList
(
@deId varchar(500)
)
as
begin

--开始事务
begin tran
--捕捉异常
begin try
--判断是否为空
if(@deId<>‘‘)
begin
--不为空执行删除
delete from tb_user where ID in (‘‘+@deId+‘‘)
delete from tb_User_Role where UserId in (‘‘+@deId+‘‘)
end
--否则提交事务
commit tran
--结束捕捉异常
end try
begin catch
print error_message()
rollback tran
end catch
end
exec proc_UserDelList ‘dadc6c3a-30cb-407a-a375-ae20b3c76516‘

 

---------------------存储过程修改-------------------

if(exists(select * from sys.objects where name=‘proc_UserUpdate‘))
drop proc proc_UserUpdate
go
create proc proc_UserUpdate
(
@tb_UId varchar(200),
@UserId varchar(200),
@lastTime datetime,
@createTime datetime,
@remark varchar(500),
@RId varchar(500)
)
as
begin
--通过用户表的id对用户id名称,最后一次登录时间,创建时间以及备注进行修改
update tb_User set UserId=@UserId,LastTime=@lastTime,CreateTime=@createTime,remark=@remark where ID=@tb_UId
--通过用户表的id对用户角色表中的角色进行修改
update tb_User_Role set RoleID=@RId where ID=@tb_UId
end


--//////////////////////////////////////////////////////////////////////////////////////--


---------------存储过程角色查询------------

create proc proc_RoleShowPage
(
@pageIndex int,
@pageSize int,
@where nvarchar(500),
@totalCount int output
)
as
begin
declare @strcount nvarchar(500)=‘‘
declare @strsql nvarchar(500)=‘‘
declare @startRow int,@endRow int
set @startRow=(@pageIndex-1)*@pageSize
set @endRow=@pageIndex+@pageSize+1
set @strcount=‘select @totalCount=count(*) from tb_Role ‘
if(@pageSize>0)
begin
set @strsql=‘select * from
(select ROW_NUMBER() over(order by ID) as RId ,* from tb_Role where 1=1 ‘+@where+‘) r where RId > ‘+str(@startRow)+‘ and Rid< ‘+str(@endRow)+‘‘
end
else
begin
set @strsql=‘select * from (select ROW_NUMBER() over(order by ID) as RId ,* from tb_Role where 1=1 ‘+@where+‘) r ‘
end
exec(@strsql)
print(@strSql)
exec sp_executesql @strcount,N‘@totalCount int output‘,@totalCount output
end
--执行
declare @count int
exec proc_RoleShowPage 0,0,‘‘,@count output
select @count

 

----------------------存储过程用户添加---------------------------------------
create proc proc_UserAdd
(
@UserId varchar(200),
@lastTime datetime,
@createTime datetime,
@remark varchar(500),
@RId varchar(500)
)
as
begin
DECLARE @tb_uId varchar(50)
begin tran--开始事务
begin try
--判断用户不存在
if(not exists(select * from tb_User where UserId=@UserId))
begin
--对用户id名称,最后一次登录时间,创建时间以及备注进行添加
insert into tb_User (UserId,LastTime,CreateTime,Remark) values(@UserId,@lastTime,@createTime,@remark)
--通过用户id查询出对应的数据id
select @tb_uId=ID from tb_User where UserId=@UserId
insert into tb_User_Role(UserId,RoleID) values(@tb_uId ,@RId)
end
--提交事务
commit tran
end try--结束
begin catch
print error_message()
rollback tran --回滚事务
end catch
end

 

---自行修改表名数据

存储过程(增删改查)

标签:eid   dad   arc   用户id   事务   end   creat   创建   output   

原文地址:https://www.cnblogs.com/xr0818/p/13159538.html

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