

- TSQL – Getting New Primary Key
- Blog
- TSQL – Getting New Primary Key
There are so many ways to getting a Primary Key from a table right after an INSERT, which should you use? Trick question. You get it when you do the INSERT, not after.
Resources
There are so many ways to getting a Primary Key from a table right after an INSERT, which should you use? Trick question. You get it when you do the INSERT, not after.
Resources

- Most Recent
- Most Relevant
How about using <cfquery name=”myQuery” result=”myQuery_Result”> and then calling #myQuery_Result.generatedKey#?
I tried to see if I could get to work. I made a query, but that field is not available in my result. It does not seem to be universally available.
If it would have worked, I would have ran
SELECT top 100 deqs.last_execution_time AS [Time], dest.TEXT AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC
To see what query it did to get the results
~~~~
If someone gets this to work, I would like to know what it does.
Interesting!
I’ve never experienced a problem getting the generatedKey to appear in the results, but apparently it can happen. I’ve just never run across it.