Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Inserting a new record into 2 tables at the same time

Inserting a new record into 2 tables at the same time

Scheduled Pinned Locked Moved Database
questiondatabaseadobetutorialannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    I took Jorgen's advice and modified my Movie database I split the table into 2, the first table being movie information and the 2nd table being FLV information So I can do the JOINs in displaying the data, and using it to create a Flash Movie Player, But it just dawned on me that I have no clue on how to insert and update records for my new setup here. So I came up with this. My question, am I on the right track here, Inserting the first table, getting the ID of the record so I can use the ID in the 2nd table? Or is there a better way in which I can insert in one shot? I'm not ready to write stored procedures yet, in case that is suggested.

    INSERT INTO MovieInfo(
    MovieName, MovieType, MoviePath, MoviePostage, MovieThumbnail, flv, h264
    )
    VALUES(
    @MovieName, @MovieType, @MoviePath, @MoviePostage, @MovieThumbnail, @flv, @H264
    );
    DECLARE @mID AS INT;
    SET @mID = (
    SELECT MovieID FROM MovieInfo WHERE MovieName=@MovieName
    )
    INSERT INTO MovieInfo_flv(
    movieID, flv_movieName, flv_skin, flv_folderpath, flv_filename
    )
    VALUES(
    @mID, @flv_movieName, @flv_skin, @flv_folderpath, @flv_filename
    )

    M 1 Reply Last reply
    0
    • J jkirkerx

      I took Jorgen's advice and modified my Movie database I split the table into 2, the first table being movie information and the 2nd table being FLV information So I can do the JOINs in displaying the data, and using it to create a Flash Movie Player, But it just dawned on me that I have no clue on how to insert and update records for my new setup here. So I came up with this. My question, am I on the right track here, Inserting the first table, getting the ID of the record so I can use the ID in the 2nd table? Or is there a better way in which I can insert in one shot? I'm not ready to write stored procedures yet, in case that is suggested.

      INSERT INTO MovieInfo(
      MovieName, MovieType, MoviePath, MoviePostage, MovieThumbnail, flv, h264
      )
      VALUES(
      @MovieName, @MovieType, @MoviePath, @MoviePostage, @MovieThumbnail, @flv, @H264
      );
      DECLARE @mID AS INT;
      SET @mID = (
      SELECT MovieID FROM MovieInfo WHERE MovieName=@MovieName
      )
      INSERT INTO MovieInfo_flv(
      movieID, flv_movieName, flv_skin, flv_folderpath, flv_filename
      )
      VALUES(
      @mID, @flv_movieName, @flv_skin, @flv_folderpath, @flv_filename
      )

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You either do this as a 2 statement operation or you create a stored procedure - the SP path is by far the simplest an most robust. I presume you have an IDENTITY field the Movie table. So when a record is inserted the system puts the new ID into a variable called @Scope_Identity you can get the value from there. I used stored procs for EVERYTHING, the UI/Service NEVER talks directly to a table/view, so I have a code generator to build my CRUD procs. So I am not going to be able to help with string inserts :-O

      Never underestimate the power of human stupidity RAH

      J 1 Reply Last reply
      0
      • M Mycroft Holmes

        You either do this as a 2 statement operation or you create a stored procedure - the SP path is by far the simplest an most robust. I presume you have an IDENTITY field the Movie table. So when a record is inserted the system puts the new ID into a variable called @Scope_Identity you can get the value from there. I used stored procs for EVERYTHING, the UI/Service NEVER talks directly to a table/view, so I have a code generator to build my CRUD procs. So I am not going to be able to help with string inserts :-O

        Never underestimate the power of human stupidity RAH

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        Well thanks for the confirmation on that. I guess I'll take a stab on stored procedures then.

        M 1 Reply Last reply
        0
        • J jkirkerx

          Well thanks for the confirmation on that. I guess I'll take a stab on stored procedures then.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          So here is some pseudo code for that. Declare incoming variables Insert into student table Get Scope_Identity into a variable Check for existing course - this may be done in the client Insert missing course Get Scope_Identity into another variable Insert variables into link table (this is a many to many table linking the course and the student records) Return the student record - I do this so the client has all the information about the student including the ID value.

          Never underestimate the power of human stupidity RAH

          J 1 Reply Last reply
          0
          • M Mycroft Holmes

            So here is some pseudo code for that. Declare incoming variables Insert into student table Get Scope_Identity into a variable Check for existing course - this may be done in the client Insert missing course Get Scope_Identity into another variable Insert variables into link table (this is a many to many table linking the course and the student records) Return the student record - I do this so the client has all the information about the student including the ID value.

            Never underestimate the power of human stupidity RAH

            J Offline
            J Offline
            jkirkerx
            wrote on last edited by
            #5

            OK I can swap out the words like student on that I'll write one and post it to see how I did on it. Thanks!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups