Insert query.

Discussion in 'Programming' started by Hercf, Jun 15, 2008.

  1. #1
    Hi everybody!
    Could you teach me how to get back the “primary key”: (“id”), from an “insert query”?

    *
    <cffunction name="insertData" access="public">
    <cfargument name="formData" type="struct" required="yes">

    <cfquery name="qInsert" datasource="mydata">
    INSERT INTO mytable
    (email, firstname, name)
    VALUES
    ('#formData.email#', '#formData.firstname#', '#formData.name#')
    </cfquery>
    </cffunction>
    *
    How could I get back the primary key:”id” and send it to another page?
    Thanks a lot!
     
    Hercf, Jun 15, 2008 IP
  2. CR250

    CR250 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <cfquery name="qInsert" datasource="mydata" result="queryResult">

    <cfset myPrimaryKey = queryResult.IDENTITYCOL />

    I use msSQL its different for different DB's. Coldfusion 8 only

    http://www.forta.com/blog/index.cfm/2007/7/6/ColdFusion-8-Can-Return-Identity-Values
     
    CR250, Jun 15, 2008 IP
  3. Hercf

    Hercf Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi CR250,
    Thank for tips and the path you provided to me!
    Hercf.
     
    Hercf, Jun 19, 2008 IP
  4. JasonBartholme

    JasonBartholme Peon

    Messages:
    396
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello Hercf,

    I use the following to get the lastID. Hope this helps:

    <cfquery name="INSERTCustomer" datasource="#APPLICATION.DSN#">
    INSERT INTO TableName
    (Fieldname)
    VALUES
    ('Value');
    SELECT LastID = @@identity
    </cfquery>
     
    JasonBartholme, Jun 20, 2008 IP
  5. Hercf

    Hercf Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you All !
    H.
     
    Hercf, Jun 29, 2008 IP