Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Get last Uniqueidentifier inserted in SQL Server table

Participant ,
Aug 16, 2011 Aug 16, 2011

I am looking for code that will get me the last entered ID in a table.  I am using SQL Server and have the ID's set as uniqueidentifiers.  I tried SCOPE_IDENTITY() and had an issue.  Also, want to make sure it is for that unique insert, not an insert that slipped in almost concurrently.

Thanks!

BCo

TOPICS
Database access
7.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Aug 17, 2011 Aug 17, 2011
I am looking for code that will get me the last entered ID in a table.  I am using SQL Server and have the ID's set as uniqueidentifiers.  I tried SCOPE_IDENTITY() and had an issue.  Also, want to make sure it is for that unique insert, not an insert that slipped in almost concurrently.

SCOPE_IDENTITY() only works with identity columns.  If your table does not contain an identity column that function will be of no use to you. Neither will cfquery's "result" attribute as it only works with ms sql

...
Translate
Explorer ,
Aug 16, 2011 Aug 16, 2011

Hi,

You can use result attribute of cfquery tag to get the id of last inserted record.

<cfquery name="testqry" datasource="somesource" result="restestqry">

    write query for insertion     

</cfquery>

<cfdump var="#restestqry#">

you will see the last inserted record id in dump result.

For details please look at documentation of coldfusion.

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Part_3_CFML_Ref_1.html

Thanks

saurav

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 17, 2011 Aug 17, 2011

Thanks for the answer, but I think I am looking for something without using CFDUMP and is in the SQL Statement.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 17, 2011 Aug 17, 2011

Alternately, if I use <cftransaction> around the input and the select queries am I guaranteed to get the last ID?

Thanks again!

B.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 17, 2011 Aug 17, 2011

The suggestion for using cfdump was to show you what information was available and to give you a hint as to the name of the variable with the information you seek.  It was not intended for your production code.

Regarding your other post, cftransaction will do some things for you, but guaranteeing that you get the correct record ID is not one of them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 17, 2011 Aug 17, 2011
I am looking for code that will get me the last entered ID in a table.  I am using SQL Server and have the ID's set as uniqueidentifiers.  I tried SCOPE_IDENTITY() and had an issue.  Also, want to make sure it is for that unique insert, not an insert that slipped in almost concurrently.

SCOPE_IDENTITY() only works with identity columns.  If your table does not contain an identity column that function will be of no use to you. Neither will cfquery's "result" attribute as it only works with ms sql identity values too.

AFAIK, there is no built in method for automatically returning a newly created uniqueidentifier.  The closest you can get in MS SQL 2005, without involving triggers, is using an OUTPUT clause.

     SET NOCOUNT ON
     INSERT INTO yourTable ( ColumnA, ColumnB )
     OUTPUT inserted.yourIDColumn
     VALUES ('foo', 'bar')

     SET NOCOUNT OFF

Message was edited by: -==cfSearching==-

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 18, 2011 Aug 18, 2011

I went with a CF solution.  What do you think about IDENITITYCOL?  It seems to be working.  But my question is whether the DB is locked and will get the correct ID or if it is possible another one could be entered before the query to get the "ID".

SELECT itemid
FROM items
WHERE itemid = #insertitemid['IDENTITYCOL']#

Thanks again everyone for the help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 18, 2011 Aug 18, 2011

SELECT itemid

FROM items

WHERE itemid = #insertitemid['IDENTITYCOL']#

I am assuming that is a typo. Because there is no reason to re-SELECT the last id when you already have it 😉

But my question is whether the DB is locked and will get the

correct ID or if it is possible another one could be entered

before the query to get the "ID".

There is no locking of the database. IIRC the result attribute of cfquery returns the correct ID by using scope_identity() internally.

http://msdn.microsoft.com/en-us/library/ms190315.aspx

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 19, 2011 Aug 19, 2011
LATEST

Thanks everyone for the help!  All is working well now.

BCo.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources