Insert into table variable with union - sql

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

Related

Insert multiple rows into a table from two different tables in oracle?

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;

Copy multiple columns from a table to another table with extra columns

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).

Which select is faster in Sqlite?

a is a table index, b is a normal column.
select a,b from ( select a,b from table where a in (*listA*) ) where b in (*listB*)
or
select a,b from table where (a=listA[0] and b=listB[0]) or (a=listA[1] and b=listB[1])...
I am using pseudocode to represent a list declaration.
The first query is wrong because it never looks at the combinations of a and b.
To use a temp table, you have to join with it:
SELECT a, b
FROM MyTable
JOIN (SELECT 1 AS a, 2 AS b UNION ALL
SELECT 3, 4 UNION ALL
SELECT 5, 6 ...)
USING (a, b)
Which version is better optimized depends on too many factors; the only way to find out is to measure with representative data.

Single line query for copying data from 3 tables to a empty table

Today in interview, I was asked, if I can write a single query to copy data from 3 tables to an empty table.
I started saying, I will use temp table or table variables but he said no, he wants to see in a single statement or query.... I was blank :(
Do any of you can share the right answer please :)
insert into <emplty_table>
select * from table1
union all
select * from table2
union all
select * from table3
provided all the table have the same structure
Depends upon the specifics, but in general I think the idea would be to use a union. The following is obviously pseudocode, but it conveys the idea:
insert into x (field1, field2, field3)
select a, b, c
from table1
union
select d, e, f
from table2
union
select g, h, i
from table3

In SQL Server, what's the best way to merge tables from multiple databases?

I'm sorry that I can't find a better title of my question. Not lemme describe it in detail.
I have 4 database which are a, b, c and d. Database a have all table's that appear in b, c and d, and they have the same structure with the same constraints(pk, fk, default, check). b, c,d just have some tables that appear in a. Now there already some data in a, b, c and d. In b, c,d there are more data than the counterparts in a. And probably a have duplicated data with b, c,d.
Now what I want to do is export all data in b, c,d and import them to a. I already have a solution but I want to know what is the best method to do such a complicated task.
Thanks.
The UNIONs (no ALL) in the subquery will remove duplicates. Then the IS NULL in the Where will only insert new rows into Table1.
Insert Into DatabaseA.dbo.Table1(ID, Value)
Select ID, Value
FROM (
Select ID, Value From DatabaseB.dbo.Table1
UNION
Select ID, Value From DatabaseC.dbo.Table1
UNION
Select ID, Value From DatabaseD.dbo.Table1
) T
LEFT JOIN DatabaseA.dbo.Table1 S ON T.ID = S.ID
WHERE S.ID IS NULL
You can perform a Insert Into statement with the use of a unions that obtains the results from other databases
Insert Into dboTableA(ID, Value)
Select ID, Value From dbo.DatabaseB.TableA
UNION AlL
Select ID, Value From dob.DatabaseC.TableA
UNION ALL
Select ID, Value From dbo.DatabaseD.TableA