Menu

SQL count

2005-04-05
2013-03-07
  • victor Saquicela

    Hi.
    How can I execute a criteria to obtain a count of objects, but i don't retrive a collection, i need only count. It is to verify if exist any objects in the association.

     
    • Richard Banks

      Richard Banks - 2005-04-05

      Hi Victor,

      If you want to use the "Select Count(...) from ..." SQL syntax then you will need to use a SummaryCriteria.  Eg

      dim sc as CSummaryCriteria
      sc.ClassMap = myClassMap
      sc.addSummaryField("myField", SumMethod.Count)
      dim cc as CCursor = sc.perform()

      dim recordCount as Integer
      recordCount = cc.ResultSet.ResultSet.Tables(0).Rows(0).Item(0)

      If you are just wanting to know if any records exist, you can use a normal retrieve criteria, and just use a rowlimit of 1.  After you do the perform just do a check if records exist in the cursor.

      For example

      Dim rc as new CRetrieveCriteria
      rc.RowLimit = 1
      ...
      dim cc as CCursor = rc.Perform()
      if cc.hasElement then
        'Records exist
      else
        'No records
      end if

      - Richard

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.