Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6711303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:07:26+00:00 2026-05-26T08:07:26+00:00

My normal process for inserting into one table and then getting the ID back

  • 0

My normal process for inserting into one table and then getting the ID back so that I can insert into another table is like this in MSSQL:

DECLARE @transactionKey uniqueidentifier
SET @transactionKey = NEWID()

INSERT INTO transactions(transactionKey, transactionDate, transactionAmount)
    VALUES(@transactionKey, '#transactionDate#', '#transactionAmount#')

DECLARE @transactionID int
SELECT @transactionID = transactionID
    FROM transactions
    WHERE transactionKey = @transactionKey


INSERT INTO transactionItems(transactionID, itemID, itemAmount)
    VALUES(@transactionID, '#itemID#', '#itemAmount#')


SELECT @transactionID as transactionID

My question is 2 parts. First, is this the best way to do this? I read that there is a chance that the GUID changes on me and I end up with a invalid GUID in the second table. I am assuming the chances of that are very slim and I have been doing this for years on various projects and have never had a problem.

The second part of my question is does something like this work in MySQL? I am starting to work on a new project using MySQL and I am not exactly sure the best way to do this. I have normally only worked on MSSQL in the past.

I am using CF9 and MySQL on this new project.

Any help on this would be great.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:07:27+00:00Added an answer on May 26, 2026 at 8:07 am

    Part 1:
    I would personally not batch multiple statements within a single query to reduce the risk of SQL injection. This is a setting within your datasource on the ColdFusion administrator. Executing a stored procedure, which might be what you are doing(?), is another story, but, you should rephrase your question to “Get primary key after insert with mySQL Stored Procedure” if that is your intention.

    Part 2:
    ColdFusion, like many things, makes getting the primary key for a newly inserted record very easy–even if you are using auto-increment keys, GUIDs or something like Oracle’s ROWNUM. This will work on any almost every database supported by Adobe ColdFusion including MSSQL or MySQL. The only exception is the version of the databse–for example, MySQL 3 will not support this; however, MySQL 4+ will.

    <cfquery result="result">
      INSERT INTO myTable (
          title
      ) VALUES (
        <cfqueryparam value="Nice feature!" cfsqltype="cf_sql_varchar">
      )
    </cfquery>
    
    <--- get the primary key of the inserted record --->
    <cfset NewPrimaryKey = result.generatedkey>
    

    As of CF9+, you can access the new ID (for any database) using the generic key name:

    result.GENERATEDKEY    // All databases
    

    For CF8, different databases will have different keys within the results value. Here is a simple table to help I copied from the cfquery documentation.

    result.identitycol    // MSSQL
    result.rowid          // Oracle
    result.sys_identity   // Sybase
    result.serial_col     // Informix
    result.generated_key  // MySQL
    

    If you have any questions you can see a pretty dump as follows:

    <cfdump var="#result#" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A normal UserControl looks like this in XAML: <UserControl x:Class=mynamespace.foo ...namespaces...> <!-- content -->
On Windows, under normal circumstances a 32 bit process can only access 2GB of
I have a java program that runs another (Python) program as a process. Process
I know that normal mapping describes the process of adding detail to meshes without
I have a large XML document (50G+) to process. Loading this to normal dom
I have a function Process(data, f) where data is a normal parameter and f
Under normal circumstances, a VB.NET application of mine can check the ClientName environmental variable
Under normal circumstances you cannot move a workflow designed in SharePoint Designer to another
In normal mode I can hit Ctrl + E which deletes the rest of
Generating normal columnar data in excel file is quite easy but does any one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.