Insert from One Table to Another, both Tables are from Diffrent Database in SQL Server

Some times we need to query on two tables which are belong from two different database with same table name. Usually it needed when we want to fetch data from archive data. (ex. reports before 1 month, in that case we always put data to another database which are older then one month, if database is heavy. )

Two tables are given as :

 
Table1 in DATABAE_1
 

CREATE TABLE table1(

[date] [datetime] NULL,

[num] [varchar](50) NULL,

[status] [nvarchar](255) NULL,

[tid] [varchar](50) NULL

)
 

Table 1 in DATABAE_2
 

CREATE TABLE table1(

[date] [datetime] NULL,

[num] [varchar](50) NULL,

[status] [nvarchar](255) NULL,

[tid] [varchar](50) NULL,

[tname] [varchar](50) NULL,

)

 

Query to insert data from table1 of database_2 to table1 of database_1 as per your requirement in one shot.
 

INSERT INTO DATABAE_1.dbo.table1 ([date] ,[num] ,[status] ,[tid])

SELECT [date],[num],[status] ,[tid] FROM DATABAE_2.dbo.table1 

 

Ebook Download
View all
Learn
View all