增删查改通用存储过程

create database Exam001 --数据库名 use Exam001 --使用数据库 create table Student --创建表 ( StuID      int identity,--学生ID StuName    varchar(50),--学生姓名 StuSex     bit,--学生性别 StuAge     int ,--学生年龄 StuKTime   datetime,--入学时间 StuJTime   datetime,--结束时间 StuCLassID int--班级 ) create table StuClass ( CLASSID    int, CLASSNAME  varchar(50), ) select * from Student

--增存储过程 if exists(select * from sysobjects where name='u_add') drop proc u_add go create PROC u_add

@StuName    varchar(50),--学生姓名 @StuSex     bit,--学生性别 @StuAge     int ,--学生年龄 @StuKTime   time,--入学时间 @StuJTime   time,--结束时间 @StuCLassID int--班级 as insert into  Student values(@StuName,@StuSex,@StuAge,@StuKTime,@StuJTime,@StuCLassID) go --删除存储过程 if exists (select * from sysobjects where name='u_del') drop proc u_del go create proc u_del @StuID int as delete from Student where StuID=@StuID go --修改存储过程 if exists (select * from sysobjects where name='u_Upd') drop proc u_Upd go create proc u_Upd

@StuID      int, @StuName    varchar(50),--学生姓名 @StuSex     bit,--学生性别 @StuAge     int ,--学生年龄 @StuKTime   time,--入学时间 @StuJTime   time,--结束时间 @StuCLassID int--班级 as update Student set StuName=@StuName ,StuSex=@StuSex ,StuAge =@StuAge ,StuKTime =@StuKTime ,StuJTime =@StuJTime ,StuCLassID=@StuCLassID where StuID=@StuID 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) = 'id',      --标识列名(如:比如主键、标识,推荐使用索引列) @pageSize int = 10,                --页面大小 @pageIndex int = 0,                --当前页 @orderCol varchar(100) = 'id 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

扫描二维码关注公众号,回复: 2446618 查看本文章

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 '+@TableName+' where ID in ( select ID from #DelID)'    exec(@strSql) end

GO

猜你喜欢

转载自www.cnblogs.com/fsl123/p/9387505.html