SQL Server > Query

Transfer from one table to another in SQL Server

How to transfer data from one table to another in SQL Server?


Transferring data from one table to another table, we can use INSERT INTO statement provided the data type of both table columns are same.

In this example, we have following data structure of both tables and we shall try to insert FirstName, LastName column data of PersonalDetails into FullName and City of MyAccounts (Do not worry, the data is not making sense, however our demonstration purpose will be solved).

Execute following query in the Query window

INSERT INTO MyDetails (FullName, City)
SELECT FirstName, LastName FROM PersonalDetails

As the data type of both columns from both database tables are same so it will select FirstName and LastName from PersonalDetails table and insert into MyDetails table.

For conditional insertion, we can put WHERE clause in the SELECT statement above.

INSERT INTO MyDetails (FullName, City)
SELECT FirstName, LastName FROM PersonalDetails
WHERE FirstName LIKE 'S%'

Above query will select only those records from the PersonalDetails table whose FirstName starts with “S” and insert into MyDetails database table.

 Views: 1609 | Post Order: 60



Write for us