Skip to main content
Inspiring
November 4, 2010
Question

Inserting latest USERID into multiple tables

  • November 4, 2010
  • 1 reply
  • 2786 views

Hello

I have a DB question:

Query 1 Inserts new member info into table1 (MEMBERID is my index)

I then have query 2 that inserts the members interests into table2. I need to keep MEMBERID from QUERY 1 and insert it into table2

Please can someone give me a safe way to do this? Would CFTRANSACTION do the trick?

If so... how would I use it?

Thanks

    This topic has been closed for replies.

    1 reply

    existdissolve
    Inspiring
    November 4, 2010

    Take a look at the docs for the cfquery tag: http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_p-q_17.html#1102316

    Under "usage", you'll see a section for using the "result" attribute to return the id of an inserted row.  Based on what you said in your post, this would probably do the trick.  Of course, there are SQL-based ways of doing this too, but I'd start with the cfquery docs.

    Example:

    <cfquery name="myquery" datasource="mydsn" result="myresult">

         insert into thetable (stuff, things, whatever)

         values (

              <cfqueryparamm value="#stuff#" cfsqltype="cf_sql_varchar">,

              <cfqueryparamm value="#things#" cfsqltype="cf_sql_varchar">,           <cfqueryparamm value="#whatever#" cfsqltype="cf_sql_varchar">

         )

    </cfquery>

    <cfquery name="secondquery" datasource="mydsn">

         insert into othertable(id,interest,whatever)

         values (

              <cfqueryparamm value="#myresult.identitycol#" cfsqltype="cf_sql_integer">, <!---sql server--->

              <cfqueryparamm value="#interest#" cfsqltype="cf_sql_varchar">,

              <cfqueryparamm value="#whatever#" cfsqltype="cf_sql_varchar">

         )

    </cfquery>

    NOTE: The result_name.identitycol is database specific.  So whether you're using MySQL, SQL Server, Oracle, etc., the syntax will be a bit different.  Be sure to check the docs to use the one that will work for your environment.

    Re: adding the transaction, that wouldn't be a bad idea, although in and of itself it won't accomplish the new id inserting into the second table.  All the transaction will do is allow you set isolation levels on the db transactions and rollback the transactions if the whole process doesn't complete properly.

    Inspiring
    November 4, 2010

    Thanks... makes total sense... BUT...

    Would the result name in the query not need to be unique?

    If there were 10 simultaneous inserts at the exact same time, wouldn’t the result need to be unique?

    Would CFTRANSACTION do the trick?

    Look forward to your reply.

    Delon Cheketri | Mobile: +27 83 406 4890 | Tel: +27 (0) 11 447 1777

    www.fusebox.co.za | delon@fusebox.co.za

    existdissolve
    Inspiring
    November 4, 2010

    I could be wrong, but I don't think that the result name from the query needs to be unique.  As far as I understand it, each hit of the code would be a separate page request, so access to the query result variable would be limited to that same request, not other requests to the same page (since the query result doesn't live beyond the request).

    Morever, cftransaction is used for ensuring that multiple queries executed in succession complete as a group, or are rolled back within the one transaction if something goes wrong. If you were looking to lock access to a shared data structure, you'd want to look at cflock.  However, for this example, I don't think cflock is necessary or appropriate.

    But again, I could be wrong.  Let's see if anyone else chimes in to correct me