User Tools

Site Tools

Action disabled: source

wiki:software:code:cfm:mssql

Coldfusion 8 and Microsoft SQL 2005 : @@IDENTITY

This article was originally published on November 12, 2008.

There’s a handy little query you can run in SQL to retrieve the identity (primary key) value of a record you just created. So, you can do something like this:

INSERT INTO
  People (
    Name
  ) VALUES (
    'Joe'
  );
  SELECT @@IDENTITY AS NewName;

This query will create a new person and also return the identity value of the newly created record. It can be useful for all kinds of things – I use it frequently.

We recently upgraded to ColdFusion 8 here at Purdue, and my code started breaking.

It turns out that CF8 attempts to “help” you by automatically running SELECT @@IDENTITY after any INSERT query. This is problematic in two ways – it cancels out your own @@IDENTITY query, and it arbitrarily names the result Generated_Keys.

So instead of being able to do something like this:

<cfset TheNewName = People.NewName>

You must use this:

<cfset TheNewName = People.Generated_Keys>

It’s really more annoying than anything else. It seems like a good idea to include the code automatically, but I think it would probably be a little better if there were a conditional in there that prevents CF8 from inserting its automatic query if a SELECT @@IDENTITY query is already present.

Articles in this section

  • ColdFusion: Browser identification in web applicationsplugin-autotooltip__default plugin-autotooltip_bigColdFusion: Browser identification in web applications

    I’m really, annoyingly, obsessively anal about browser compatibility in web design. Even if you’re convinced that none of your users will ever touch Opera, there’s no reason to not make sure your site is usable in Opera…or Lynx, or IE 5.5, or anything else.
  • Internet Explorer and images as form input controlsplugin-autotooltip__default plugin-autotooltip_bigInternet Explorer and images as form input controls

    I like to provide clear visual or textual cues for an application. In the admin interface for one of my apps, I provide a pretty easy way of rejecting or accepting form submissions for a class – a red “X” to reject, and a green checkmark to accept. In order to pass these actions to the ColdFusion page on the server side and rely on
  • Coldfusion 8 and Microsoft SQL 2005 : @@IDENTITYplugin-autotooltip__default plugin-autotooltip_bigColdfusion 8 and Microsoft SQL 2005 : @@IDENTITY

    There’s a handy little query you can run in SQL to retrieve the identity (primary key) value of a record you just created. So, you can do something like this:

    INSERT INTO People ( Name ) VALUES ( 'Joe' ); SELECT @@IDENTITY AS NewName;

    cfm index
  • ColdFusion: Generating and parsing RSS feedsplugin-autotooltip__default plugin-autotooltip_bigColdFusion: Generating and parsing RSS feeds

    One of my current projects at work has an RSS requirement. I’m redesigning the university’s policies website. My customers are concerned that other departments at Purdue have previously copied the content of a policy and placed it on a different site, which is problematic when policies are revised, superseded, or retired.
  • ColdFusion: Encrypting sensitive data in URL stringsplugin-autotooltip__default plugin-autotooltip_bigColdFusion: Encrypting sensitive data in URL strings

    On one of my current projects, I’ve found myself in need of passing sensitive data through a URL string. I could use an individual form for each link and pass it via POST, but I wanted to see what I might be able to do by way of encrypting the data. Turns out it’s quite easy.