SQL2005 페이지 나누기 스토리지 프로세스

12688 단어 sql2005
   CREATE PROCEDURE [dbo].[GetRecordFromPage] 
@SelectList VARCHAR(2000), --
@TableSource VARCHAR(100), --
@SearchCondition VARCHAR(2000), --
@OrderExpression VARCHAR(1000), --
@PageIndex INT = 1, -- , 0
@PageSize INT = 10 --
AS
BEGIN
IF @SelectList IS NULL OR LTRIM(RTRIM(@SelectList)) = ''
BEGIN
SET @SelectList = '*'
END
PRINT @SelectList

SET @SearchCondition = ISNULL(@SearchCondition,'')
SET @SearchCondition = LTRIM(RTRIM(@SearchCondition))
IF @SearchCondition <> ''
BEGIN
IF UPPER(SUBSTRING(@SearchCondition,1,5)) <> 'WHERE'
BEGIN
SET @SearchCondition = 'WHERE ' + @SearchCondition
END
END
PRINT @SearchCondition

SET @OrderExpression = ISNULL(@OrderExpression,'')
SET @OrderExpression = LTRIM(RTRIM(@OrderExpression))
IF @OrderExpression <> ''
BEGIN
IF UPPER(SUBSTRING(@OrderExpression,1,5)) <> 'WHERE'
BEGIN
SET @OrderExpression = 'ORDER BY ' + @OrderExpression
END
END
PRINT @OrderExpression

IF @PageIndex IS NULL OR @PageIndex < 1
BEGIN
SET @PageIndex = 1
END
PRINT @PageIndex
IF @PageSize IS NULL OR @PageSize < 1
BEGIN
SET @PageSize = 10
END
PRINT @PageSize

DECLARE @SqlQuery VARCHAR(4000)

SET @SqlQuery='SELECT '+@SelectList+',RowNumber
FROM
(SELECT ' + @SelectList + ',ROW_NUMBER() OVER( '+ @OrderExpression +') AS RowNumber
FROM '+@TableSource+' '+ @SearchCondition +') AS RowNumberTableSource
WHERE RowNumber BETWEEN ' + CAST(((@PageIndex - 1)* @PageSize+1) AS VARCHAR)
+ ' AND ' +
CAST((@PageIndex * @PageSize) AS VARCHAR)
-- ORDER BY ' + @OrderExpression
PRINT @SqlQuery
SET NOCOUNT ON
EXECUTE(@SqlQuery)
SET NOCOUNT OFF

RETURN @@RowCount
END

 
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go






ALTER PROCEDURE [dbo].[PROCE_SQL2005PAGECHANGE]
(
@TableName varchar(50), --
@ReFieldsStr varchar(200) = '*', -- ( *)
@OrderString varchar(200), -- ( ! order by)
@WhereString varchar(500) =N'', -- ( where)
@PageSize int, --
@PageIndex int = 1 , --
@TotalRecord int output --
)
AS

BEGIN

--
Declare @StartRecord int;
Declare @EndRecord int;
Declare @TotalCountSql nvarchar(500);
Declare @SqlString nvarchar(2000);
set @StartRecord = (@PageIndex-1)*@PageSize + 1
set @EndRecord = @StartRecord + @PageSize - 1
SET @TotalCountSql= N'select @TotalRecord = count(*) from ' + @TableName;--
SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--
--
IF (@WhereString! = '' or @WhereString!=null)
BEGIN
SET @TotalCountSql=@TotalCountSql + ' where '+ @WhereString;
SET @SqlString =@SqlString+ ' where '+ @WhereString;
END
--
--IF(@TotalRecord is null)
-- BEGIN
EXEC sp_executesql @totalCountSql,N'@TotalRecord int out',@TotalRecord output;--
-- END
----
set @SqlString ='select * from ' + @SqlString + ') as t where rowId between ' + ltrim(str(@StartRecord)) + ' and ' + ltrim(str(@EndRecord));
Exec(@SqlString)
END

 
 
LTRIM(SQL Server Compact)은 선행 공백을 제거한 후 문자 표현식을 반환합니다.
RTRIM(Transact-SQL)은 문자 변수의 후행 공백을 제거합니다.
ISNULL(check expression, replacement value)
check_expression이 NULL이 아니면 값을 반환합니다.그렇지 않으면
replacement_value 암시적 변환
check_expression의 종류 (만약 이 두 종류가 다르다면) 는 전자를 되돌려줍니다.

좋은 웹페이지 즐겨찾기