Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add relational data from multiple table
like assume that i have two table
tbl_emp and tbl_empaddress

tbl_emp
'''''''''''''''''
empid PK (auto increment) empname

2 Debashis
3 Amit
5 Abc

tbl_empaddress
'''''''''''''''''
addressid PK (auto increment) empid Address

1 2 Kolkata
2 2 Pune
3 3 Delhi
4 5 Ranchi
5 2 Abc


I want to add data in another database same table name and same column name

Data should be display like

tbl_emp
'''''''''''''''''
empid PK (auto increment) | empname

1 Debashis
2 Amit
3 Abc

tbl_empaddress
'''''''''''''''''
addressid PK (auto increment) | empid | Address

1 1 Kolkata
2 1 Pune
3 2 Delhi
4 3 Ranchi
5 1 Abc

Please help

Thanks
Debashis
Posted

1 solution

Only difference from your usual INSERT statement is that you add database (and schema) prefix.
SQL
INSERT INTO [Database1].[schema_Name].[TableXYZ]
SELECT 
  Column1
, Column2
, Column3
....
, ColumnN 
FROM [Database2].[schema_Name].[TableXYZ]

-- example with your tbl_empaddress

INSERT INTO [Database1].[dbo].[tbl_empaddress]
SELECT empid, Address
FROM [Database2].[dbo].[tbl_empaddress]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900