How to use SQL INSERT INTO SELECT Statement
By FoxLearn 2/19/2024 9:49:46 AM 378
It allows you to select data from one or more tables and insert it into another table in a single SQL statement. It's requires that the data types in source and target tables match
Here's the basic syntax of the INSERT INTO SELECT statement
INSERT INTO table2 (column1, column2, column3, ...) SELECT column1, column2, column3, ... FROM source_table WHERE condition;
- target_table
: The name of the table where you want to insert data.
- (column1, column2, ...)
: The columns in the target table where you want to insert data.
- source_table
: The name of the table from which you want to select data.
- column1, column2, ...
: The columns in the source table that you want to select data from.
- WHERE condition
: An optional condition that filters the rows to be copied from the source table.
sql server insert into select example
Suppose we have two tables: Customers and Persons.
We want to copy the data of persons from the Persons table into the Customers table.
INSERT INTO Customers (CustomerName, City, Country) SELECT Name, City, Country FROM Persons;
Run this query will help you copy all data from the persons table into the customers table
- How to fix 'The transaction log for the database is null due to OLDEST_PAGE'
- How to convert varchar to uniqueidentifier in SQL Server
- How to convert timestamp to date in SQL Server
- How to Download and Restore Northwind database to SQL Server
- How to Download Microsoft SQL Server
- Saving changes is not permitted in SQL Server
- How to change ‘Edit Top 200 Rows’ and ‘Select Top 1000 Rows’ in SQL
- How to fix 'The specified sa password does not meet strong password requirements'