create procedure main_table_pwqzc
(@pagesize int,
@pageindex int,
@docount bit,
@this_id)
asif(@docount=1)
begin
select count(id) from luntan where this_id=@this_id
endelse
begin
declare @indextable table(id int identity(1,1),nid int)
declare @pagelowerbound int
declare @pageupperbound int
set @pagelowerbound=(@pageindex-1)*@pagesize
set @pageupperbound=@pagelowerbound+@pagesize
set rowcount @pageupperbound
insert into @indextable(nid) select id from luntan where this_id=@this_id order by reply_time desc
select a.* from luntan a,@indextable t where a.id=t.nid
and t.id>@pagelowerbound and t.id<=@pageupperbound order by t.id
endgo
儲存過程會根據傳入的引數@docount來確定是不是要返回所有要分頁的記錄總數
特別是這兩行
set rowcount @pageupperbound
insert into @indextable(nid) select id from luntan where this_id=@this_id order by reply_time desc
真的是妙不可言!!set rowcount @pageupperbound當記錄數達到@pageupperbound時就會停止處理查詢
,select id 只把id列取出放到臨時表裡,select a.* from luntan a,@indextable t where a.id=t.nid
and t.id>@pagelowerbound and t.id<=@pageupperbound order by t.id
而這句也只從表中取出所需要的記錄,而不是所有的記錄,結合起來,極大的提高了效率!!
妙啊,真的妙!!!!
create procedure paging_rowcount
(@tables varchar(1000),
@pk varchar(100),
@sort varchar(200) = null,
@pagenumber int = 1,
@pagesize int = 10,
@fields varchar(1000) = '*',
@filter varchar(1000) = null,
@group varchar(1000) = null)
as/*default sorting*/
if @sort is null or @sort = ''
set @sort = @pk
/*find the @pk type*/
declare @sorttable varchar(100)
declare @sortname varchar(100)
declare @strsortcolumn varchar(200)
declare @operator char(2)
declare @type varchar(100)
declare @prec int
/*set sorting variables.*/
if charindex('desc',@sort)>0
begin
set @strsortcolumn = replace(@sort, 'desc', '')
set @operator = '<='
endelse
begin
if charindex('asc', @sort) = 0
set @strsortcolumn = replace(@sort, 'asc', '')
set @operator = '>='
endif charindex('.', @strsortcolumn) > 0
begin
set @sorttable = substring(@strsortcolumn, 0, charindex('.',@strsortcolumn))
set @sortname = substring(@strsortcolumn, charindex('.',@strsortcolumn) + 1, len(@strsortcolumn))
endelse
begin
set @sorttable = @tables
set @sortname = @strsortcolumn
endselect @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 @str******filter varchar(1000)
declare @strgroup varchar(1000)
/*default page number*/
if @pagenumber < 1
set @pagenumber = 1
/*set paging variables.*/
set @strpagesize = cast(@pagesize as varchar(50))
set @strstartrow = cast(((@pagenumber - 1)*@pagesize + 1) as varchar(50))
/*set filter & group variables.*/
if @filter is not null and @filter != ''
begin
set @strfilter = ' where ' + @filter + ' '
set @str******filter = ' and ' + @filter + ' '
endelse
begin
set @str******filter = ''
set @strfilter = ''
endif @group is not null and @group != ''
set @strgroup = ' group by ' + @group + ' '
else
set @strgroup = ''
/*execute dynamic query*/
exec(
'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 ' + @str******filter + ' ' + @strgroup + ' order by ' + @sort + '')
go
精彩的近乎完美的分頁儲存過程
create procedure main table pwqzc pagesize int,pageindex int,docount bit,this id asif docount 1 begin select count id from luntan where this id this i...
精彩的近乎完美的分頁儲存過程
create procedure main table pwqzc pagesize int,pageindex int,docount bit,this id asif docount 1 begin select count id from luntan where this id this i...
精彩的近乎完美的分頁儲存過程
create procedure main table pwqzc pagesize int,pageindex int,docount bit,this id asif docount 1 begin select count id from luntan where this id this i...
一種近乎完美的加密演算法 擴散加密演算法
引言 在開始介紹之前,請讓我們來看幾個問題。有沒有一種對稱加密演算法能夠支援任意長度密匙加密呢?有沒有一種加密演算法能夠同時支援對稱和非對稱加密呢?有麼有一種加密演算法在實現完全擴散時所需要的步驟最少呢?這樣的演算法一定是存在的!擴散加密演算法是以擴散演算法為基礎,在處理的過程中加入密匙從而實現加密...
近乎完美的社會工程毀於一個小小的失誤
這封社會工程郵件近乎完美,但一個小小的破綻導致整個社工嘗試失敗。美國一位最近分享了一封寄電子郵件,看到這封郵件的所有人都認為這是一次近乎完美的社會工程嘗試。不過,資訊保安意識培訓加上管理層對任何可疑要求進行詢問的全力支援,成功避免了該攻擊可能造成的鉅額財務損失。那封電子郵件一眼看去似乎是從該公司執行...