execute sql count statement in asp

Discussion in 'ASP General' started by Matt, Jan 11, 2004.

  1. Matt

    Matt Guest

    In ASP page, if we want to retrive the number of rows in database: should we
    use recordset object? Since this is not update, insert, delete, or select
    statement

    sqlStmt = "SELECT COUNT(*) from table1;"
    Set objRS = Server.CreateObject("ADODB.RecordSet")
    objRS.open sqlStmt, strConnect

    But how do we can the value?
     
    Matt, Jan 11, 2004
    #1
    1. Advertising

  2. Matt

    Ray at Guest

    "Matt" <> wrote in message
    news:...
    > In ASP page, if we want to retrive the number of rows in database: should

    we
    > use recordset object? Since this is not update, insert, delete, or select
    > statement


    Yes, you should, since you need to retrieve a value. But it'd be like so:

    sqlStmt = "SELECT COUNT(*) from table1;"
    Set oADO = Server.CreateObject("ADODB.Connection")
    oADO.Open strConnect
    Set rsCount = oADO.Execute(sqlStmt)
    iCount = rsCount.Fields.Item(0).Value
    rsCount.Close : Set rsCount = Nothing
    oADO.Close : Set oADO = Nothing

    Response.Write "The count is " & iCount


    Just be aware of issues with counting * as opposed to a specific column that
    will always have a value, ie. the PK. See here, at least.
    http://www.aspfaq.com/show.asp?id=2073

    Ray at home


    >
    > sqlStmt = "SELECT COUNT(*) from table1;"
    > Set objRS = Server.CreateObject("ADODB.RecordSet")
    > objRS.open sqlStmt, strConnect
    >
    > But how do we can the value?
    >
    >
    >
     
    Ray at, Jan 11, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    392
  2. Replies:
    21
    Views:
    1,068
    Giannis Papadopoulos
    Aug 2, 2005
  3. efelnavarro09
    Replies:
    2
    Views:
    953
    efelnavarro09
    Jan 26, 2011
  4. Savas Ates
    Replies:
    1
    Views:
    294
    Patrice
    Aug 17, 2004
  5. weiwei

    asp and sql statement in sql server db

    weiwei, Sep 22, 2004, in forum: ASP General
    Replies:
    3
    Views:
    211
    Jeff Cochran
    Sep 22, 2004
Loading...

Share This Page