비교적 좋은 페이지 저장 프로세스

4353 단어 저장 프로세스
set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

go





--drop procedure p_page 

--go 



ALTER procedure [dbo].[CutPage_PRO] 



( 

@Tables varchar(1000), --   testtable (          as ,       table1 left join table2)

@PrimaryKey varchar(100),--    ,      (            :table1.id)

@Sort varchar(200) = NULL,--     f_Name asc f_name desc(           )(          : table1.id) 

@CurrentPage int = 1,--    

@PageSize int = 10,---     

@Fields varchar(1000) = '*',--        (       .    :table1.name,table1.age,table2.lvl,table2.gender)

@Filter varchar(1000) = NULL,--    ,  where,  f_id>3 

@Group varchar(1000) = NULL,--     

@TotalPage int output --      

) 



--WITH ENCRYPTION ---     



AS 







SET NOCOUNT ON 

Declare @intResult Int 



Begin Tran 



DECLARE @sql nvarchar(4000) 



if @Filter is null or @Filter='' 

set @Sql = 'select @intResult = count(' + @PrimaryKey + ') from ' + @Tables 

else 

set @Sql = 'select @intResult = count(' + @PrimaryKey + ') from ' + @Tables + ' where + ' + @Filter 



EXEC sp_executesql @sql,N'@intResult int OUTPUT',@intResult OUTPUT--       

select @TotalPage=CEILING((@intResult+0.0)/@PageSize)--      





IF @Sort IS NULL or @Sort = '' 

SET @Sort = @PrimaryKey 



DECLARE @SortTable varchar(100) 

DECLARE @SortName varchar(100) 

DECLARE @strSortColumn varchar(200) 

DECLARE @operator char(2) 

DECLARE @type varchar(100) 

DECLARE @prec int 



IF CHARINDEX('DESC',@Sort)>0 

BEGIN 

SET @strSortColumn = REPLACE(@Sort, 'DESC', '') 

SET @operator = '<=' 

END 

ELSE 

IF CHARINDEX('ASC', @Sort) > 0 

BEGIN 

SET @strSortColumn = REPLACE(@Sort, 'ASC', '') 

SET @operator = '>=' 

END 



ELSE 

BEGIN 

SET @strSortColumn = @SORT 

SET @operator = '>=' 

END 



IF CHARINDEX('.', @strSortColumn) > 0 

BEGIN 

SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn)) 

SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn)) 

END 

ELSE 

BEGIN 

SET @SortTable = @Tables 

SET @SortName = @strSortColumn 

END 



Select @type=t.name, @prec=c.prec 

FROM sysobjects o 

JOIN syscolumns c on o.id=c.id 

JOIN systypes t on c.xusertype=t.xusertype 

Where o.name = @SortTable AND c.name = @SortName 



IF CHARINDEX('char', @type) > 0 

SET @type = @type + '(' + CAST(@prec AS varchar) + ')' 



DECLARE @strPageSize varchar(50) 

DECLARE @strStartRow varchar(50) 

DECLARE @strFilter varchar(1000) 

DECLARE @strSimpleFilter varchar(1000) 

DECLARE @strGroup varchar(1000) 



IF @CurrentPage < 1 

SET @CurrentPage = 1 



SET @strPageSize = CAST(@PageSize AS varchar(50)) 

SET @strStartRow = CAST(((@CurrentPage - 1)*@PageSize + 1) AS varchar(50)) 



IF @Filter IS NOT NULL AND @Filter != '' 

BEGIN 

SET @strFilter = ' Where ' + @Filter + ' ' 

SET @strSimpleFilter = ' AND ' + @Filter + ' ' 

END 

ELSE 

BEGIN 

SET @strSimpleFilter = '' 

SET @strFilter = '' 

END 



IF @Group IS NOT NULL AND @Group != '' 

SET @strGroup = ' GROUP BY ' + @Group + ' ' 

ELSE 

SET @strGroup = '' 



set @sql = 'DECLARE @SortColumn ' + @type + ' 

SET ROWCOUNT ' + @strStartRow + ' 

Select @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' orDER BY ' + @Sort + ' 

SET ROWCOUNT ' + @strPageSize + ' 

Select ' + @Fields + ' FROM ' + @Tables + ' Where ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' orDER BY ' + @Sort + ' '



--print @sql 



exec(@sql) 





If @@Error <> 0 

Begin 

RollBack Tran 

Return -1 

End 

Else 

Begin 

Commit Tran 

Return @intResult ---       

End 



---------------------------eg-------------

/*

Declare @@intResult Int 

exec CutPage_PRO 

' TreavlPlan left join Book on Book.id=TreavlPlan.planid ',

'TreavlPlan.id',

'TreavlPlan.id desc',

2,

2,

'*',

'TreavlPlan.userid=1',

'',

@@intResult

*/





좋은 웹페이지 즐겨찾기