How to insert rows with structure from one table to another table? - sql

What is the query in SQL Server to insert rows with structure from one table to another table?

A couple ways
SELECT INTO
SELECT
field1,
field2,
...
INTO Table1
FROM Table2
INSERT INTO
INSERT INTO Table1
field1,
field2,
...
SELECT
field1,
field2,
...
FROM Table2

Sounds like you want something like this:
INSERT INTO DestTable (Column1,COlumn2)
SELECT Column1, Column2
FROM SourceTable

Here is the documentation for the INSERT...EXECUTE statement that will allow you to do what you need.

Related

SQL select only specific fields and insert them in another table + static values

I would like to insert data from specific fields from one table in another table + some static values. Roughly I would like to do this:
INSERT INTO TableA(Field1, Field2, Field3)
SELECT Field1, Field2, 'staticvalue', Field3
FROM TableB
WHERE TableB.Field6 = 'XYZ'
Any idea how can I mix both specific fields from one table and static values?
You're on the right track... you need to supply a column for the static value to be inserted into.
INSERT INTO TableA(Field1, Field2, someOtherField, Field3)
SELECT Field1, Field2, 'staticvalue', Field3
FROM TableB
WHERE TableB.Field6 = 'XYZ'

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';

add a temporary column with a value

I have a select statement like this
select field1, field2
from table1
What I want is to have a newfield with only value "example".
newfield does not exist in the table, but when I run this query it kinda makes a virtual column with the values of example field.
select field1, field2, 'example' as TempField
from table1
This should work across different SQL implementations.
You mean staticly define a value, like this:
SELECT field1,
field2,
'example' AS newfield
FROM TABLE1
This will add a column called "newfield" to the output, and its value will always be "example".
I'm rusty on SQL but I think you could use select as to make your own temporary query columns.
select field1, field2, 'example' as newfield from table1
That would only exist in your query results, of course. You're not actually modifying the table.
In this example, the TABLE registrofaena doesn't have the column called minutos. The query returns a column "minutos" with demora/60 as the content (the values represent the delay in minutes). The table is not modified in the process.
This is the query:
SELECT idfaena,fechahora,demora, demora/60 as minutos,comentario
FROM registrofaena
WHERE fecha>='2018-10-17' AND comentario <> ''
ORDER BY idfaena ASC;
This is the view:
select field1, field2, NewField = 'example' from table1
select field1, field2, '' as newfield from table1
This Will Do you Job

Refer to other SQL SELECT statements aliased as tables in FROM clause

I have a very large query that follows the format below:
select ...
from ( select field1,
field2
from some_table ) table1,
( select field1,
field3
from other_table ) table2
where .....
Is is possible for me to refer to one of the tables "defined" in the from clause, lets say table1, in one of the other table definitions in the from clause?
For example:
select ....
from ( select field1,
field2
from some_table ) table1,
( select table1.field1,
field3
from other_table,
table1 ) table2
where .....
Disclaimer: What I am trying to do is not as simple as the example above. The example is simply to illustrate the idea.
WITH
table1 AS
(
SELECT field1, field2
FROM some_table
),
table2 AS
(
SELECT field1, field2
FROM other_table, table1
)
SELECT *
FROM table2
If you are using SQL 2005, you can use Common Table Expressions for doing what you are trying; Quassnoi gives us an example, in Oracle I don't know how to achieve it though

Copy record to another table adding fields

I have 2 tables:
tab1 (field1, field2, field3)
tab2 (field1, field2,field3, field4)
I want to copy a record from tab1 to tab2 taking all the fields and adding a value for field4.
How can I select field1, field2 and field3 from tab2 and also add a value? I know that SELECT and VALUES in a INSERT query are mutually exclusive.
Thanks in advance.
Gustavo.
I don't know Oracle, but in Ms SQL it works like this:
insert into tab2 (field1, field2, field3, field4)
select field1, field2, field3, 'New Value' from tab1