Skip to main content
August 6, 2010
Question

3 questions about unique identifiers and cftransaction

  • August 6, 2010
  • 1 reply
  • 759 views

I need to insert a row into table1 that has a unique identifier made by SQL.

Immediately after that, I need to insert a row into table2 that has the unique id from table1 in it. Table2 also has a unique id made for its rows by SQL.

Immediately after that, I need to insert a row into table3 that has the unique ids from table1 and table2 in it. Table3 doesn't have its own unique id.

This will happen thousands of times a day along with updates an deletes. This is a new way of doing things for me.

QUESTION 1: Is this the way to do it ?

<cftransaction name="firsttry">

   <cfquery name="queryname1" result="resultname1" datasource="datasourcename">

      insert into table1 (...) values (...)

   <cfquery>

   <cfset uniqueid1=resultname1.IDENTITYCOL>

   <cfquery name="queryname2" result="resultname2" datasource="datasourcename">

      insert into table2 (...uniqueid1...) values (...#uniqueid1#...)

   <cfquery>

   <cfset uniqueid2=resultname2.IDENTITYCOL>

   <cfquery name="queryname3" datasource="datasourcename">

      insert into table3 (...uniqueid1,uniqueid2...) values (...#uniqueid1#,#uniqueid2#...)

   <cfquery>

</cftransaction>

QUESTION 2: Are there any problems that come with using the automatically made unique ids or are they very dependable ?

QUESTION 3: Are there any problems that come with using cfttransaction or is it very dependable ?

    This topic has been closed for replies.

    1 reply

    Inspiring
    August 6, 2010

    Questions

    1. What database product and version are you using ( Microsoft SQL Server 2008, MySQL 5.1, etc)?

    2. By "unique identifier" do you mean a UUID or GUID or an autonumber AKA identity column?

    3. Can you post your database creation scripts and some sample data?

    Suggestion:

    Consider wrapping your database logic inside a stored procedure, if your database supports them, and calling the stored procedure inside a transaction.

    In response to your question #3.  I have not had any issues with CFTRANSACTION, but it's use depends on the database supporting transactions.  For example MS Access does not support database transactions.

    August 6, 2010

    1. Microsoft SQL Server 2008 R2.

    2. Identity Column.

    3. I don't know about the scripts but a small portion of the data looks something like

    table1 (a demographic)   table2 (enrollments)                             table3 (audit trail)

    id1   name         dob       id1   id2   addr        city      enrolled     action          id1   id2

    ----------------------------------   ---------------------------------------------------    --------------------------------

    1   John Smith 01/01/01  1     1     34 1 Ave   Perry   01/01/01    table1 insert  1

                                         1     2    34 1 Ave   Perry   02/02/02     table2 insert  1       1

    2   Ann Jones  02/05/01   2     3    43 8 St     Tampa 01/05/01    table2 insert   1       2

                                                                                                table1 insert   2

                                                                                                 table2 insert   2       3

    etc

    Table2 will be related to other tables in a 1 to many way also.

    Thanks for writing back and hope this helps some.

    Inspiring
    August 6, 2010

    In response to question #1:

    The code you posted looks OK.

    In response to question # 2:

    An identity column in MS SQL Server is serial as in 1,2,3,4... etc. and is unique to that table provided that the column has a unique or primary key constraint on it.

    In response to question #3:

    You shouldn't have a problem using CFTRANSACTION with MS SQL Server, database transactions are supported. You should avoid placing any non-database logic within the start and end of the CFTRANSACTION tags.

    You might also consider using CFQUERYPARAM in your SQL statements. This should improve the security and performance of your application.

    You might also ask your DBA to provide you with a stored procedure rather than requiring CF to run several INSERTs. Using a single stored procedure rather than 3 CFQUERY calls should give you a small performance improvement since all the database logic is run on the database server.