Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. CRecordSet and CDatabase.ExecuteSQL

CRecordSet and CDatabase.ExecuteSQL

Scheduled Pinned Locked Moved Database
databasehelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pnpfriend
    wrote on last edited by
    #1

    Hello all.. i need your help again. I'm use CDatabase and CRecordSet to access to my access database. forexample myTable have two rows, row1 and row2. row1 contains texts, row2 contain numbers as shown below. r1 | r2 ___________ a | 1 a | 1 b | 2 c | 1 a | 1 c | 5 d | 5 d | 9 a | 3 I want only a,b,c,d from row1 not entire column, so I wrote the following code to get select the data that I want. but it still giving me entire column instead of only a,b,c,d could you please point me out that what am i doing wrong? CDatabase db; MyCRecordSet rs(&db); db.open..... rs.open... //some more code here then CString qstr = "SELECT DISTINCTROW r1 FROM myTable"; rs.m_pDatabase->ExecuteSQL(qstr); if(rs.ReQuery()) { while(!rs.isEOF()) { MessageBox(rs.rOneData); } } thank you

    R D 2 Replies Last reply
    0
    • P pnpfriend

      Hello all.. i need your help again. I'm use CDatabase and CRecordSet to access to my access database. forexample myTable have two rows, row1 and row2. row1 contains texts, row2 contain numbers as shown below. r1 | r2 ___________ a | 1 a | 1 b | 2 c | 1 a | 1 c | 5 d | 5 d | 9 a | 3 I want only a,b,c,d from row1 not entire column, so I wrote the following code to get select the data that I want. but it still giving me entire column instead of only a,b,c,d could you please point me out that what am i doing wrong? CDatabase db; MyCRecordSet rs(&db); db.open..... rs.open... //some more code here then CString qstr = "SELECT DISTINCTROW r1 FROM myTable"; rs.m_pDatabase->ExecuteSQL(qstr); if(rs.ReQuery()) { while(!rs.isEOF()) { MessageBox(rs.rOneData); } } thank you

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      pnpfriend wrote: CString qstr = "SELECT DISTINCTROW r1 FROM myTable"; should be "SELECT DISTINCT r1 FROM myTable"; pnpfriend wrote: myTable have two rows, row1 and row2 I think you meant myTable have two Columns, r1 and r2 and you want one row for each unique value in column r1 "select distinct" will do that. select distinctrow retuns all the rows that are not duplicates (all column vaues equal). Power corrupts and PowerPoint corrupts absolutely. - Vint Cerf

      P 1 Reply Last reply
      0
      • R Rob Graham

        pnpfriend wrote: CString qstr = "SELECT DISTINCTROW r1 FROM myTable"; should be "SELECT DISTINCT r1 FROM myTable"; pnpfriend wrote: myTable have two rows, row1 and row2 I think you meant myTable have two Columns, r1 and r2 and you want one row for each unique value in column r1 "select distinct" will do that. select distinctrow retuns all the rows that are not duplicates (all column vaues equal). Power corrupts and PowerPoint corrupts absolutely. - Vint Cerf

        P Offline
        P Offline
        pnpfriend
        wrote on last edited by
        #3

        yes.. i meant myTable have two columns, r1 and r2 i think i have to use distinctrow, since you said that distinctrow returns all the rows that are not duplicates meaning if i use distinctrow i will get a,b,c,d from r1 as I wanted. anyway i tried both and it is not giving me the answer i wanted. CString qstr = "select distinctrow r1 from myTable"; rs.m_pDatabase->ExecuteSQL(qstr); if(rs.ReQuery()) { while(!rs.IsEOF()) { MessageBox(rs.r1Data); rs.MoveNext(); } } keeping giving me entire r1 column data. is it because of rs.IsEOF() while loop?? is is CRecordSet will have the record of whatever record set of sql query statement executed and return??

        R 1 Reply Last reply
        0
        • P pnpfriend

          yes.. i meant myTable have two columns, r1 and r2 i think i have to use distinctrow, since you said that distinctrow returns all the rows that are not duplicates meaning if i use distinctrow i will get a,b,c,d from r1 as I wanted. anyway i tried both and it is not giving me the answer i wanted. CString qstr = "select distinctrow r1 from myTable"; rs.m_pDatabase->ExecuteSQL(qstr); if(rs.ReQuery()) { while(!rs.IsEOF()) { MessageBox(rs.r1Data); rs.MoveNext(); } } keeping giving me entire r1 column data. is it because of rs.IsEOF() while loop?? is is CRecordSet will have the record of whatever record set of sql query statement executed and return??

          R Offline
          R Offline
          Roger Wright
          wrote on last edited by
          #4

          The DISTINCTROW only eliminates duplicate records from the query result; it does nothing to qualify the selection. You need to include a WHERE clause in the query to define what records you want returned. Adding the clause WHERE r1 >= 'a' AND r1 <= 'd' (or whatever is syntactically correct for Access) will filter the resulting recordset so that it contains only records that meet the specified criteria. Also, specifying r1 as the only value returned is probably not what you intend - try SELECTing * to return entire records. Will Build Nuclear Missile For Food - No Target Too Small

          1 Reply Last reply
          0
          • P pnpfriend

            Hello all.. i need your help again. I'm use CDatabase and CRecordSet to access to my access database. forexample myTable have two rows, row1 and row2. row1 contains texts, row2 contain numbers as shown below. r1 | r2 ___________ a | 1 a | 1 b | 2 c | 1 a | 1 c | 5 d | 5 d | 9 a | 3 I want only a,b,c,d from row1 not entire column, so I wrote the following code to get select the data that I want. but it still giving me entire column instead of only a,b,c,d could you please point me out that what am i doing wrong? CDatabase db; MyCRecordSet rs(&db); db.open..... rs.open... //some more code here then CString qstr = "SELECT DISTINCTROW r1 FROM myTable"; rs.m_pDatabase->ExecuteSQL(qstr); if(rs.ReQuery()) { while(!rs.isEOF()) { MessageBox(rs.rOneData); } } thank you

            D Offline
            D Offline
            DiWa
            wrote on last edited by
            #5

            ExecuteSQL lets you send SQL-Commands to the Database directly, but it doesn't change the recordset (you can prove by examining the m_strSQL member) to use aggregate functions and the like (sum, count, distinct, ...) you have to modify the DopFieldExchange-function: void CDPDCount::DoFieldExchange(CFieldExchange* pFX) { pFX->SetFieldType(CFieldExchange::outputColumn); RFX_Long(pFX, _T("COUNT(*)"), m_lCount); } hope it helps :) dirk

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups