ptmvc存储过程

--显示---
create proc up_show
@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)

 

--添加
ALTER proc up_add
(
@Name varchar(50),
@Age varchar(50),
@Sex varchar(20),
@StatId int
)
as
begin
insert into Stu values(@Name,@Age,@Sex,@StatId)
end

---修改--
ALTER proc up_upt
(
@ID int,
@Name varchar(50),
@Age varchar(50),
@Sex varchar(20),
@StatId int
)
as
begin
update Stu set Name=@Name,Age=@Age,Sex=@Sex,StatId=@StatId where ID=@ID
end

----删除
ALTER proc up_del
(
@ID int
)
as
delete from Stu where ID=@ID

posted @ 2018-07-29 20:55  真是善良  阅读(131)  评论(0编辑  收藏  举报