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. Web Development
  3. ASP.NET
  4. sql help

sql help

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorial
3 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.
  • A Offline
    A Offline
    Aljaz111
    wrote on last edited by
    #1

    I would like to know how to add data into 2 tables in one procedure. I have table1 and table2. Now i insert data to first table, but i have problems with inserting ID(auto-increment) of first table into second table. I have procedure like this but it doesn't work: ALTER procedure dbo.insert_test //// @value1, @value2, @value3 /// as declare @table_1 int insert into table1(value1,value2,value3) values(@value1,@value2,@value3) SET @table_1 = (SELECT table1.ID FROM table1) --> table1.ID is primary key insert into table2(table_1, value2) values(@table_1, @value2) --> @value2 is same as for table1 Thanks

    N 1 Reply Last reply
    0
    • A Aljaz111

      I would like to know how to add data into 2 tables in one procedure. I have table1 and table2. Now i insert data to first table, but i have problems with inserting ID(auto-increment) of first table into second table. I have procedure like this but it doesn't work: ALTER procedure dbo.insert_test //// @value1, @value2, @value3 /// as declare @table_1 int insert into table1(value1,value2,value3) values(@value1,@value2,@value3) SET @table_1 = (SELECT table1.ID FROM table1) --> table1.ID is primary key insert into table2(table_1, value2) values(@table_1, @value2) --> @value2 is same as for table1 Thanks

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      First, there is a database forum here which would have been a better place to ask this than the ASP.NET forum don't you think? I'm assuming you are trying are trying to join these two tables via the table_1 column.

      SET @table_1 = (SELECT table1.ID FROM table1

      ) This will only return the first id in this table, not that last one inserted. This what you want.

      SET @table_1 = SCOPE_IDENTITY()


      I know the language. I've read a book. - _Madmatt

      A 1 Reply Last reply
      0
      • N Not Active

        First, there is a database forum here which would have been a better place to ask this than the ASP.NET forum don't you think? I'm assuming you are trying are trying to join these two tables via the table_1 column.

        SET @table_1 = (SELECT table1.ID FROM table1

        ) This will only return the first id in this table, not that last one inserted. This what you want.

        SET @table_1 = SCOPE_IDENTITY()


        I know the language. I've read a book. - _Madmatt

        A Offline
        A Offline
        Aljaz111
        wrote on last edited by
        #3

        That works fine, 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