Record count using Read()

倖福魔咒の 提交于 2019-12-25 07:49:03

问题


I am trying to figure out how to get the max record count before i populate a listview grid. I'm using the Oracle 10g DB and I've tried:

SELECT COUNT(*) as countNum, status, date, theTitle, theMessage, date2 " & _
       "FROM blah blah...

messagebox.show(dr(0))

But that makes the SQL query crash. It doesnt seem as though i am about to put anything related to "count" in my query or it will crash so is there any other way i can see how many records it returns other than that?

Thanks! :o)

David


回答1:


Use analytical count like so: SELECT COUNT(*) over () as countNum, status, date, theTitle, theMessage, date2 " & _ "FROM blah blah




回答2:


When using an aggregate function such as count you need to use a group by clause for all those fields in your result set that are not the result of an aggregate function.




回答3:


I wrote your query once in a kind of dynamic view in the WITH clause
and combine a count and select over this query in one select.

WITH query AS (
 SELECT *
 FROM foobar
 WHERE foo = 1
)
SELECT
  ( SELECT COUNT(*) FROM query ) cnt,
  status,
  date,
  theTitle,
  theMessage
FROM query



回答4:


I would use THIS to retrieve a simple count of records. Not sure why you would want to pull any other fields in this operation. If there are selection criteria, apply a WHERE or HAVING clause (as appropriate):

SELECT COUNT(YourPrimaryKey) As RecordCount
FROM YourTable
GROUP BY YourPrimaryKey

THen in your code use cmd.ExecuteScalar to retrieve the result . . .



来源:https://stackoverflow.com/questions/4829329/record-count-using-read

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!