第一次写存储过程,错误若干多。。。请前辈们帮忙看看。。。
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[cpage] (
@page_num int, -- 当前页数
@page_max int, -- 每页显示条数
@table varchar(50), -- 表名
@id varchar(20), -- ID字段
@shows varchar(800), -- 显示列
@sort varchar(20), -- 排序字段(推荐用ID)
@where varchar(800), -- 查询条件不推荐用like '%.. '
@order varchar(20), -- 排序方式
@record_count int OUTPUT -- 记录总数
)
AS
DECLARE @sql nvarchar(4000), @t_where varchar(800), @t_order varchar(50),
@page_count float, @mod int, @top int
IF @page_num Is Null Or @page_num < 1
SET @page_num = 1
IF @shows Is Null Or LTrim(RTrim(@where)) = ' '
SET @shows = '* '
IF @where Is Null Or LTrim(RTrim(@where)) = ' ' -- 设置条件
SET @t_where = ' '
ELSE
SET @t_where = ' WHERE ' + LTrim(RTrim(@where))
If @order = 'DESC ' -- 设置排序
SET @t_order = 'ASC '
Else
SET @t_order = 'DESC '
SET @sql = 'SELECT @record_count = COUNT(*) FROM ' + @table + @t_where
EXEC sp_executesql @sql, N '@record_count int OUTPUT ', @record_count OUTPUT -- 返回记录总数
SET @page_count = @record_count / @page_max
SET @mod = @record_count - CAst(@page_count As int) * @page_max
SET @page_count = CAst(@page_count As int) + @mod / @mod
IF @page_count > @page_num Or @mod = 0 -- 设置显示页数
SET @top = @page_max <