[thelist] Using @@Identity

Joshua Olson joshua at waetech.com
Tue Aug 23 19:22:30 CDT 2005


It seems that a lot of people don't do this, but here is something I
personally prefer:

Add a trigger to the table on insert:

CREATE TRIGGER mytable_insert ON dbo.mytable
FOR INSERT
AS SELECT @@identity AS id

Then, whenever you insert a record you'll automatically get back a recordset
containing the inserted identity.

In CF, this is really slick.. you can do something like this:

<cfquery name="queryname">
  INSERT INTO mytable
  ...
</cfquery>

<cfoutput>Inserted ID: #queryname.id#</cfoutput>

In ASP you can do something very similar:

Set oRS = oConn.Execute("INSERT INTO mytable...")

Response.Write "Inserted ID: " & oRS("id")

etc...

<><><><><><><><><><>
Joshua L. Olson
WAE Tech Inc.
http://www.waetech.com/
Phone: 706.210.0168 
Fax: 413.812.4864

Monitor bandwidth usage on IIS6 in real-time:
http://www.waetech.com/services/iisbm/




More information about the thelist mailing list