페이지별 저장 프로세스 (1): 페이지별로 한 테이블의 데이터를 얻습니다. (검증되지 않았습니다.)
22220 단어 저장 프로세스
-------------------------------------------------------------------------
-- (Name):GetRecordFromSingleTableByPage
-- (Function):
-- (Author):
-- (Date):2006 02 15
-- :
/*
@tblName nvarchar(255), --
@priKeyName nvarchar(50), --
@fldNames nvarchar(1000), -- ,
@PageSize int = 0, -- ,0
@PageIndex int = 1, -- , 1
@OrderType nvarchar(200) = '', -- ,'': 0: 1: else:
@strWhere nvarchar(2000) = '', -- ( : where)
*/
-- :
-- @TotalItem int output, --
-- @TotalPage int output --
-- :
-------------------------------------------------------------------------
-- :
-- :
-- :
-------------------------------------------------------------------------
ALTER PROCEDURE [dbo].[GetRecordFromSingleTableByPage]
@tblName nvarchar(255), --
@priKeyName nvarchar(50), --
@fldNames nvarchar(1000), -- ,
@PageSize int = 0, --
@PageIndex int = 1 output, --
@OrderType nvarchar(200) = '', -- ,'': 0: 1: :
@strWhere nvarchar(2000) = '', -- ( : where)
@TotalItem int output,
@TotalPage int output
AS
declare @strByPage nvarchar(4000) --
declare @strNoPage nvarchar(1500) -- ,
declare @strTotal nvarchar(1000) --
declare @strNonResult nvarchar(1500) --
declare @strTmp nvarchar(100)
declare @strOrder nvarchar(200)
if @OrderType is null or @OrderType=''
begin
set @strOrder = ''
set @strTmp=''
end
else if @OrderType = '0' --
begin
set @strTmp = @priKeyName + '>(select max([' + @priKeyName + ']) from '
set @strOrder = ' order by ' + @priKeyName + ' asc'
end
else if @OrderType = '1' --
begin
set @strTmp = @priKeyName + '<(select min([' + @priKeyName + ']) from '
set @strOrder = ' order by ' + @priKeyName + ' desc'
end
else --
begin
set @strTmp = ''
set @strOrder = ' order by ' + @OrderType
end
set @strNonResult = 'select ' + @fldNames + ' from ' + @tblName + ' where 1=2'
if @strWhere is null or @strWhere = '' --
begin
set @strTotal = N'select @TotalItem = count(*) from ' + @tblname
set @strNoPage = N'select ' + @fldNames + ' from ' + @tblName + @strOrder
end
else
begin
set @strTotal = N'select @TotalItem = count(*) from ' + @tblname + ' where ' + @strWhere
set @strNoPage = N'select ' + @fldNames + ' from ' + @tblName + ' where ' + @strWhere + @strOrder
end
--
exec sp_executeSql @strTotal,N'@TotalItem int output',@TotalItem output
-- ,
if @TotalItem = 0
begin
set @TotalPage = 0
exec sp_executeSql @strNonResult
return 0
end
-- ,
if @PageSize = 0 --
begin
set nocount off
set @TotalPage = 0
exec sp_executeSql @strNoPage
return
end
else --
begin
set nocount on
-- , , 1
set @TotalPage=CEILING(cast(@TotalItem as float)/@PageSize)
if(@PageIndex>@TotalPage)
set @PageIndex=@TotalPage
if(@PageIndex <1)
set @PageIndex=1
if @PageIndex =1 --
begin
if @strWhere=''
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldNames + ' from ' + @tblName + @strOrder
else
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldNames + ' from ' + @tblName + ' where ' + @strWhere + @strOrder
end
else --
begin
if (@OrderType='0' or @OrderType='1') --
begin
if @strWhere=''
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames
+ ' from ' + @tblName
+ ' where ' + @strTmp
+ ' (select top ' + cast((@PageIndex-1) * @PageSize as varchar) + ' ' + @priKeyName
+ ' from ' + @tblName + @strOrder + ') as tmptbl)'
+ @strOrder
else
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames
+ ' from ' + @tblName
+ ' where ' + @strTmp
+ ' (select top '+ cast((@PageIndex-1) * @PageSize as varchar) + ' ' + @priKeyName
+ ' from ' + @tblName + ' where ' + @strWhere + @strOrder + ') as tmptbl)'
+ ' and ' + @strWhere
+ @strOrder
end
else --
begin
if @strWhere=''
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames
+ ' from ' + @tblName
+ ' where not exists (select * from '
+ ' (select top ' + cast((@PageIndex-1) * @PageSize as varchar) + ' * from '
+ @tblName + @strorder + ') as tmpTable '
+ ' where tmpTable.' + @priKeyName + ' = ' + @tblName +'.' + @priKeyName + ')'
+ @strorder
else
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames
+ ' from ' + @tblName
+ ' where not exists (select * from '
+ ' (select top ' + cast((@PageIndex-1) * @PageSize as varchar) + ' * from '
+ @tblName + ' where ' + @strWhere + @strorder + ') as tmpTable '
+ ' where tmpTable.' + @priKeyName + ' = ' + @tblName + '.' + @priKeyName + ')'
+ ' and ' + @strWhere
+ @strorder
end
end
end
--print @strByPage
exec sp_executeSql @strByPage
return
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 호출 Oracle 스토리지 프로세스 상세 정보Java 호출 Oracle 스토리지 프로세스 상세 정보 단계: 1. Oracle 스토리지 프로세스 작성 2. 데이터베이스 작성 연결 도구 클래스 얻기 3. 간단한 응용 프로그램 호출 저장 프로세스 작성 구현: 1. ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.