Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Sirrombor
Starting Member
12 Posts |
Posted - 2002-07-01 : 15:25:42
|
| I am still somewhat new to SQL so please be kind to me :)I am using SQL SERVER 2000 and working with WebForms using C#...I have 2 tables that I am working with...TABLE_1TABLE_1_ID = primary key Identity Auto IncrementSTATUS = Some Insert ValuesTABLE_2TABLE_2_ID = primary key Identity Auto IncrementTABLE_1_ID - Foreign key from TABLE_1 TABLE_1_IDColumn1 = Some Insert ValuesColumn2 = Some Insert ValuesColumn3 = Some Insert ValuesI have just realized that I have no clue on how to insert a value into TABLE_1 which generates a unique ID and at the same time insert into TABLE_2 Values while relating the Table 2 values to the Unique ID that was generated in Table_1_ID...Any help will be greatly appreciated...... |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2002-07-01 : 15:29:31
|
| The inserts will have to be done as two separate commands. To maintain data integrity, you probably will want to explicitly declare a transaction. Do the INSERT to Table1 first, then retrieve the @@IDENTITY or SCOPE_IDENTITY into a variable and use that variable in the INSERT to table2.You can save yourself some headaches of you'll put this whole bit of code into a stored procedure and call the sproc from your C# code rather than trying to have C# make multiple calls. |
 |
|
|
Sirrombor
Starting Member
12 Posts |
Posted - 2002-07-01 : 16:09:50
|
| Its all clear now...Thank you I know just what I need to do :) |
 |
|
|
|
|
|