sqlserver2000 stored procedure paging-only one table

Source: Internet
Author: User
Tags rowcount

Search from the Internet, the use of cursors for the stored procedure paging, but there is a problem, there are two tables, the first table is empty, and the second table is the real data.

The stored procedure is now modified so that it only generates a single table.

CREATE PROCEDURE [dbo]. [Pr_querybypage]
@sqlstr nvarchar (4000),--Query SQL
@currentpage int,--page record number
@pagesize int,--Show records per page
@tablename varchar (100)--table name
As
SET NOCOUNT ON
declare @P1 int,--P1 is the ID of the cursor
@rowcount int,--
@sql varchar (100)--Dynamic SQL statement
--a stupid method that uses a temporary table to receive an empty result set
Set @sql = ' SELECT top 0 * into # #tmpTable from ' [email protected]
EXEC (@sql)
INSERT INTO # #tmpTable exec sp_cursoropen @P1 output, @sqlstr, @scrollopt =1, @ccopt =1,@[email protected] Output
--select Ceiling (1.0* @rowcount/@pagesize) as Totalpage, @rowcount as [rowcount]
Set @currentpage = (@currentpage-1) * @pagesize +1
exec sp_cursorfetch @P1, @currentpage, @pagesize
EXEC sp_cursorclose @P1
Drop Table # #tmpTable
SET NOCOUNT OFF
GO

Pass in 4 parameters, SQL statement, page number, data per page, table name.

The following SQL statements can be used in Query Analyzer:

EXEC pr_querybypage ' select * FROM table1 ORDER by id ', 1,10, ' table1 '

C # calls:

To perform a paged stored procedure, note that status= ' 0 ' is a front and back two single quotes, not double quotes.
String sql = "Exec pr_querybypage ' select * FROM table1 a where status= ' 0";
if (Baodanhao! = "")
{
SQL + = "and A.baodanhao like"% "+ Baodanhao +"% "";
}
SQL + = "ORDER by id", "+page+", "+pagesize+", ' table1 ' ";
Oledbhelper = new Oledbhelper ();
return oledbhelper.executequeryfy (SQL);

sqlserver2000 stored procedure paging-only one table

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.