Manoj Savalia
Manoj Savalia

Reputation: 1402

How to insert Master/Detail record in SQL Server?

I have three tables: Employee, Emp_Address and Emp_AddressDetail.

  1. Employee table is master and Emp_Address is detail.
  2. Emp_Address is master and Emp_AddressDetail is detail.

I want to copy all rows from a table to another table.

How can I do it?

Upvotes: 0

Views: 1011

Answers (2)

SWeko
SWeko

Reputation: 30912

If the tables have foreign keys defined, and those foreign key definitions need to be set on the new database, than you need to take a look at the data diagrams, and identify any tables that do not have foreign key columns. In your case the copy order will be Employee, Emp_Address, Emp_AddressDetail

If you do not have explicit SQL Server maintained foreign keys, or if the foreign keys are not set on the target database, you can just copy the data in any order you like.

Note that it's entirely possible to paint yourself into a corner, e.g. if there was field in the Employee table called PrimaryAddress that would be a foreign key to the Emp_Address table.

Upvotes: 1

Per Kastman
Per Kastman

Reputation: 4504

If I understand your question correct you want to insert data from one table to another? If so, you should have a look at the INTO statement. http://msdn.microsoft.com/en-us/library/ms188029.aspx

SELECT * INTO dbo.OneTable FROM Production.AnotherTable

Upvotes: 1

Related Questions