Inserting data from one table to another in SQL can be done in two ways:
1. Insert into XXXX select XXXX
2. Select XXXX into XXXX
1. Insert into XXXX select XXXX -
This method is useful when we two different databases exist and we have to export data form one to another.
USE [MCMER_DB_A]
GO
/**Object Table [dbo].[GS]**/
SET ANSI_NULLS ON
INSERT INTO [MCMER_DB_A].[Dime] (Col1, Col2, Col3, Col4, Col5)
SELECT Col1, Col2, Col3, Col4, Col5
FROM [MCMER_DB_B].[Dime]
GO
2. Select XXXX into XXXX
This method is used when the table in which data is to be copied doesn't exist.
When data is copied a new table is created with same datatypes as of existing one.
USE [MCMER_DB_A]
GO
/**Object Table [dbo].[HR]**/
SET ANSI_NULLS ON
/**Create and update**/
SELECT Col1, Col2, Col3, Col4, Col5
INTO [MCMER_DB_A].[HR]
FROM [MCMER_DB_B].[HR]
WHERE HRid = 1
/**Verify new table created**/
SELECT Col1, Col2, Col3, Col4, Col5
FROM [MCMER_DB_A].[HR]
GO
No comments:
Post a Comment