• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Inserting records in two different tables at the same time?

Community Beginner ,
Dec 08, 2015 Dec 08, 2015

Copy link to clipboard

Copied

Hello everyone, I have question about inserting records in two different tables at the same time, what I'm looking for is passing unique id that gets created in first insert statement to second insert statement. Example for this problem:

<cfquery name="addRecords1" datasource="test">

      Insert Into Table1(Name, Date, Age)

      Values (<cfqueryparam cfsqltype="cf_sql_char" value="#arguments.Name#">,

                  <cfqueryparam cfsqltype="cf_sql_date" value="#arguments.Date#">,

                  <cfqueryparam cfsqltype="cf_sql_int" value="#arguments.Age#">);

      Select SCOPE_IDENTITY() As RecID;

  </cfquery>

  <cfquery name="addRecords2" datasource="test">

       Insert Into Table2(Company,City,Date,ID)

       Values(<cfqueryparam cfsqltype="cf_sql_char" value="#arguments.Company#">,

                  <cfqueryparam cfsqltype="cf_sql_char" value="#City#">,

                  <cfqueryparam cfsqltype="cf_sql_date" value="#Date#">,

                  <cfqueryparam cfsqltype="cf_sql_int" value="How to pass RecID to be inserted in this table???">);

  </cfquery>

 

In this example I'm inserting records in table 1 and creating SCOPE IDENTITY as RecId. I would like to pass that id and insert in my table 2. That Id I will use  in my second table as ID. If anyone knows anything about this please let me know. Thank you.

Views

290

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 08, 2015 Dec 08, 2015
<cfqueryparam cfsqltype="cf_sql_int" value="How to pass RecID to be inserted in this table???">);


QueryName - DOT - ColumnName, so it should be:


<cfqueryparam cfsqltype="cf_sql_int" value="#addRecords1.RecID#">);


HTH,


^_^

Votes

Translate

Translate
LEGEND ,
Dec 08, 2015 Dec 08, 2015

Copy link to clipboard

Copied

<cfqueryparam cfsqltype="cf_sql_int" value="How to pass RecID to be inserted in this table???">);


QueryName - DOT - ColumnName, so it should be:


<cfqueryparam cfsqltype="cf_sql_int" value="#addRecords1.RecID#">);


HTH,


^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 08, 2015 Dec 08, 2015

Copy link to clipboard

Copied

LATEST

Thank you for marking my answer correct.  I really appreciate it.

V/r,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation