sql help
-
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 -
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
ThanksFirst, 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
-
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