2014-03-26 258 views
0

我想知道有多少行獲得與此查詢:@@ ROWCOUNT返回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 
End Using 

在SQL Server Management Studio中執行該查詢返回38,但在我的網頁表單它返回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

我該如何做才能在第一時間取得好成績?

+0

選擇表的所有記錄,只計算其元素效率很低,請改用'SELECT COUNT(*)FROM yourTable' –

回答

2

@@ROWCOUNT填充作爲查詢,在查詢中未使用的結果,你可以使用COUNT(*)

SELECT COUNT(*) 
FROM Specifications 

或者:

SELECT * 
FROM Specifications 
PRINT @@ROWCOUNT