这是SQL Server中的存储过程:
----分页存储过程开始创建-----------------------------------------------------------------------------------------------------------
CREATE Proc [dbo].[PageShowOne]
@PageSize int=10 ,--每页显示的记录数
@PageCurrent int=1 ,--当前要显示的页号
@FdName varchar(100)='' ,--主键名或者标识列名
@SelectStr varchar(2000)='', --select子句,不包含select关键字,如:*或者Id,UserId,UserName等。
@FromStr varchar(1000)='', --from子句,不包含from关键子,如:myTable或者myTable,yourTable
@WhereStr varchar(2000)='', --Where子句,不包含where关键字,如空的,或者 id>2 等
@OrderByStr varchar(1000)='',--order by 子句,不包含order by 子句 ,如id desc,UserId asc 等
@CountRows int=0 output, --返回记录总数
@CountPage int=0 output --返回总页数
as
--------定义局部变量---------
declare @Id1 varchar(20),@Id2 varchar(20) --开始和结束的记录号
declare @OrderBySqls varchar(1000) --order by 子句
declare @WhereSqls varchar(2000) --where 子句
declare @Sqls nvarchar(4000) --最终组合成的Sqls语句
declare @TmpStr varchar(2000) --临时
----------------------------
if @OrderByStr <> ''
set @OrderBySqls = ' order by '+@OrderByStr
else
set @OrderBySqls = ''
--------
if @WhereStr <> ''
set @WhereSqls = ' where ('+@WhereStr+')'
else
set @WhereSqls = ''
--------
set @TmpStr = @WhereSqls
--如果显示第一页,可以直接用top来完成
if @PageCurrent<=1
begin
select @Id1=cast(@PageSize as varchar(20))
exec('select top '+@Id1+' '+@SelectStr+' from '+@FromStr+@WhereSqls+@OrderBySqls)
goto LabelRes
end
---------------------------
select @Id1=cast(@PageSize as varchar(20))
,@Id2=cast((@PageCurrent-1)*@PageSize as varchar(20))
----------
if @WhereSqls <> ''
set @WhereSqls = @WhereSqls + ' and (' + @FdName+' not in(select top '+@Id2+' '+@FdName+' from '+@FromStr+@WhereSqls+@OrderBySqls+'))'
else
set @WhereSqls = ' where ' + @FdName+' not in(select top '+@Id2+' '+@FdName+' from '+@FromStr+@WhereSqls+@OrderBySqls+')'
----------
set @Sqls = 'select top '+@Id1+ ' '+ @SelectStr+' from '+@FromStr+@WhereSqls+@OrderBySqls
exec (@Sqls)
-----------
LabelRes:
-----返回总记录数
set @Sqls = 'select @a=count(1) from '+@FromStr+@TmpStr
exec sp_executesql @sqls,N'@a int output',@CountRows output
-----返回总页数
if @CountRows <= @PageSize
set @CountPage = 1
else
begin
set @CountPage = @CountRows/@PageSize
if (@CountRows%@PageSize) > 0
set @CountPage = @CountPage + 1
end
return
GO
X3 平台代码:
public
{public declarations}
i, RsCount: integer;
procedure TMainForm.btnFirstClick(Sender: TObject);
var
guid:string;
sqlstr:string;
lStoredProc: TStoredProc;
a,b:string;
begin
guid:=DataSetBroker1.DataSet.FieldByName('guid').AsString ;
i := 1;
with DataSetSP1 do
try
close;
ConnectionString := 'DATABASEURL=Biz:\SDBZBZYTHXXGLXT\SJK.Database';
StoredProcName := 'PageShowOne';
Params.Clear;
Params.CreateParam(TFieldType.ftInteger, '@RETURN_VALUE', TParamType.ptResult);
Params.CreateParam(TFieldType.ftInteger, '@PageSize', TParamType.ptInput);
Params.CreateParam(TFieldType.ftInteger, '@PageCurrent', TParamType.ptInput);
Params.CreateParam(TFieldType.ftString, '@FdName', TParamType.ptInput);
Params.CreateParam(TFieldType.ftString, '@SelectStr', TParamType.ptInput);
Params.CreateParam(TFieldType.ftString, '@FromStr', TParamType.ptInput);
Params.CreateParam(TFieldType.ftString, '@WhereStr', TParamType.ptInput);
Params.CreateParam(TFieldType.ftString, '@OrderByStr', TParamType.ptInput);
Params.CreateParam(TFieldType.ftInteger, '@CountRows', TParamType.ptOutPut);
Params.CreateParam(TFieldType.ftInteger, '@CountPage', TParamType.ptOutPut);
Params[1].AsInteger := 20;
Params[2].AsInteger := i;
Params[3].AsString := 'guid';
Params[4].AsString := '*';
Params[5].AsString := 'ZBJS';
Params[6].AsString := 'zbguid='+guid;
Params[7].AsString := 'SJ';
Open ;
//dialogs.showmessage(Params.ParamByName('@aaa').AsString);
edit1.Text := '记录总数:'+SysUtils.IntToStr(Params[8].AsInteger );
edit2.Text := '总页数:'+SysUtils.IntToStr(Params[9].AsInteger);
RsCount := Params[9].AsInteger;
edit3.Text := '第 '+SysUtils.IntToStr(i)+ ' 页';
//jsDialogs.ShowMsg (SysUtils.InttoStr(sp.RecordCount),'dd');
finally
//Free;
end;
总页数返回为0,总记录数也返回为0
可是在SQL Server的查询分析器中,就有值了,比如执下面代码:
declare @abc int,@cba int
execute PageShowOne 20,1,'guid','*','ZBJS','zbguid=''8114''','SJ asc',@CountRows=@abc output
,@CountPage=@cba outPut
print @abc
print @cba |