通用存储过程怎样返回记录总数
存储过程代码:
ALTER PROCEDURE sp_Search
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '* ', -- 需要返回的列
@fldName varchar(255)= ' ', -- 排序的字段名
@PageSize int = 25, -- 页尺寸
@PageIndex int = 1, -- 页码
@doCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 1, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1500) = ' ' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @doCount != 0
begin
if @strWhere != ' '
set @strSQL = 'select count(*) as Total from [ ' + @tblName + '] where '+@strWhere
else
set @strSQL = 'select count(*) as Total from [ ' + @tblName + '] '
end
else
begin
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
if @PageIndex = 1
begin
if @strWhere != ' '
set @strSQL = 'select top ' + str(@PageSize) + ' '+@strGetFields+ ' from [ ' + @tblName + '] where ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) + ' '+@strGetFields+ ' from [ '+ @tblName + '] '+ @strOrder
end
else
begin
set @strSQL = 'select top ' + str(@PageSize) + ' '+@strGetFields+ ' 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) + ' '+@strGetFields+ ' from [ '
+ @tblName + '] where [ ' + @fldName + '] ' + @strTmp + '([ '
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [ '
+ @fldName + '] from [ ' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
end
exec (@strSQL)
asp文件调用代码:
<p><% Dim CmdSP,rs
set CmdSP = Server.CreateObject("ADODB.Command")
CmdSP.ActiveConnection = cnn
CmdSP.CommandText = "sp_search"
CmdSP.CommandType = adCmdStoredProc
CmdSP.Prepared = true
Set rs = cnn.Execute("sp_Search 'v_datasheets', 'Part,Name', 'part',10," & request.querystring("p") & ",0,1,'brand=" & rs("ID") & "' ")
While Not rs.EOF
% <br><%=rs("name")%> <br>
<%rs1.MoveNext
Wend%></p>
现在要获取rs的记录总数进行分页,请教该怎样写呢。
------解决方案--------------------select count(*) from (子查询) t
--或
select @@ROWCOUNT
------解决方案--------------------在exec (@strSQL)后面加上select @@ROWCOUNT 就可以得到返回行数了`
------解决方案--------------------@doCount bit = 0, -- 返回记录总数, 非 0 值则返回 这里写的比较清楚了啊!