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];
Where:
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;
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!