简体   繁体   English

获取没有循环的存储过程的行数

[英]Get number of rows of a stored procedure without loop

Using c# how can I get the number of rows of the output of a stored procedure? 使用C#如何获取存储过程输出的行数? I am trying something without loop and without using COUNT(*) in the database. 我正在尝试一些没有循环且没有在数据库中使用COUNT(*)的东西。 basically something like: 基本上是这样的:

db.Query("storedProcedure").count()

If you fetch the data into a DataSet , you can then do something like: 如果将数据提取到DataSet ,则可以执行以下操作:

ds.Tables[0].Rows.Count

http://msdn.microsoft.com/en-us/library/system.data.datatable.rows(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system.data.datatable.rows(v=vs.110).aspx

.Tables[0] indicates the first set of results returned by your query (if you happen to have multiple SELECT statements in your stored procedure). .Tables[0]指示查询返回的第一组结果(如果您的存储过程中碰巧有多个SELECT语句)。 Otherwise it's simply referring to the results of your only SELECT . 否则,它只是指您唯一的SELECT的结果。

You can modify the stored procedure to output rows total along with its usual output (Microsoft SQL server syntax) 您可以修改存储过程以输出总行数及其常规输出(Microsoft SQL Server语法)

create proc dbo.My_Test
  @rows int out
as

-- preparing test data - ignore that
declare @tmp table(id int)
insert into @tmp values(1)
insert into @tmp values(2)

select * from @tmp

select @rows = @@rowcount

If you stored procedure already exists and it doesnt' play with "set nocount on"; 如果存储过程已经存在并且不能与“ set nocount on”一起使用; you can retrieve counts like: 您可以像这样检索计数:

create proc dbo.My_Test
as
-- preparing test data - ignore that
declare @tmp table(id int)
insert into @tmp values(1)
insert into @tmp values(2)

select * from @tmp

and call the sp like: 并像这样调用sp:

declare @rows int
exec dbo.My_Test
select @rows = @@rowcount
-- @rows = 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM