Skip to main content
Known Participant
May 6, 2010
Question

SELECT Scope_Identity() AS ID not working?

  • May 6, 2010
  • 1 reply
  • 4966 views

Hi all,

I've had this code in an application for a while and it stopped working when my server was migrated to CF8 from CF7. It's writing to a SQL Server 2005 DB.

<cfquery  name="insertGame"

          datasource="#application.datasource#"

          username="#application.datasource_username#"

          password="#application.datasource_password#">

          INSERT INTO cdw_games(gameName)

          VALUES ('#FORM.gameName#')

          SELECT Scope_Identity() AS ID

</cfquery>

<cfloop index = "loopCount" from="1" to="10">

<cfquery  name="insertTeams"

          datasource="#application.datasource#"

          username="#application.datasource_username#"

          password="#application.datasource_password#">

          INSERT INTO cdw_teams(teamName, gameID)

          VALUES ('Team#loopCount#','#insertGame.ID#')

</cfquery>

I'm getting a 500 error saying that "Element ID is undefined in insertGame." Any idea why this is happening?

I also tried adding "result=newGameID" to the cfquery tag, then referenced newGameID.IDENTITYCOL and got the same error, element IDENTITYCOL is undefined in newGameID.

This is hosted on goDaddy, so I'm assuming it's something they did because it's been working for a number of years now.

Any ideas?

Thanks,

Keith

    This topic has been closed for replies.

    1 reply

    Inspiring
    May 6, 2010

    You can remove the "SELECT Scope_Identity() AS ID" from your insertGame query.  Getting the newly created ID value is automatically handled by CF8.   You can access the new value with #insertGame.identityCol#.

    See CFQUERY documentation:
    http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_p-q_17.html#1102316
    khomelAuthor
    Known Participant
    May 6, 2010

    I did mention in my post that I tried that (actually you need to reference identitycol from the result attribute...see the documentation). Didn't work.

    Inspiring
    May 6, 2010

    khomel wrote:

    I did mention in my post that I tried that (actually you need to reference identitycol from the result attribute...see the documentation). Didn't work.

    This does not match the code you posted.  Did you remove your SELECT SCOPE_IDENTITY call when using identitycol?