Home > Database > Mysql Tutorial > How to Insert Multiple Rows from One Table into Another Using SQL Server?

How to Insert Multiple Rows from One Table into Another Using SQL Server?

Susan Sarandon
Release: 2025-01-04 17:42:39
Original
767 people have browsed it

How to Insert Multiple Rows from One Table into Another Using SQL Server?

Inserting Rows into Existing Tables Using SQL Server

When working with SQL Server, you may encounter the need to insert data from one table into an existing table. One method you may consider is using the SELECT ... INTO ... statement. However, as you discovered, this approach is only applicable for temporary tables. To insert multiple rows into an existing table, you can utilize the INSERT INTO statement.

INSERT INTO Syntax:

The INSERT INTO statement follows the syntax below:

INSERT INTO [TableName]
([Column1], [Column2], ...)
SELECT [Expression1], [Expression2], ...
FROM [SourceTable]
WHERE [Condition];
Copy after login

Where:

  • [TableName] specifies the name of the existing table to insert data into.
  • [Column1], [Column2], ... (optional) specifies the specific columns in the existing table to insert data into.
  • [Expression1], [Expression2], ... specifies the values to insert into the corresponding columns in the existing table.
  • [SourceTable] specifies the source table from which data will be selected.
  • [Condition] (optional) specifies the criteria for selecting rows from the source table.

Inserting Rows from dbo.TableOne into dbo.TableTwo:

In your specific case, you want to insert rows from dbo.TableOne into dbo.TableTwo. Assuming the destination table has two columns, col1 and col2, the following statement will accomplish this:

INSERT INTO dbo.TableTwo
(col1, col2)
SELECT col1, col2
FROM dbo.TableOne
WHERE col3 LIKE @search_key;
Copy after login

This statement inserts selected rows from dbo.TableOne into dbo.TableTwo based on the value specified in the @search_key parameter. Note that you need to specify the column names in the INSERT INTO statement if the destination table contains more than two columns.

The above is the detailed content of How to Insert Multiple Rows from One Table into Another Using SQL Server?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template