페이지별 저장 프로세스 요약
12301 단어 저장 프로세스
/*********************************************************************/
/* MSSQL : TOP MAX/TOP MIN */
/* 2010/07/09 */
/* Leo.wl */
/*********************************************************************/
CREATE PROCEDURE [dbo].[getPagerTopMAX] (
@tblName nvarchar(2000), ---- , :"myTable"
@ID nvarchar(150), ---- , :"id"
@page int = 1, ----
@pageSize int = 10, ----
@fldName nvarchar(500) = '*', ---- , :"id, cdsinger, cdsingertype, area, cdsingerreadme"
@fldSort nvarchar(200) = @ID, ----
@Sort bit = 1, ---- ,0 ,1 ( Sort ( )-- :' SortA Asc,SortB Desc,SortC ')
@strCondition nvarchar(1000) = null, ---- , where, :"and a=1 and b=2"
@Dist bit = 0, ---- DISTINCT 0 /1
@Counts int = 1 output, ----
@pageCount int = 1 output ----
) AS begin
SET NOCOUNT ON
Declare @sqlTmp nvarchar(3000) ---- SQL
Declare @strTmp nvarchar(3000) ----
Declare @strID nvarchar(3000) ---- ID
Declare @strSortType nvarchar(10) ---- A
Declare @strFSortType nvarchar(10) ---- B
Declare @SqlSelect nvarchar(50) ---- DISTINCT SQL
Declare @SqlCounts nvarchar(50) ---- DISTINCT SQL
if @Dist = 0
begin
set @SqlSelect = 'select '
set @SqlCounts = 'Count(*)'
end
else
begin
set @SqlSelect = 'select distinct '
set @SqlCounts = 'Count(DISTINCT '+@ID+')'
end
if @Sort=0
begin
set @strFSortType=' ASC '
set @strSortType=' DESC '
end
else
begin
set @strFSortType=' DESC '
set @strSortType=' ASC '
end
-------- --------
-- @strTmp
if @strCondition is null or @strCondition='' --
begin
set @sqlTmp = @fldName + ' From ' + @tblName
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
set @strID = ' From ' + @tblName
end
else
begin
set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
end
---- -----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
declare @tmpCounts int
if @Counts = 0
set @tmpCounts = 1
else
set @tmpCounts = @Counts
--
set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
/**//** **/
if @page>@pageCount
set @page=@pageCount
--/*----- 2 -------*/
declare @pageIndex int -- /
declare @lastcount int -- %
set @pageIndex = @tmpCounts/@pageSize
set @lastcount = @tmpCounts%@pageSize
if @lastcount > 0
set @pageIndex = @pageIndex + 1
else
set @lastcount = @pagesize
--//***
if @strCondition is null or @strCondition='' --
begin
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --
begin
if @page=1
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName
+' order by '+ @fldSort +' '+ @strFSortType
else
begin
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
+' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
+' order by '+ @fldSort +' '+ @strFSortType
end
end
else
begin
set @page = @pageIndex-@page+1 --
if @page <= 1 --
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(20))+' '+ @fldName+' from '+@tblName
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
else
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
+' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
end
end
else --
begin
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --
begin
if @page=1
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName
+' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType
else
begin
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
+' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
+' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType
end
end
else
begin
set @page = @pageIndex-@page+1 --
if @page <= 1 --
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(20))+' '+ @fldName+' from '+@tblName
+' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
else
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
+' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
+' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
end
end
------ -----
--print @strTmp
exec (@strTmp)
SET NOCOUNT OFF
end
/*********************************************************************/
/* MSSQL : ROW_NUMBER() OVER */
/* 2010/07/09 */
/* Leo.wl */
/*********************************************************************/
CREATE PROCEDURE [dbo].[getPagerROWOVER]
@PageIndex int = 1,
@PageSize int = 10,
@Tables nvarchar(1000) = NULL,
@Fields nvarchar(2000) = '*',
@Where nvarchar(2000) = NULL,
@GroupBy nvarchar(2000) = NULL,
@OrderBy nvarchar(1000) = NULL,
@ReturnCount int = 0 OUTPUT
AS
BEGIN
SET NOCOUNT ON;
declare @Sql nvarchar(4000),@startRowIndex int, @maximumRows int;
declare @strWhere nvarchar(2000)
declare @strGroupBy nvarchar(2000)
set @startRowIndex = (@PageIndex - 1) * @PageSize;
set @maximumRows = @PageSize;
IF @Where IS NOT NULL AND @Where != ''
BEGIN
SET @strWhere = ' WHERE ' + @Where
END
ELSE
BEGIN
SET @strWhere = ''
END
IF @GroupBy IS NOT NULL AND @GroupBy != ''
BEGIN
SET @strGroupBy = ' GROUP BY ' + @GroupBy
END
ELSE
BEGIN
SET @strGroupBy = ''
END
set @Sql = 'select ' + @Fields + ' from (select ' + @Fields + ', ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') as rownum from ' + @Tables + @strWhere + @strGroupBy + ') as tmpTable where rownum > ' + CONVERT(nvarchar(10), @startRowIndex) + ' and rownum <= (' + CONVERT(nvarchar(10), @startRowIndex) + ' + ' + CONVERT(nvarchar(10), @maximumRows) + ')'
exec(@Sql)
print @sql
set @Sql = 'select @ReturnCount=COUNT(*) from ' + @Tables + @strWhere
exec sp_executesql @Sql,N'@ReturnCount int OUTPUT',@ReturnCount OUTPUT
print @sql
END
/*********************************************************************/
/* MSSQL : */
/* 2010/07/09 */
/* Leo.wl */
/*********************************************************************/
CREATE PROCEDURE [dbo].[Pagination_dongde_2]
(
@tab nvarchar(1000),---
@strFld nvarchar(2500), --
@strWhere varchar(8000), --where
@Page int, --
@RecsPerPage int, --
@ID VARCHAR(255), -- ID
@Sort VARCHAR(255), --
@isGetCount bit--
)
AS
DECLARE @Str nVARCHAR(4000)
declare @SQL nVARCHAR(4000) -- SQL
set @SQL='select ' + @strFld + ' from '+ @tab +' where ' +@strWhere
if(@isGetCount=0)
begin
SET @Str='SELECT TOP '+CAST(@RecsPerPage AS VARCHAR(20))+' * FROM
('+@SQL+') bb WHERE bb.'+@ID+' NOT IN (SELECT TOP '+CAST((@RecsPerPage*(@Page-1))
AS VARCHAR(20))+' '+@ID+' FROM ('+@SQL+') aa ORDER BY '+@Sort+') ORDER BY '+@Sort
PRINT @Str
end
else
begin
SET @Str='select count('+@ID+') from '+ @tab + ' where ' +@strWhere
PRINT @Str
end
EXEC sp_ExecuteSql @Str
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 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에 따라 라이센스가 부여됩니다.