真正的分页存储过程,借鉴了CSDN上众多力量,除BUG版
2007-07-12 13:03
375 查看
前段时间研究分页的时候,在CSDN的BLOG上看到了一位兄弟写的分页存储过程,发现非常好,于是,就使用了这个存储过程,下面是原版的分页存储过程
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--开始
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE PROCEDURE GetRecordFromPage
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@tblName varchar(255), -- 表名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@fldName varchar(255), -- 字段名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageSize int = 10, -- 页尺寸
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageIndex int = 1, -- 页码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@IsCount bit = 0, -- 返回记录总数, 非 0 值则返回
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AS
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strSQL varchar(1000) -- 主语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strTmp varchar(300) -- 临时变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strOrder varchar(400) -- 排序类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if @OrderType != 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
begin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strTmp = "<(select min"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strOrder = " order by [" + @fldName +"] desc"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
end
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
begin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strTmp = ">(select max"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strOrder = " order by [" + @fldName +"] asc"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
end
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strSQL = "select top " + str(@PageSize) + " * from ["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @strOrder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if @strWhere != ''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strSQL = "select top " + str(@PageSize) + " * from ["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @fldName + "] from [" + @tblName + "] where " + @strWhere + " "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if @PageIndex = 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
begin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strTmp = ""
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if @strWhere != ''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strTmp = " where " + @strWhere
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strSQL = "select top " + str(@PageSize) + " * from ["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @tblName + "]" + @strTmp + " " + @strOrder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
end
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if @IsCount != 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
exec (@strSQL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
GO
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--结束
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
当我在用这个存储过程的时候,刚开始没有发现问题,后来当我的条件很复杂的时候,发现,此存储过程执行遇到错误,下面是出现问题的条件
id<>0 and (companyenname like '%shenzhen%' or companychname like '%shenzhen%' or web like '%shenzhen%' or memo like '%shenzhen%' or address like '%shenzhen%') order by [id] desc) as tblTmp) and id<>0 and (companyenname like '%shenzhen%' or companychname like '%shenzhen%' or web like '%shenzhen%' or memo like '%shenzhen%' or address like '%shenzhen%') and salesid=9
照说这个条件是没有问题的,可是,用上面的存储过程执行,却老是报告错误
后来,经调试,输出生成后的SQL语句,发现,原来问题是出现在嵌套的SQL语句中使用的()身上,于是,我把存储过程改为下面的效果,终于排除了BUG,下面的存储过程不管你的条件有多复杂,只要格式正确,就能运行
CREATE PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(1000) -- 主语句
declare @strTmp varchar(300) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = "<(select min"
set @strOrder = " order by [" + @fldName +"] desc"
end
else
begin
set @strTmp = ">(select max"
set @strOrder = " order by [" + @fldName +"] asc"
end
set @strSQL = "select top " + str(@PageSize) + " * from ["
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
+ @strOrder
if @strWhere != ''
set @strSQL = "select top " + str(@PageSize) + " * from ["
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @fldName + "] from [" + @tblName + "] where (" + @strWhere + ") "
+ @strOrder + ") as tblTmp) and (" + @strWhere + ") " + @strOrder
if @PageIndex = 1
begin
set @strTmp = ""
if @strWhere != ''
set @strTmp = " where (" + @strWhere + ")"
set @strSQL = "select top " + str(@PageSize) + " * from ["
+ @tblName + "]" + @strTmp + " " + @strOrder
end
if @IsCount != 0
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
exec (@strSQL)
GO
要注意看,修改后的存储过程在使用@strWhere时,都在其前后加上了(),这样,就防止嵌套的()出现错误
下面的代码是引用该存储过程的一个范例
SqlConnection MyConnection=new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
DataSet MyDataSet=new DataSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string strKeyword=Keyword.Text.Trim().Replace("'","''");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string strSalesId=Sales.SelectedItem.Value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int RecordCount=CalcRecordCount();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RecordNumber.Text=RecordCount.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
LblRecordNumber.Text=RecordCount.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string strExpress="Id<>0";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if (strKeyword!="")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
strExpress=strExpress+" and (companyenname like '%"+strKeyword+"%' or companychname like '%"+strKeyword+"%' or Companyshortname like '%"+strKeyword+"%' or web like '%"+strKeyword+"%' or mainproduct like '%"+strKeyword+"%' or phone like '%"+strKeyword+"%' or memo like '%"+strKeyword+"%' or address like '%"+strKeyword+"%' or linkmanphone like '%"+strKeyword+"%')";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if (strSalesId!="")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
strExpress=strExpress+" and salesid="+strSalesId;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SqlCommand MyCommand=new SqlCommand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Connection=MyConnection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.CommandText="GetRecordFromPage";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.CommandType=CommandType.StoredProcedure;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Parameters.Add("@tblName","customerview");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Parameters.Add("@fldName","id");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Parameters.Add("@strWhere",strExpress);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Parameters.Add("@PageSize",Int32.Parse(CustomerList.PageSize.ToString()));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Parameters.Add("@PageIndex",Int32.Parse(ViewState["PageIndex"].ToString())+1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SqlDataReader MyReader;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyConnection.Open();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyReader=MyCommand.ExecuteReader();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CustomerList.VirtualItemCount=RecordCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CustomerList.DataSource=MyReader;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CustomerList.DataKeyField="id";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CustomerList.DataBind();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyReader.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyConnection.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
在这里,要注意的是存储过程使用的PAGEINDEX变量是从1开始
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
最后,再排除一个BUG
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
将存储过程中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strSQL varchar(1000) -- 主语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strTmp varchar(300) -- 临时变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strOrder varchar(400) -- 排序类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
改为
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strSQL varchar(2000) -- 主语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strTmp varchar(1000) -- 临时变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strOrder varchar(1000) -- 排序类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
因为我在调试中发现有些查询用的语句选不止300
最后,只要将以上存储过程代码复制并加入到你的数据库中,再按照范例来调用即可了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--开始
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE PROCEDURE GetRecordFromPage
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@tblName varchar(255), -- 表名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@fldName varchar(255), -- 字段名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageSize int = 10, -- 页尺寸
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageIndex int = 1, -- 页码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@IsCount bit = 0, -- 返回记录总数, 非 0 值则返回
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AS
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strSQL varchar(1000) -- 主语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strTmp varchar(300) -- 临时变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strOrder varchar(400) -- 排序类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if @OrderType != 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
begin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strTmp = "<(select min"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strOrder = " order by [" + @fldName +"] desc"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
end
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
begin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strTmp = ">(select max"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strOrder = " order by [" + @fldName +"] asc"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
end
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strSQL = "select top " + str(@PageSize) + " * from ["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @strOrder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if @strWhere != ''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strSQL = "select top " + str(@PageSize) + " * from ["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @fldName + "] from [" + @tblName + "] where " + @strWhere + " "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if @PageIndex = 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
begin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strTmp = ""
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if @strWhere != ''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strTmp = " where " + @strWhere
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strSQL = "select top " + str(@PageSize) + " * from ["
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+ @tblName + "]" + @strTmp + " " + @strOrder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
end
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if @IsCount != 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
exec (@strSQL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
GO
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--结束
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
当我在用这个存储过程的时候,刚开始没有发现问题,后来当我的条件很复杂的时候,发现,此存储过程执行遇到错误,下面是出现问题的条件
id<>0 and (companyenname like '%shenzhen%' or companychname like '%shenzhen%' or web like '%shenzhen%' or memo like '%shenzhen%' or address like '%shenzhen%') order by [id] desc) as tblTmp) and id<>0 and (companyenname like '%shenzhen%' or companychname like '%shenzhen%' or web like '%shenzhen%' or memo like '%shenzhen%' or address like '%shenzhen%') and salesid=9
照说这个条件是没有问题的,可是,用上面的存储过程执行,却老是报告错误
后来,经调试,输出生成后的SQL语句,发现,原来问题是出现在嵌套的SQL语句中使用的()身上,于是,我把存储过程改为下面的效果,终于排除了BUG,下面的存储过程不管你的条件有多复杂,只要格式正确,就能运行
CREATE PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(1000) -- 主语句
declare @strTmp varchar(300) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = "<(select min"
set @strOrder = " order by [" + @fldName +"] desc"
end
else
begin
set @strTmp = ">(select max"
set @strOrder = " order by [" + @fldName +"] asc"
end
set @strSQL = "select top " + str(@PageSize) + " * from ["
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
+ @strOrder
if @strWhere != ''
set @strSQL = "select top " + str(@PageSize) + " * from ["
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @fldName + "] from [" + @tblName + "] where (" + @strWhere + ") "
+ @strOrder + ") as tblTmp) and (" + @strWhere + ") " + @strOrder
if @PageIndex = 1
begin
set @strTmp = ""
if @strWhere != ''
set @strTmp = " where (" + @strWhere + ")"
set @strSQL = "select top " + str(@PageSize) + " * from ["
+ @tblName + "]" + @strTmp + " " + @strOrder
end
if @IsCount != 0
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
exec (@strSQL)
GO
要注意看,修改后的存储过程在使用@strWhere时,都在其前后加上了(),这样,就防止嵌套的()出现错误
下面的代码是引用该存储过程的一个范例
SqlConnection MyConnection=new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
DataSet MyDataSet=new DataSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string strKeyword=Keyword.Text.Trim().Replace("'","''");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string strSalesId=Sales.SelectedItem.Value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int RecordCount=CalcRecordCount();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RecordNumber.Text=RecordCount.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
LblRecordNumber.Text=RecordCount.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string strExpress="Id<>0";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if (strKeyword!="")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
strExpress=strExpress+" and (companyenname like '%"+strKeyword+"%' or companychname like '%"+strKeyword+"%' or Companyshortname like '%"+strKeyword+"%' or web like '%"+strKeyword+"%' or mainproduct like '%"+strKeyword+"%' or phone like '%"+strKeyword+"%' or memo like '%"+strKeyword+"%' or address like '%"+strKeyword+"%' or linkmanphone like '%"+strKeyword+"%')";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if (strSalesId!="")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
strExpress=strExpress+" and salesid="+strSalesId;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SqlCommand MyCommand=new SqlCommand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Connection=MyConnection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.CommandText="GetRecordFromPage";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.CommandType=CommandType.StoredProcedure;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Parameters.Add("@tblName","customerview");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Parameters.Add("@fldName","id");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Parameters.Add("@strWhere",strExpress);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Parameters.Add("@PageSize",Int32.Parse(CustomerList.PageSize.ToString()));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyCommand.Parameters.Add("@PageIndex",Int32.Parse(ViewState["PageIndex"].ToString())+1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SqlDataReader MyReader;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyConnection.Open();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyReader=MyCommand.ExecuteReader();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CustomerList.VirtualItemCount=RecordCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CustomerList.DataSource=MyReader;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CustomerList.DataKeyField="id";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CustomerList.DataBind();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyReader.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
MyConnection.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
在这里,要注意的是存储过程使用的PAGEINDEX变量是从1开始
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
最后,再排除一个BUG
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
将存储过程中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strSQL varchar(1000) -- 主语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strTmp varchar(300) -- 临时变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strOrder varchar(400) -- 排序类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
改为
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strSQL varchar(2000) -- 主语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strTmp varchar(1000) -- 临时变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @strOrder varchar(1000) -- 排序类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
因为我在调试中发现有些查询用的语句选不止300
最后,只要将以上存储过程代码复制并加入到你的数据库中,再按照范例来调用即可了
相关文章推荐
- 真正的分页存储过程,借鉴了CSDN上众多力量,除BUG版,分享给大家
- [分享]真正的分页存储过程,借鉴了CSDN上众多力量,除BUG版,分享给大家
- 超经典的存储过程分页 ;-) 引自CSDN网友所作
- 通用高效分页存储过程代码(来自CSDN文档)
- 真正通用的SQL分页存储过程
- 存储过程分页,这段代码太复杂了。不过值得借鉴
- 真正的分页存储过程
- 修复一个吉日嘎拉MSSQL数据库版中的分页存储过程bug
- 真正高效的SQLServer中数据分页的通用存储过程
- SQL Server 存储过程的分页方案比拼
- 自定义分页控件UI,从Discuz截取,配合SQL自定义分页存储过程一起使用
- 存储过程分页简单例子
- 一个通用的数据分页的存储过程
- 一个高效的数据分页的存储过程
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- Asp.net中DataGrid控件的自定义分页以及存储过程的问题
- [转]SQL Server 存储过程的分页方案比拼
- DataGrid自定义分页存储过程
- 分页的存储过程