Skip to main content
Known Participant
April 30, 2010
Question

How insert into 2 tables.

  • April 30, 2010
  • 1 reply
  • 994 views

Hi ,

I have 2 tables like this:

Table 1:

CustomerID (auto number)

other feilds,

Table2:

EventID(auto number)

CustomerID

other feilds,

I would like to insert a record into these two tables. I am using CFC to insert, I have problem to insert CustomerID into the second table. I really appreciate if you can help me.

<cfcomponent>
       <cffunction name="Add" access="public" returntype="void"> 
               <cfargument name="formData" type="struct" required="yes">
          <cfquery datasource="DB">
 
     INSERT INTO dbo.Table 1
     (
     other feilds

  )

VALUES                   
         ('#formData.feildsName#'
        
         )
   
</cfquery>

<cfquery name="qGetID" datasource="DB">
SELECT MAX(CustomerID ) AS ID
FROM table1
</cfquery>

<cfquery datasource="DB">
         INSERT INTO dbo.table2
             (
           
          ID ,

          other feilds

            )

          VALUES                            
        ( '#ID#',

        ('#formData.feildsName#'

        )

Thanks.

    This topic has been closed for replies.

    1 reply

    Inspiring
    April 30, 2010

    The short answer is to qualify your id variable with the query name.  In this case, that would be qGetID.

    However, there are better ways to get that value.  The details depend on the database and cold fusion version you are using.  If you do have to use the select max(id) method, add a where clause to ensure that you really do get the record you want.

    nikoo560Author
    Known Participant
    April 30, 2010

    I tried this it gave me this error.

    The expression has requested a variable or an intermediate expression result as a simple value. However, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values.

    Inspiring
    April 30, 2010

    add the row number