데이터베이스 페이지 저장 프로 세 스 코드
2063 단어 데이터베이스 페이지 저장 프로 세 스 코드
/*
,
:
@tablename:
@tablefield: , ,
@where: , "1=1"
@orderby: , order by id desc
@fieldlist: , userid, username
@curpage:
@page_record:
@Sort: ( , desc, , asc, orderby )
: tablename where curpage page_record , orderby
*/
CREATE PROCEDURE proc_CommonPaging
@tablename varchar(100),
@tablefield varchar(20),
@where varchar(5000),
@orderby varchar(500),
@fieldlist varchar(1000),
@curpage int,
@page_record int,
@sort varchar(8)
AS
BEGIN
DECLARE @cmd varchar(8000)
DECLARE @uprecord int
DECLARE @Op varchar(2) --
DECLARE @max_min varchar(4) -- /
SET @op = '<'
SET @max_min = 'MIN'
IF @sort = 'asc'
BEGIN
SET @Op = '>'
SET @max_min = 'MAX'
END
SET @uprecord=@curpage * @page_record
IF @curpage = 0
SET @cmd = 'SELECT TOP '+cast(@page_record AS NVARCHAR)+' '+@fieldlist+' FROM '+@tablename+' WHERE '+@where+' '+@orderby
ELSE
SET @cmd = 'SELECT TOP '+cast(@page_record AS NVARCHAR)+' '+@fieldlist+' FROM '+@tablename+' WHERE '+@where+' AND '+@tablefield+'
'+@op+' (SELECT '+@max_min+'('+@tablefield+') FROM (SELECT TOP '+cast(@uprecord AS NVARCHAR)+' '+@tablefield+' FROM '+@tablename+' WHERE
'+@where+' '+@orderby+') AS TmpTbl ) AND '+@where+' '+@orderby
SET @cmd = @cmd + '; SELECT COUNT(*) FROM '+@tablename+' WHERE '+@where
EXEC(@cmd)
PRINT(@cmd)
END
GO