我有这样的存储过程:
create procedure sp_testsp
(
@vc_order_by varchar(100),
@int_start_index INT,
@int_grid_size INT,
@count bigint output
)
as
begin
select * from
(select ROW_NUMBER() over
(order by
case @vc_order_by = '' then tab1.int_id end desc) AS row,
*,
COUNT(tab1.int_id) OVER() as totalRowCount
from
(select * from tbl_test) tab1) tab2
where row BETWEEN CONVERT(VARCHAR, @int_start_index) and CONVERT(VARCHAR,(@int_start_index-1) + @int_grid_size);
set @count = 0;
end
我们可以通过以下方式执行上述存储过程:
DECLARE @size bigint;
EXEC sp_testsp '', 1,5, @size output;
SELECT @size;
编写的sp提供基于分页的数据,我们可以通过在@int_grid_size中传递一个数字来检索100条或任意数量的记录。
表格输出如下所示:
row int_id vc_name totalRowCount
1 5 a 107
2 6 ab 107
3 7 abc 107
4 8 abcd 107
5 10 abcc 107
如果我们使用where条件,最后一列给出了表的总记录数或总记录数。
我想在存储过程的"@count"中输出totalRowCount的任意一列值。
我不能使用@ROWCOUNT,因为它只发送sp输出的记录数,即在本例中为5,但实际记录为107。
只是想知道是否有办法。任何帮助都是值得的。谢谢
编辑:
我试过这样的方法,它有效:
create procedure sp_testsp
@param1 nvarchar(800),
@count bigint output
as
begin
select * from tbl_test tt where tt.col1 = @param1;
set @count = select Count(*) from tbl_test tt where tt.col1 = @param1;
end
这个问题是我必须调用一次查询,然后再次调用@count的查询。这是有效的,但对于大型查询需要花费大量时间。
您可以通过临时表来实现这一点
select * into #temp from
(select ROW_NUMBER() over
(order by
case @vc_order_by = '' then tab1.int_id end desc) AS row,
*,
COUNT(tab1.int_id) OVER() as totalRowCount
from
(select * from tbl_test) tab1) tab2
where row BETWEEN CONVERT(VARCHAR, @int_start_index) and CONVERT(VARCHAR,(@int_start_index-1) + @int_grid_size);
select top 1 @count=totalRowCount from #temp
select * from #temp --you can exclude totalRowCount