Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8270139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:28:48+00:00 2026-06-08T06:28:48+00:00

How can I get ID of last inserted row from SQL Server by PyQt4.QtSql

  • 0

How can I get ID of last inserted row from SQL Server by PyQt4.QtSql module? Now I’m using SQL Server 2012 Express, but program has to work also on SQL Server 2000.

Here is my code (Python + PyQt) and results:

from PyQt4.QtGui import QApplication
from PyQt4 import QtSql

app = QApplication([])
db = QtSql.QSqlDatabase.addDatabase("QODBC")
db.setDatabaseName('Driver={SQL Server Native Client 11.0};Server=(localdb)\\v11.0;')
db.open()

query = QtSql.QSqlQuery()
query.prepare("""CREATE TABLE Test(
    ID INT PRIMARY KEY IDENTITY(1, 1),
    Row nvarchar(255)
)
""")
query.exec_()

query = QtSql.QSqlQuery()
query.prepare('INSERT Test OUTPUT Inserted.ID VALUES(?)')
query.bindValue(0, 'Test')

query.exec_()

while query.next():
    last_inserted_id = query.value(0)

print('OUTPUT: ', last_inserted_id)
print('QSqlQuery.lastInsertId: ', query.lastInsertId())

query = QtSql.QSqlQuery('SELECT SCOPE_IDENTITY()')

while query.next():
    last_inserted_id_ = query.value(0)

print('SCOPE_IDENTITY: ', last_inserted_id_)

db.close()

Results:

OUTPUT:  1
QSqlQuery.lastInsertId:  None
SCOPE_IDENTITY:  <PyQt4.QtCore.QPyNullVariant object at 0x00000000032D88D0>

Unfortunately OUTPUT Clause is supported by SQL Server 2005 or above.

Python 3.2.3 (x64), PyQt 4.9.4, SQL Server 2012 Express

Any ideas?

Edit:

So far I use SELECT @@IDENTITY.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:28:52+00:00Added an answer on June 8, 2026 at 6:28 am

    Moving my comment to an answer to allow this to be closed cleanly:

    I don’t know Python, but I think SCOPE_IDENTITY() only works within a batch. So, you might want to add the ;SELECT SCOPE_IDENTITY() to the query with the INSERT. Hope this helps.

    So your Insert could look like:

    query = QtSql.QSqlQuery() 
    query.prepare('INSERT Test VALUES(?); SELECT SCOPE_IDENTITY()') 
    query.bindValue(0, 'Test') 
    
    query.exec_() 
    
    while query.next(): 
        last_inserted_id = query.value(0) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I fetch the last row that was inserted using DBI ( DBD::mysql
How can i get Last inserted row when in table there is no Uniqueidentifier
How can I get last year's start and end date using PHP code? Is
Can we get relationship status changes of our friends from last 3 days including
Is there any way I can get the last inserted ID if I am
I am using MySQL.I have to get the last inserted record's primary key value.
I want to get the entire row of a table in SQL Server, but
I need to get the last inserted row in a specific table for strictly
can you get the last or newly added element in std::set? for example say
How can I get the last key of an array?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.