I am trying to select some fields from one table and insert them into an existing table from a stored procedure. Here is what I am trying:
I think
SELECT ... INTO ...
is for temporary tables which is why I get an error that dbo.TableTwo
already exists.How can I insert multiple rows from
dbo.TableOne
into dbo.TableTwo
?DanielDaniel
5 Answers
SQL HOME SQL Intro SQL Syntax SQL Select SQL Select Distinct SQL Where SQL And, Or, Not SQL Order By SQL Insert Into SQL Null Values SQL Update SQL Delete SQL Select Top SQL Min and Max SQL Count, Avg, Sum SQL Like SQL Wildcards SQL In SQL Between SQL Aliases SQL Joins SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL Self Join SQL. SELECT (Transact-SQL); 5 minutes to read; Contributors. In this article. APPLIES TO: SQL Server (starting with 2008) Azure SQL Database Azure SQL Data Warehouse Parallel Data Warehouse Retrieves rows from the database and enables the selection of one or many rows or columns from one or many tables in SQL Server.
SELECT ... INTO ...
only works if the table specified in the INTO clause does not exist - otherwise, you have to use:This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:
OMG PoniesOMG Ponies
There are two different ways to implement inserting data from one table to another table.
For Existing Table - INSERT INTO SELECT
This method is used when the table is already created in the database earlier and the data is to be inserted into this table from another table. If columns listed in insert clause and select clause are same, they are not required to list them. It is good practice to always list them for readability and scalability purpose.
For Non-Existing Table - SELECT INTO
This method is used when the table is not created earlier and needs to be created when data from one table is to be inserted into the newly created table from another table. The new table is created with the same data types as selected columns.
Ref 12
Somnath MulukSomnath Muluk
Vinod PareekVinod Pareek
If you have used
select * into tablename from other tablenames
already, next time, to append, you say select * into existing table tablename from other tablenames
Verena_TechieVerena_Techie
If the destination table does exist but you don't want to specify column names:
slayernoahslayernoah