copy data from one table to another existing table using sql - sql

I know we could use
Insert Into table_name(cloumn_names)
Select column_names
From table_name
but how about Select Into? Can we also use Select Into to copy data from one table to another EXISTING table using? I mean can we specify the column names of the latter table using Select Into?
Thx!

You can use insert into select syntax
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
It basically selects columns from table1 and respectively insert in table 2

SELECT INTO
This method is used when table is not created earlier and needs to be created when data from one table is to be inserted into newly created table from another table. New table is created with same data types as selected columns.
Inserting Rows by Using SELECT INTO

You can try this :
UPDATE [TestDB].[dbo].[Table1]
SET [Content] = (
Select [TestDB].[dbo].[Table2].[Content] from Table2 where ID= 'XYZ')
WHERE [ID] = 'XYZ'
GO

Related

Create additional columns when using "Create Table As"

I am creating a table using the CREATE TABLE AS Statement. Script looks like this :
CREATE TABLE table2
AS
SELECT column1, column2, ..., columnN
FROM table1
WHERE ROWNUM <= 50;
My question is, can I create additional columns on table2 that don't exist in table1 inside the CREATE TABLE AS Statement or do I have to resort to ALTER afterwards?
EDIT: For example table1 contains ID, FULLNAME, STATUS and I wanna add a column somewhere in between called AGE
I am using Oracle Sql
You can add whatever you'd like to the SELECT
CREATE TABLE table2
AS
SELECT column1, column2, ..., columnN,
trunc(months_between(birth_date,sysdate)/12) age,
'Some string' another_column
FROM table1
WHERE ROWNUM <= 50;

duplicate rows using insert into

I have a table that contains data and I want to use insert into statement
here is my query
INSERT INTO [table]
SELECT Distinct * FROM [DataSource]
how to to ignore a row from being inserted if it's already found in the table?
Assuming your target table and your data source table have identical structure, you could do something like this:
INSERT INTO [table]
SELECT Distinct *
FROM [DataSource]
EXCEPT
SELECT *
FROM [table]
Alternatively, you could use MERGE statement or SELECT ... WHERE NOT EXISTS.

Copy data into another table

How to copy/append data from one table into another table with same schema in SQL Server?
Edit:
let's say there is a query
select *
into table1
from table2
where 1=1
which creates table1 with the same schema as well as data as in table2.
Is there any short query like this to only copy entire data only into an already existing table?
If both tables are truly the same schema:
INSERT INTO newTable
SELECT * FROM oldTable
Otherwise, you'll have to specify the column names (the column list for newTable is optional if you are specifying a value for all columns and selecting columns in the same order as newTable's schema):
INSERT INTO newTable (col1, col2, col3)
SELECT column1, column2, column3
FROM oldTable
Simple way if new table does not exist and you want to make a copy of old table with everything then following works in SQL Server.
SELECT * INTO NewTable FROM OldTable
This is the proper way to do it:
INSERT INTO destinationTable
SELECT * FROM sourceTable
INSERT INTO table1 (col1, col2, col3)
SELECT column1, column2, column3
FROM table2
Try this:
INSERT INTO MyTable1 (Col1, Col2, Col4)
SELECT Col1, Col2, Col3 FROM MyTable2
Try this:
Insert Into table2
Select * from table1
Insert Selected column with condition
INSERT INTO where_to_insert (col_1,col_2) SELECT col1, col2 FROM from_table WHERE condition;
Copy all data from one table to another with the same column name.
INSERT INTO where_to_insert
SELECT * FROM from_table WHERE condition;
INSERT INTO DestinationTable(SupplierName, Country)
SELECT SupplierName, Country FROM SourceTable;
It is not mandatory column names to be same.
CREATE TABLE `table2` LIKE `table1`;
INSERT INTO `table2` SELECT * FROM `table1`;
the first query will create the structure from table1 to table2 and second query will put the data from table1 to table2
Copy all columns from one table to another table:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
Copy only some columns from one table into another table:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
You can duplicate or "clone" a table's contents by executing:
CREATE TABLE new_table AS SELECT * FROM original_table;
-- for Sql Server users.
if you don't have the new table then you can create the new table with same structure as old table, and also copy data over from old table to the new table. For example:
select * into new_table
from old_table;
also you can copy the column / table structure, and just some of data. For example:
select * into new_table
from old_table
where country = 'DE';

sql select into columns to table and columns

I have a source table that is NewID|Fruit|Apples and I need to insert those rows into target table OldID|Fruit|Apples
Is there a way to select into a new table while changing columns?
Thanks!
Try:
CREATE TABLE new_table SELECT NewID as OldID, Fruit, Apples FROM old_table
Assuming target table already exists and you want to copy all the columns
INSERT INTO target_table(OldID,Fruit,Apples)
SELECT NewID,Fruit,Apples FROM source_table

How to copy a row from one SQL Server table to another

I have two identical tables and need to copy rows from table to another. What is the best way to do that? (I need to programmatically copy just a few rows, I don't need to use the bulk copy utility).
As long as there are no identity columns you can just
INSERT INTO TableNew
SELECT * FROM TableOld
WHERE [Conditions]
Alternative syntax:
INSERT tbl (Col1, Col2, ..., ColN)
SELECT Col1, Col2, ..., ColN
FROM Tbl2
WHERE ...
The select query can (of course) include expressions, case statements, constants/literals, etc.
Jarrett's answer creates a new table.
Scott's answer inserts into an existing table with the same structure.
You can also insert into a table with different structure:
INSERT Table2
(columnX, columnY)
SELECT column1, column2 FROM Table1
WHERE [Conditions]
SELECT * INTO < new_table > FROM < existing_table > WHERE < clause >
INSERT INTO DestTable
SELECT * FROM SourceTable
WHERE ...
works in SQL Server
To select only few rows..This will work like charm..
SELECT TOP 10 *
INTO db2.dbo.new_table
FROM db1.dbo.old_table;
Note : Just create a new table in the required db..We need not define its structure.