Results 1 to 2 of 2

Thread: Trouble retrieving 'last Insert ID' on SQL Server

  1. #1
    Join Date
    Dec 2010
    Posts
    35
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Trouble retrieving 'last Insert ID' on SQL Server

    I'm developing a program that will work with both MySQL and SQLServer databases.

    On MySQL, I can successfully use the QtSQL module to insert records into the database - the tables have a an 'AUTO_INCREMENT' key field on them. I can successfully retrieve the 'last Insert ID' value via the 'QSqlQuery::lastInsertId ()' method. Works great.

    On SQLServer, that feature does not exist so I updated my program to issue a QSqlQuery of "SELECT SCOPE_IDENTITY()". This seems to execute but when I retrieve the resultant values from the query, I get one value and it is always 0.

    In a SQL Server Management Studio query window, I successfully inserted a record into a table (as my program does) and executed the "SELECT SCOPE_IDENTITY()" query immediately afterward and it retrieved the correct value.

    I've tried a number of options - using transactions and no transactions. commiting the INSERT transaction before issuing the SELECT transaction, etc.

    Basic code:
    Qt Code:
    1. db = QSqlDatabase::database(dbName, true);
    2. // db.transaction();
    3. QString queryStr = "INSERT INTO ADDRESS (ts, address1) "
    4. " VALUES (:ts, :address1)";
    5. QSqlQuery qQuery (db);
    6. qQuery.prepare(queryStr);
    7. QDateTime ts = QDateTime::currentDateTime();
    8.  
    9. qQuery.bindValue (":ts", QVariant (ts));
    10. qQuery.bindValue (":address1", QVariant ("test address1"));
    11.  
    12. if (qQuery.exec()) {
    13. cout << "Success!" << endl;
    14.  
    15. // db.commit();
    16.  
    17. QSqlQuery query2 (db);
    18. query2.prepare ("SELECT SCOPE_IDENTITY()");
    19. query2.setForwardOnly(true);
    20. if (query2.exec()) {
    21. while (query2.next()) {
    22. QVariant dbValue = query2.value(0);
    23. cout << "testdb(): DB value: " << dbValue.toString().toStdString() << endl;
    24. }
    25. } else {
    26. // error
    27. }
    28. } else {
    29. // error
    30. }
    To copy to clipboard, switch view to plain text mode 
    Ideas?


    Added after 1 1:


    This appears to be a SQLServer 2008 bug and I'm using SQLServer 2008. Grr.....
    Last edited by redBeard; 11th October 2011 at 00:09.

  2. #2
    Join Date
    Feb 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Trouble retrieving 'last Insert ID' on SQL Server

    Try to use @@IDENTITY poor man.

Similar Threads

  1. SQL Server data retrieving speed problem
    By Aleksandar in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2010, 13:52
  2. Trouble Applying the Fortune Threaded Server
    By dhice in forum Qt Programming
    Replies: 7
    Last Post: 18th April 2009, 22:04
  3. retrieving signal name in a slot
    By Baschterl in forum Qt Programming
    Replies: 2
    Last Post: 10th November 2008, 20:44
  4. Trouble with "INSERT" by QSqlTableModel
    By AD in forum Qt Programming
    Replies: 12
    Last Post: 10th November 2008, 08:21
  5. Storing and Retrieving a QFlags
    By darkadept in forum Qt Programming
    Replies: 3
    Last Post: 4th October 2007, 18:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.