| 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==-