存储过程
Posted yanyuhao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了存储过程相关的知识,希望对你有一定的参考价值。
--增存储过程
if exists(select * from sysobjects where name=‘UP_ADDSTUDENT‘)
drop proc UP_ADDSTUDENT
go
create PROC UP_ADDSTUDENT
@StuName varchar(50),--学生姓名
@StuSex bit,--学生性别
@StuAge int ,--学生年龄
@StuKTime datetime,--入学时间
@StuJTime datetime,--结束时间
@StuCLassID int--班级
as
insert into Student values(@StuName,@StuSex,@StuAge,@StuKTime,@StuJTime,@StuCLassID)
go
--删除存储过程
if exists (select * from sysobjects where name=‘UP_DELETESTUDENT‘)
drop proc UP_DELETESTUDENT
go
create proc UP_DELETESTUDENT
@StuID int
as
delete from Student where [email protected]
go
--修改存储过程
if exists (select * from sysobjects where name=‘UP_UPDATESTUDENT‘)
drop proc UP_UPDATESTUDENT
go
create proc UP_UPDATESTUDENT
@StuID int,
@StuName varchar(50),--学生姓名
@StuSex bit,--学生性别
@StuAge int ,--学生年龄
@StuKTime datetime,--入学时间
@StuJTime datetime,--结束时间
@StuCLassID int--班级
as
update Student set [email protected] ,[email protected] ,StuAge [email protected] ,StuKTime [email protected] ,StuJTime [email protected] ,[email protected] where [email protected]
go
/****** Object: StoredProcedure [dbo].[p_paging] Script Date: 07/14/2018 11:18:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[p_paging]
@tableName varchar(8000), --表名、视图名
@indexCol varchar(50) = ‘StuID‘, --标识列名(如:比如主键、标识,推荐使用索引列)
@pageSize int = 10, --页面大小
@pageIndex int = 0, --当前页
@orderCol varchar(100) = ‘StuID desc‘,--排序 (如:id)
@where varchar(max) = ‘‘, --条件
@columns varchar(500) = ‘*‘ --要显示的列
as
declare @sql varchar(max)
declare @sql2 varchar(max)
declare @where2 varchar(max)
if @where <> ‘‘
begin
select @where2 = ‘ And ‘ + @where
select @where = ‘ Where ‘ + @where
end
else
select @where2 = ‘‘
select @sql = ‘Select Top ‘ + Convert(varchar(10),@pageSize) + ‘ ‘ + @columns + ‘ From ‘ + @tableName
select @sql2 = @sql + @where
select @sql = @sql + ‘ Where ‘ + ‘(‘ + @indexCol + ‘ Not In (Select Top ‘ + Convert(varchar(10), @pageSize * @pageIndex) + ‘ ‘ + @indexCol + ‘ From ‘ + @tableName + @where + ‘ Order by ‘+ @orderCol +‘))‘
select @sql = @sql + @where2
select @sql = @sql + ‘ Order by ‘ + @orderCol
--获取数据集
exec (@sql)
PRINT @sql
select @sql2 = Replace(@sql2,‘Top ‘ + Convert(varchar(10), @pageSize) + ‘ ‘ + @columns, ‘count(1)‘)
--获取总数据条数
exec(@sql2)
GO
/****** Object: StoredProcedure [dbo].[proc_Delete] Script Date: 07/27/2018 19:36:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[proc_Delete]
@TableName varchar(50),
@Id varchar(5000)
as
begin
declare @strSql varchar(5000)
declare @sql varchar(4000)
set @sql=‘select col=‘‘‘+ replace(@Id,‘,‘,‘‘‘ union all select ‘‘‘)+‘‘‘‘
if OBJECT_ID(‘tempdb..#DelID‘) is not null
drop table #DelID
create table #DelID(ID VARCHAR(36))
insert into #DelID exec(@sql)
set @strSql = ‘delete from ‘[email protected]+‘ where ID in ( select ID from #DelID)‘
exec(@strSql)
end
GO
if exists(select * from sysobjects where name=‘UP_FANTIAN‘)
drop proc UP_FANTIAN
go
create proc UP_FANTIAN
@StuID int
as
select * from Student where [email protected]
go
以上是关于存储过程的主要内容,如果未能解决你的问题,请参考以下文章