Good day,
I have a little witty problem. Say, I inserted a row with some data in a table where a primary key is present. How would one "SELECT" the primary key of the row one just inserted?
Thanks in advance.
~Insomnia
For MS SQL Server:
|
|||||||||||
|
For SQL Server 2005 and up, and regardless of what type your primary key is, you could always use the
|
|||
|
For MySQL, use LAST_INSERT_ID() http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html You should also be able to start a transaction, insert the row, and select the row using some field that has a unique value that you just inserted, like a timestamp or guid. This should work in pretty much any RDBMS that supports transactions, as long as you have a good unique field to select the row with. |
|||
|
MS SQL You can use @@IDENTITY. After an insert statement, you can run:
This will give you the primary key of the record you just inserted. If you are planning to use it later, I suggest saving it:
If you are using this in a stored procedure and want to access it back in your application, make sure to have nocount off. |
|||||
|
For Postgresql:
Source: http://stackoverflow.com/questions/2944297/postgresql-function-for-last-inserted-id |
|||
|