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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What is wrong with these SQL statements??

What is wrong with these SQL statements??

Scheduled Pinned Locked Moved C / C++ / MFC
databasequestionhelp
7 Posts 5 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.
  • I Offline
    I Offline
    IrishSonic
    wrote on last edited by
    #1

    Hiya getting a message saying "Database error - There were no columns specified to retrive." when using these 2 lines: SqlString = "DELETE * from Branches WHERE Branch_No LIKE '4%' "; OR SqlString = "INSERT INTO Branches (Branch_No,Branch_Name) VALUES ('6','Derry') "; But Branch_No is the first column and Branch_Name is the second.. Am I using the statements wrong?? Thanks, grahamoj.

    V T J 3 Replies Last reply
    0
    • I IrishSonic

      Hiya getting a message saying "Database error - There were no columns specified to retrive." when using these 2 lines: SqlString = "DELETE * from Branches WHERE Branch_No LIKE '4%' "; OR SqlString = "INSERT INTO Branches (Branch_No,Branch_Name) VALUES ('6','Derry') "; But Branch_No is the first column and Branch_Name is the second.. Am I using the statements wrong?? Thanks, grahamoj.

      V Offline
      V Offline
      vin
      wrote on last edited by
      #2

      grahamoj wrote: SqlString = "DELETE * from Branches WHERE Branch_No LIKE '4%' "; It should be DELETE FROM WHERE Vini

      1 Reply Last reply
      0
      • I IrishSonic

        Hiya getting a message saying "Database error - There were no columns specified to retrive." when using these 2 lines: SqlString = "DELETE * from Branches WHERE Branch_No LIKE '4%' "; OR SqlString = "INSERT INTO Branches (Branch_No,Branch_Name) VALUES ('6','Derry') "; But Branch_No is the first column and Branch_Name is the second.. Am I using the statements wrong?? Thanks, grahamoj.

        T Offline
        T Offline
        Ted Ferenc
        wrote on last edited by
        #3

        grahamoj wrote: SqlString = "INSERT INTO Branches (Branch_No,Branch_Name) VALUES ('6','Derry') "; 1. What is the error message 2. Does the row exists, if so the statement will fail 3. Is Branch_No declared as INT it so try 6 not '6', although I don't think this will make any diference.


        If I have seen further it is by standing on the shoulders of Giants. - Isaac Newton 1676

        I 1 Reply Last reply
        0
        • T Ted Ferenc

          grahamoj wrote: SqlString = "INSERT INTO Branches (Branch_No,Branch_Name) VALUES ('6','Derry') "; 1. What is the error message 2. Does the row exists, if so the statement will fail 3. Is Branch_No declared as INT it so try 6 not '6', although I don't think this will make any diference.


          If I have seen further it is by standing on the shoulders of Giants. - Isaac Newton 1676

          I Offline
          I Offline
          IrishSonic
          wrote on last edited by
          #4

          // Open the database database.Open(NULL,false,false,sDsn); // Allocate the recordset CRecordset recset( &database ); // Build the SQL statement SqlString = "DELETE FROM Branches WHERE Branch_No LIKE '4%'"; //SqlString = "INSERT INTO Branches (Branch_No,Branch_Name) VALUES ('6','Derry') "; // Execute the query recset.Open( CRecordset::forwardOnly,SqlString,CRecordset::readOnly ); I now know it is the last line, so changed it to: recset.Open( AFX_DB_USE_DEFAULT_TYPE,SqlString,CRecordset::none ); but this still does not delete the line. The error message I am getting is: "Database Error: Attempt to open a table failed - there were no columns to retrive were specified". But my table is called Branches and is specified!! Any ideas. grahamoj

          T K 2 Replies Last reply
          0
          • I IrishSonic

            // Open the database database.Open(NULL,false,false,sDsn); // Allocate the recordset CRecordset recset( &database ); // Build the SQL statement SqlString = "DELETE FROM Branches WHERE Branch_No LIKE '4%'"; //SqlString = "INSERT INTO Branches (Branch_No,Branch_Name) VALUES ('6','Derry') "; // Execute the query recset.Open( CRecordset::forwardOnly,SqlString,CRecordset::readOnly ); I now know it is the last line, so changed it to: recset.Open( AFX_DB_USE_DEFAULT_TYPE,SqlString,CRecordset::none ); but this still does not delete the line. The error message I am getting is: "Database Error: Attempt to open a table failed - there were no columns to retrive were specified". But my table is called Branches and is specified!! Any ideas. grahamoj

            T Offline
            T Offline
            Ted Ferenc
            wrote on last edited by
            #5

            What I always do is use "Query Analyzer" to check out my SQL statement, I tried the following on a scratch table use teds INSERT INTO winrasdialit (col001, col002, col003) VALUES('hello', getdate(), 5) It works OK, so if your SQL statement works in "Query Analyzer" you have at least eliminated one possible error. It does look like you do not have the database open for read only as delete seems to work? I don't use CRecordset so I can't comment on your synatx


            If I have seen further it is by standing on the shoulders of Giants. - Isaac Newton 1676

            1 Reply Last reply
            0
            • I IrishSonic

              Hiya getting a message saying "Database error - There were no columns specified to retrive." when using these 2 lines: SqlString = "DELETE * from Branches WHERE Branch_No LIKE '4%' "; OR SqlString = "INSERT INTO Branches (Branch_No,Branch_Name) VALUES ('6','Derry') "; But Branch_No is the first column and Branch_Name is the second.. Am I using the statements wrong?? Thanks, grahamoj.

              J Offline
              J Offline
              JockeP
              wrote on last edited by
              #6

              Hi! I'm just curious how you manage to execute those insert and delete queries? I get rt error when doing it like this: CDaoDatabase* m_pDB; CDaoRecordset* m_pRS; m_pDB = new CDaoDatabase; m_pRS = new CDaoRecordset(m_pDB); try { m_pDB->Open("c:\\accessdb\\db1.mdb"); // CString strQuery = _T("SELECT * FROM Table1"); //Works just fine... CString strQuery = _T("INSERT INTO Table1 VALUES ('3', 'Olga')"); //Doesn't work m_pRS->Open(dbOpenDynaset, strQuery); } catch (CDaoException* e) { AfxMessageBox(e->m_pErrorInfo->m_strDescription); e->Delete(); } Can you tell me how to do it?

              1 Reply Last reply
              0
              • I IrishSonic

                // Open the database database.Open(NULL,false,false,sDsn); // Allocate the recordset CRecordset recset( &database ); // Build the SQL statement SqlString = "DELETE FROM Branches WHERE Branch_No LIKE '4%'"; //SqlString = "INSERT INTO Branches (Branch_No,Branch_Name) VALUES ('6','Derry') "; // Execute the query recset.Open( CRecordset::forwardOnly,SqlString,CRecordset::readOnly ); I now know it is the last line, so changed it to: recset.Open( AFX_DB_USE_DEFAULT_TYPE,SqlString,CRecordset::none ); but this still does not delete the line. The error message I am getting is: "Database Error: Attempt to open a table failed - there were no columns to retrive were specified". But my table is called Branches and is specified!! Any ideas. grahamoj

                K Offline
                K Offline
                Kelly Herald
                wrote on last edited by
                #7

                Since the DELETE and INSERT commands don't return a recordset you shouldn't use the CDaoRecordset (or CRecordset) class to execute them. Just use the Execute method in the CDaoDatabase (or CDatabase) class. Kelly Herald Software Developer MPC formerly Micronpc

                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