From: Andreas Warning on
Hello,

how can I find out how many records are in a SQL Table.

I used MFC Recordsets

Do you have an idea ?

Thanks
Andreas


From: Arman Sahakyan on
Just the rough idea:

recordset->Open("select * from table");
recordset->GetRecordCount();

"Andreas Warning" <andy41(a)gmx.net> wrote in message
news:O5tXVgOpKHA.1548(a)TK2MSFTNGP04.phx.gbl...
> Hello,
>
> how can I find out how many records are in a SQL Table.
>
> I used MFC Recordsets
>
> Do you have an idea ?
>
> Thanks
> Andreas
>
From: Hector Santos on
Andreas Warning wrote:

> Hello,
>
> how can I find out how many records are in a SQL Table.
>
> I used MFC Recordsets
>
> Do you have an idea ?

As another poster answered, you can use the MFC class CRecordset
member function, GetRecordCount().

But if you are only concern the COUNT and not the actual records or
wish to optimize the query, then you can optimize this depending on
the backend SQL server, for example:

Select count(*) from TABLE

The SQL server MAY support the count(*) field which will return 1
record set and 1 field.

This is more efficient since GetRecordCount() will technically do a
LOOP to read (fetch) all the recordsets into memory. That also depend
son whether you are using snap shots, the type of DSN and protocol
driver (i.e., ODBC) optimizes its memory, etc.

Nonetheless, in general, the results of a SQL command is not known
until you actually FETCH the records. That is the RULE of THUMB and
in general, you have to LOOP the result to get a count.

So if you only need the COUNT, see if your SQL server supports it.

Microsoft SQL Server is one that does:

http://msdn.microsoft.com/en-us/library/ms175997.aspx
http://msdn.microsoft.com/en-us/library/aa258232(SQL.80).aspx

--
HLS
From: Goran on
On Feb 3, 4:46 pm, "Andreas Warning" <and...(a)gmx.net> wrote:
> Hello,
>
> how can I find out how many records are in a SQL Table.
>
> I used MFC Recordsets
>
> Do you have an idea ?

+1 for "don't use GetRecordCount()". As Hector says, under the hood,
things are not supposed to work that way with all databases (recordset
record count is not known up-front). SELECT COUNT(*) FROM X is better.

Note, however, that result is purely informational. For example
(depending on your situation), you should not be using said count+1 as
next record key, due to concurrency issues. If all you want is e.g.
"processing X of Y records", then it's OK.

Goran.
From: Andreas Warning on
here my code. It shows always a result auf 1.

What do I wrong



// Create a Action Log Object
m_pActLog=new CActionLog(g_bSQLServer);
m_pActLog->SetTableName(_T("[ActionLog]"));
TRY
{

m_pActLog->Open( CRecordset::dynaset,_T("Select count(*) from
ActionLog"));

long zz = m_pActLog->GetRecordCount();

}
CATCH(CDBException, e)
{
CString error = CString("ERROR: ") + e->m_strError + CString("\nODBC: ")
+ e->m_strStateNativeOrigin;
AfxMessageBox(error);
delete m_pActLog;
AfxMessageBox(_T("ActionLog Table not ok Terminated Program"));
exit(0);
}
END_CATCH
"Goran" <goran.pusic(a)gmail.com> schrieb im Newsbeitrag
news:de01d86e-4efb-4f07-8f9c-4769c1e3b31d(a)q16g2000yqq.googlegroups.com...
On Feb 3, 4:46 pm, "Andreas Warning" <and...(a)gmx.net> wrote:
> Hello,
>
> how can I find out how many records are in a SQL Table.
>
> I used MFC Recordsets
>
> Do you have an idea ?

+1 for "don't use GetRecordCount()". As Hector says, under the hood,
things are not supposed to work that way with all databases (recordset
record count is not known up-front). SELECT COUNT(*) FROM X is better.

Note, however, that result is purely informational. For example
(depending on your situation), you should not be using said count+1 as
next record key, due to concurrency issues. If all you want is e.g.
"processing X of Y records", then it's OK.

Goran.