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. insert record into two joined tables

insert record into two joined tables

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
5 Posts 3 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.
  • M Offline
    M Offline
    MarkyMark
    wrote on last edited by
    #1

    This is on SQL Server 2000, I don't think it's possible, but maybe there is some way of doing it, or another better idea? If I have two tables, and I want to insert a new record into both of them, and at the same time set a field in one table to be the identity inserted field in the other, can that be done? e.g. the SQL I'd want to work would be for example: insert into tablea inner join tableb on tablea.bid = tableb.id( a, b, c, bid ) values( 1, 2, 3, tableb.id)

    M M 2 Replies Last reply
    0
    • M MarkyMark

      This is on SQL Server 2000, I don't think it's possible, but maybe there is some way of doing it, or another better idea? If I have two tables, and I want to insert a new record into both of them, and at the same time set a field in one table to be the identity inserted field in the other, can that be done? e.g. the SQL I'd want to work would be for example: insert into tablea inner join tableb on tablea.bid = tableb.id( a, b, c, bid ) values( 1, 2, 3, tableb.id)

      M Offline
      M Offline
      mr_12345
      wrote on last edited by
      #2

      use a stored procedure.

      M 1 Reply Last reply
      0
      • M mr_12345

        use a stored procedure.

        M Offline
        M Offline
        MarkyMark
        wrote on last edited by
        #3

        OK, thanks, that's a good idea. But the question still remains about the SQL to use in the stored procedure.

        1 Reply Last reply
        0
        • M MarkyMark

          This is on SQL Server 2000, I don't think it's possible, but maybe there is some way of doing it, or another better idea? If I have two tables, and I want to insert a new record into both of them, and at the same time set a field in one table to be the identity inserted field in the other, can that be done? e.g. the SQL I'd want to work would be for example: insert into tablea inner join tableb on tablea.bid = tableb.id( a, b, c, bid ) values( 1, 2, 3, tableb.id)

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          You can retrieve the value of the identity column from the last insert performed using the @@IDENTITY variable. Due to the action of triggers, however, this can be the wrong value if one or more triggers that fired also performed an insert. On SQL Server 2000 and later the SCOPE_IDENTITY() function is a better choice. So something like:

          INSERT INTO tableb( c )
          VALUES( 3 )

          INSERT INTO tablea( a, b, bid )
          VALUES( 1, 2, SCOPE_IDENTITY() )

          Stability. What an interesting concept. -- Chris Maunder

          M 1 Reply Last reply
          0
          • M Mike Dimmick

            You can retrieve the value of the identity column from the last insert performed using the @@IDENTITY variable. Due to the action of triggers, however, this can be the wrong value if one or more triggers that fired also performed an insert. On SQL Server 2000 and later the SCOPE_IDENTITY() function is a better choice. So something like:

            INSERT INTO tableb( c )
            VALUES( 3 )

            INSERT INTO tablea( a, b, bid )
            VALUES( 1, 2, SCOPE_IDENTITY() )

            Stability. What an interesting concept. -- Chris Maunder

            M Offline
            M Offline
            MarkyMark
            wrote on last edited by
            #5

            That's great, thanks Mike!

            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