This question is very similar to the question asked here so I hope this isn’t too redundant.
I am new to T-SQL and I am trying to understand if there is a different/better approach to inserting records into two tables.
For example I have a form with name and address, in my DB this is represented with two tables ( client and address) on the most basic level if I wanted to insert the following:
Name fields (first, middle last age) to name table and address (street number, street name, city, state, zip) to address table with name being the primary table could someone point me in the right direction as to the best way to do this in SQL Server / T-sql? Or would it be the same as within MySQL syntax by inserting the person first and then selecting the last inserted user getting the id and then adding the address and id (as the fk) to the address table?
I’d also appreciate any links or additional reading on the best approach.
Cheers (and thanks in advance)
You need to do two inserts. After you do the first insert into the client table use @@identity to get the ID of the inserted record.