@@ ROWCOUNT首次返回0

时间:2014-03-26 16:10:11

标签: asp.net sql sql-server vb.net

我想知道这个查询有多少行:

Using ConLoad As SqlConnection = New SqlConnection(constring)
     Dim Query As String = "SELECT @@ROWCOUNT AS ROWnum, *  FROM Specifications")

     ConLoad.Open()
     Using cmd As SqlCommand = New SqlCommand(Query, ConLoad)
         Response.Write(cmd.ExecuteScalar)
     End Using
End Using

在SQL Server Management Studio中执行的此查询返回38,但在我的webform中,它返回0。

我改进了两次发布:

Using ConLoad As SqlConnection = New SqlConnection(constring)
    Dim Query As String = "SELECT @@ROWCOUNT AS ROWnum, *  FROM Specifications")

    ConLoad.Open()
    Using cmd As SqlCommand = New SqlCommand(Query, ConLoad)
        Response.Write(cmd.ExecuteScalar)
    End Using
    Using cmd2 As SqlCommand = New SqlCommand(Query, ConLoad)
        Response.Write(cmd2.ExecuteScalar)
    End Using
End Using

cmd2.ExecuteScalar的结果是38。

如何在第一时间获得好成绩?

2 个答案:

答案 0 :(得分:2)

来自documentation

  

@@ ROWCOUNT返回受 last 语句影响的行数。

通常COUNT(*)用于确定结果的数量,其中结果集本身并不需要。

答案 1 :(得分:2)

@@ROWCOUNT作为查询结果填充,未在查询中使用,您可以使用COUNT(*)

SELECT COUNT(*)
FROM Specifications

或者:

SELECT *
FROM Specifications
PRINT @@ROWCOUNT