Several good database paging procedures

Source: Internet
Author: User

Author: MaverickBlog:Http://blog.csdn.net/zhaohuabing reprinted please indicate the source


Oracle

Create or replace procedure page (
P_num integer,
P_size integer,
Condition clob,
Table_name varchar,
P_rowset out sys_refcursor)
As
Begin
Open p_rowset
'Select *
From (
Select rownum R, T1 .*
From (
Select '| table_name |'. * from' | table_name |''
| Condition | 'order by happentime DESC) T1
Where rownum <= '| p_size * p_num |') T2
Where t2.r> '| p_size * (p_num-1 );
End page;

Ii. MSSQL
Create procedure page
@ P_num int,
@ P_size int,
@ Condition text,
@ Table_name nvarchar (100 ),
@ Current_page_size int
As
If @ p_num = 1
Execute ('select top '+ @ p_size +' * from '+ @ table_name + ''+ @ condition + 'order by happentime DESC ')
Else
If @ current_page_size! = @ P_size
Execute ('select * from (
Select top '+ @ current_page_size +' * from' + @ table_name + ''+ @ condition + 'order by happentime
) As t order by happentime DESC ')
Else
Begin
Declare @ TMP int
Set @ TMP = @ p_size * @ p_num
Execute ('select * from (
Select top '+ @ p_size +' * from (
Select top '+ @ TMP +' * from '+ @ table_name + ''+ @ condition + 'order by happentime DESC
) As T1 order by happentime
) As T2 order by happentime DESC ')
End
Go

Sybase 3
Create procedure page
@ P_num int,
@ P_size int,
@ Condition nvarchar (3000 ),
@ Table_name nvarchar (100 ),
@ Current_p_size int
As
Declare @ str_p_size varchar (20 ),
@ Str_tmp varchar (20 ),
@ Str_current_p_size varchar (20 ),
@ I _rowcount int
Begin
Select @ str_tmp = cast (@ p_size * @ p_num as varchar (20 ))
Select @ str_p_size = cast (@ p_size as varchar (20 ))
Select @ str_current_p_size = cast (@ current_p_size as varchar (20 ))
 
If @ p_num = 1
Begin
Set @ I _rowcount = @ p_size * @ p_num
Set rowcount @ I _rowcount
Execute ('select * from' + @ table_name + ''+ @ condition + 'order by happentime DESC ')
End
Else
If @ current_p_size! = @ P_size
Begin
Set rowcount @ current_p_size
Execute ('select * into # temp from '+ @ table_name + ''+ @ condition + 'order by happentime
Select * from # temp order by happentime DESC ')
End
Else
Begin
Set @ I _rowcount = @ p_size * @ p_num
Set rowcount @ I _rowcount
Execute ('select * into # temp1 from '+ @ table_name + ''+ @ condition + 'order by happentime DESC
Select top '+ @ str_p_size +' * into # temp2 from # temp1 order by happentime
Select * from # temp2 order by happentime DESC ')
End
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.