www.webdeveloper.com
Results 1 to 3 of 3

Thread: Inserting and Selecting in the same pass

  1. #1
    Join Date
    Mar 2014
    Posts
    8

    Inserting and Selecting in the same pass

    Hello,

    I am trying to create a new season, so the first this I do is insert the dates etc into the seasons table. I then select all from the seasons table order them in order to extract the ID for the last record, i.e the one I just created using my insert.

    Is there something problematic with doing this? The reason I ask is that if i create the next season and go onto do everything else in a single pass I get very random failures, so bits work some don't.

    If however I create the new season manually and then run the rest of the script then it all works just fine.

    Any ideas?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Depending on the DBMS being used and the application language, you may have a built-in function already available to get the last insert ID (e.g. mysql_insert_id() in PHP). Alternatively, your DBMS may support the use of a RETURNING clause in an INSERT statement, allowing you to return the value of the ID column for the just inserted row. E.g., in PostgreSQL:
    PHP Code:
    INSERT INTO table_name (col2col3VALUES ('foo''bar'RETURNING col1
    In that case, you then do a fetch after the insert query as if it were a select query, in order to get the returned column.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Depending on the DBMS being used and the application language, you may have a built-in function already available to get the last insert ID (e.g. mysql_insert_id() in PHP). Alternatively, your DBMS may support the use of a RETURNING clause in an INSERT statement, allowing you to return the value of the ID column for the just inserted row. E.g., in PostgreSQL:
    PHP Code:
    INSERT INTO table_name (col2col3VALUES ('foo''bar'RETURNING col1
    In that case, you then do a fetch after the insert query as if it were a select query, in order to get the returned column.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles