增删改查存储过程

Posted ryzryz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了增删改查存储过程相关的知识,希望对你有一定的参考价值。

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
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)

以上是关于增删改查存储过程的主要内容,如果未能解决你的问题,请参考以下文章

Oracle存储过程----存储过程执行简单的增删改查

学习下oracle的存储过程增删改查操作以及java调用存储过程

sql数据库--存储过程增删改查四个语句?

有一存储过程,里面实现了增删改查,我如何在C/S界面中,事件中调用此存储过程,举例说明,求高手解答!

pymysql增删改查视图触发器事务存储过程内置函数流程控制索引理论

pymysql增删改查视图触发器事务存储过程内置函数流程控制索引理论