I’m trying to get a row count out of a recordset so that I can make sure I have actually data to work with. The table is fed from another source and sometimes they have problems updating the table, hence I need to check I have something there before I continue with the data.
The server is SQL Server 2012. I can get the connection without any issues. I have the following code:
Dim rec_count as Int64
Dim rs2 As RecordSet
rs2 = mDb.SQLSelect(“SELECT COUNT_BIG(*) AS recordcount FROM Product_Local;”)
rec_count = rs2.Field(“recordcount”).Int64Value
When I run “SELECT COUNT_BIG(*) AS recordcount FROM Product_Local;” in Microsoft SQL Server Management Studio I get the following result:
|___| recordcount|
| 1 | 5485114 |
According to Microsoft, COUNT and COUNT_BIG return the value as bigint.
I has assumed I could read that field just like any other recordset but when I try get a NilObjectException but I appear to be missing something. I have tried COUNT with the same results.
are you saying RS2 is NIL?
if so that indicates the SQLQuery is not valid (not that it didn’t return a result, but that it COULDN’T)
perhaps the database plugin doesn’t recognize COUNT_BIG? (oh… I see you tried just COUNT)
that is strange… as a COUNT(*) should always return a non-nil value even if the table in question is empty, and if the table didn’t exist you would get an error
Yet there was one.
What does that catch code look like?
two questions:
Is the table actually called Product_Local
Does it work if you remove the ; character, since that may be causing the database to begin a new statement?
Dim rec_count as Integer
Dim rs2 As RecordSet
rs2 = mDb.SQLSelect(“SELECT COUNT_BIG(*) AS recordcount FROM Product_Local;”)
rec_count = rs2.Field(“recordcount”).Value
I put:
System.DebugLog(str(rs2.Field(“recordcount”).Value) + “record count”)
and the program gave me the results.
I then commented out “System.DebugLog(str(rs2.Field(“recordcount”).Value) + “record count”)” and it still runs correctly now.