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

万能存储过程

时间:2018-07-30 11:14:36      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:add   put   变量   ade   删除   proc   bsp   index   database   

--分页
  create proc e_page
  (
  @pageIndex int,--当前页索引
  @pageSize int, --一页显示几条
  @allcount int output,  --一共有多少条数据
  @name  nvarchar(20)=null --模糊查询
  )
  as
  declare  @startRow int ,@endRow int   --定义两个变量 
  set @startRow=(@pageIndex-1)*@pageSize+1
  set @endRow=@startRow+@pageSize-1
  if(@name is null)
  begin
  select @allcount=(select count(*) from Student)
  select * from (select ROW_NUMBER() over(order by (Student.ID))as bid,*from Student)  as temp where temp.bid between @startRow and @endRow
  end
  else
  begin
  set @allcount=(select count(*) from Student  where Student.Name like ‘%‘+@name+‘%‘)
   select * from (select ROW_NUMBER() over(order by (Student.ID))as bid,*from Student where  Student.Name like ‘%‘+@name+‘%‘) as temp where temp.bid between @startRow and @endRow
  end

  select * from Student  where Student.Name like ‘%大%‘
  create database Stu
  use Stu
  create table Student
  (
  ID int identity,
  Name varchar(20),
  Age varchar(20),
  Crade varchar(20)
  )
  create proc e_GetList
  as
  begin
  select * from Student
  end
  --查询
  create proc e_GetLists
  @name varchar(50),
  @age varchar(50)
  as
  begin
  declare @sql varchar(500)
  set @sql = ‘select * from  Student where 1=1‘
  if(@name!=‘‘)
  set @sql+=‘and Name like‘+‘‘‘‘+‘%‘+@name+‘%‘+‘‘‘‘;
  if(@age!=‘‘)
  set @sql+=‘and Age=‘‘‘+@age+‘‘‘‘;
  exec(@sql)
  end
  exec e_GetLists ‘a‘,null
  --添加
  create proc e_Add
  (@Name varchar(20),
  @Age varchar(20),
  @Crade varchar(20)
  )
  as
  begin
  insert into Student values(@Name,@Age,@Crade)
  end
  drop proc e_Add
  --删除
  create proc e_delete
  (
 @nid varchar(50)
  )
  as
  begin
  exec(‘delete from Student where ID in (‘+@nid+‘)‘)
  end
  --返填
  create proc e_GetListById
  (
 @id int
  )
  as
  begin
  select * from Student where ID=@id
  end
  --修改
  create proc e_update
  (
 @Name varchar(20),
 @Age varchar(20),
 @Crade  varchar(20),
 @ID int
  )
  as
  begin
  update Student set Name=@Name,Age=@Age,Crade=@Crade where ID=@ID
  end

万能存储过程

标签:add   put   变量   ade   删除   proc   bsp   index   database   

原文地址:https://www.cnblogs.com/xuyuequan/p/9388643.html

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