INSERT INTO SQL statement is used to insert data from one database table to another. The INSERT INTO can be used to transfer data from one database to other database or between two tables in the same database.
Let's take an example. Here are two database tables in two different databases.
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,
- )
The tables can be same name or different names.
Here is the SQL query to insert data from table1 of database_2 to table1 of database_1.
- INSERT INTO DATABAE_1.dbo.table1 ([date] ,[num] ,[status] ,[tid])
- SELECT [date],[num],[status] ,[tid] FROM DATABAE_2.dbo.table1
The INSERT INTO SQL statement is followed by the databasename.tablename ( column names) SELECT (column names) FROM databasename.tablename.
You can execute this table direct on a SQL Sever or from code.
Here is a detailed tutorial on INSERT INTO with SQL examples: