I want to copy records from one table to another table with extra columns.
The table columns looks like this
Table1: A,B,C,...,X
Table2: A,B,C,...,X,Y,Z
I know that i can do it in the following way
Insert into table2
Select
A,
B,
C,
...
X,
'1',
'1'
from table1
But it is a very inefficient way if the table contains lots of columns. I need to do the similar procedure for several tables. Is there a better way to do it?
I tried the following but it doesn't work
insert into table2 select * , '1', '1' from table1;
The thing you tried should work, just add alias.
INSERT INTO table2
SELECT t1.*,
'1',
'1'
FROM table1 t1;
Insert into table2 ( A, B, C, ... , X, Y , Z) SELECT ( A, B, C, ... , X, '1' , '1') from table1 ;
In this case Y & Z are Strings (characters).
Related
I'm trying to insert multiple rows into my table using select but I'm getting not enough values error.
My query:
Insert into c(x, y) select * from a union all select * from d;
table a and b contains 2 records each and table c has one record.
try like below by specifying both column names
Insert into c(x, y)
select col1,col2 from a
union all
select col1,col2 from d
for union all both tables have the same number of colums and their data type also need to be same
List the columns explicitly:
Insert into c (x, y)
select col1, col2
from a
union all
select col1, col2
from d;
If one of tables has only one column, then use a placeholder for the value:
Insert into c (x, y)
select col1, col2
from a
union all
select col1, NULL
from d;
I would like to use UNION on two tables in order to combine a similar field, then insert that UNION data into a different table.
Example:
Table1 has the following fields:
x
y
z
Table2 has the following fields:
x
w
v
I would like to perform UNION on x in order to insure there are no duplicate rows, then put that data in another table.
Example:
I would like MainTable to have the following fields:
x
y
z
w
v
As you can tell, all of the fields from both Table1 and Table2 exist in MainTable, but x has had UNION performed on it.
This SQL code does not work in a query, however, and is giving me Syntax Error in FROM Clause:
INSERT INTO MainTable(x)
SELECT x
FROM (Table1)
UNION
SELECT x
FROM (Table2)
Try:
INSERT INTO MainTable(x)
FROM
(
SELECT x
FROM (Table1)
UNION
SELECT x
FROM (Table2)
) as t
I am trying to create a SQL query where I need to insert a new sets of records based but 1 column is needed from required table.
E.g.,
TABLE_1
=======
ID,
A,
B
TABLE2
======
ID,
C,
D
Each ID are identical ID column.
I have a query of this format:
INSERT INTO TABLE_1 (ID, A, B) VALUES (???, "Yes", "What")
WHERE ID IN (SELECT ID FROM TABLE_2 WHERE ID > 10)
This, clearly doesn't work.
My question: how do I add the ID value from Table 2 into Table 1 and make it runnable? This is a query that must run on Oracle 11g.
Try this query:
INSERT INTO MyTable(ID, A, B)
SELECT ID,'YES','What' FROM TABLE_2 WHERE ID > 10
I've got a table variable that I'm wanting to insert a union query. The union query runs fine, but I can't seem to get the insert to work (syntax error)
INSERT INTO #table
(a,
b,
c,
d)
VALUES
(SELECT
a,
b,
c,
d
FROM table1
UNION
SELECT
a,
b,
c,
d
FROM table2)
Should this be working? I can post my real code if there is an issue elsewhere!
I'm getting a syntax error on the first SELECT
INSERT INTO #table(a,b,c,d)
SELECT a,b,c,d
FROM table1
UNION
SELECT a,b,c,d
FROM table2
You do not need to use the Values clause when Inserting data using SELECT statement. Therefore I have removed the VALUES bit from it and just simply doing a UNION of rows being returned from both SELECT queries.
Sql server supports the syntax for INSERT statement like
INSERT INTO Table_Name(Col1, COl2. Col3...)
SELECT Col1, COl2. Col3...
FROM Other_Table_Name
This will insert that result set returned by the select statement into target table. In your case the Result is a UNION of two selects therefore it is not any different from a single select.
"VALUES" isn't needed in this case
INSERT INTO #table (a, b, c, d)
SELECT a, b, c, d FROM table1
UNION
SELECT a, b, c, d FROM table2
i tried something like this but the id value is not changing, getting the value for first record and set the value for all rest...
insert into table1 (Id,b,c,d)
(select (select max(Id)+1 from table1),x,y,z from table2 where... )
Use an identity column. Then you can just do this:
INSERT INTO table1 (b, c, d)
SELECT x, y, z
FROM table2
WHERE ...
If you don't want to use an auto-increment column you can get the same effect by using adding the row number of the row instead of always adding 1. This syntax works in almost all mainstream SQL databases (not MySQL though):
INSERT INTO table1 (Id, b, c, d)
SELECT
(SELECT MAX(Id) FROM table1) + ROW_NUMBER() OVER (ORDER BY x),
x, y, z
FROM table2
WHERE ...