Using Union with Insert to add one row - sql

I have a query which is made up of two select statements with a union in the middle.
This works for what I need it for.
However, there is one value missing which I want to manually enter.
What I'm looking to is:
select * from tab1
union
select * from tab2
union
insert values('John',cast('2013-01-01' as date), 'Jim', 130)
Unfortunately this is not working. Can someone suggest how I do this please?
I'm using Teradata.

You need to keep selecting:
select * from tab1
union
select * from tab2
union
select 'John', cast('2013-01-01' as date), 'Jim', 130 from dual
The name dual is used in Oracle for a table with one row (and one column). Depending on the DBMS you use, you may be able to omit that final FROM altogether (and you may be able to do this in Oracle too):
select * from tab1
union
select * from tab2
union
select 'John', cast('2013-01-01' as date), 'Jim', 130
or you may have to choose from a system catalog table and ensure you get one row returned (FROM systables WHERE tabid = 1 was the classic mechanism in Informix, though you could also use 'sysmaster':sysdual instead of dual, etc, too), or you can select from any other table with a query that is guaranteed one row. There are probably ways to do it using a VALUES clause too.
Note the change from double quotes to single quotes. In strict standard SQL, double quotes enclose a delimited identifier, but single quotes surround strings.

From your question I' guessing you just want to SELECT that row, not INSERT it (into the database):
select * from tab1
union
select * from tab2
union
select "John", cast('2013-01-01' as date), "Jim", 130

You just want to SELECT the data, not INSERT it.
Not very familiar with TeraData, perhaps you need a FROM in which case limiting to 1 record would also make sense:
select * from tab1
union
select * from tab2
union
SELECT 'John',cast('2013-01-01' as date), 'Jim', '130' FROM dbc.columns 1

I am affraid the union must reference a table and if you need to add the data that does not exist in your database, try this:
select * from tab1
union
select * from tab2
union
select * from (SELECT 'John' as col1 ,cast('2013-01-01' as date) as col2, 'Jim' as col3, '130' as col4) dummy
You will, of course, have to change the name of the columns to fit those from your db (i.e. dont use col1, col2 etc.).
Good luck!

Related

MS Access SQL for UNION from 3 similar tables INSERT INTO a single field in a 4th table?

Aim:
Select all distinct values from three different columns, each located in a different table: [QueryA].Att1, [QueryB].Att2, [QueryC].[Att C].
Return all selected values in a single column in a 4th table: [Table1].Field1
The 'INSERT INTO' line isn't working. The rest of it works (makes query) when 1st line is removed.
The rest of it works (makes query) when 1st line is removed.
INSERT INTO [Table1].Field1
SELECT DISTINCT [QueryA].Att1
FROM [QueryA]
UNION
SELECT DISTINCT [QueryB].Att2
FROM [QueryB]
UNION
SELECT DISTINCT [QueryC].[Att C]
FROM [QueryC];
Error message:
Syntax error in FROM clause.
In MS Access, you need to place UNION in a subquery or entirely separate query to be included in an append. Additionally, period qualifier between table and column should not be used in INSERT INTO:
INSERT INTO [Table1] (Field1)
SELECT Att1
FROM
(SELECT [QueryA].Att1
FROM [QueryA]
UNION
SELECT [QueryB].Att2
FROM [QueryB]
UNION
SELECT [QueryC].[Att C]
FROM [QueryC]
) AS sub
Alternatively, with a separate query:
INSERT INTO [Table1] (Field1)
SELECT Att1
FROM mySavedUnionQuery
could be your insert and select columns don't match (you have more than a col inf table1)
INSERT INTO [Table1] (Field1 )
SELECT [QueryA].Att1
FROM [QueryA]
UNION
SELECT [QueryB].Att2
FROM [QueryB]
UNION
SELECT [QueryC].[Att C]
FROM [QueryC];
and if you use UNION you don't need distinct

Override alphabetical default ORDER BY with a UNION of 2+ tables?

Really quick question... I have 4 tables that are UNION-ed together like so:
SELECT * FROM table1
UNION
SELECT * FROM table2
UNION
SELECT * FROM table3
UNION
SELECT * FROM table4
Without specifying an ORDER BY, the query orders by the first column in ascending alphabetical order (which in my case happens to be a varchar type). I don't want ORDER BY [Column1] DESC either.
I simply want to order the results in the same order as the tables themselves are UNION-ed. 1, 2, 3, 4.
Is there a simply way to do this?
Thanks!!
One way
SELECT *,1 as SortOrder FROM table1
UNION
SELECT *,2 FROM table2
UNION
SELECT *,3 FROM table3
UNION
SELECT *,4 FROM table4
order by SortOrder
what happens is that you are using UNION, sql server then makes the result set distinct, in order to do that it needs to sort the tables
Does UNION ALL make a difference?
We had a similar issue. We have a union query with 32 subsets. We use it to populate a spreadsheet that is then used to build a PowerPoint presentation. The first field in each query is a text field that is a description of the data point. The spreadsheet is expecting the data to be in a specific order.
We made a slight change to one of the fields:
Concat('Annual incidence rate- ', Year(start_date))
This caused SQL to sort the unions in alphabetical order! I suspect that if you ordered your query as #SQLMenace indicated, but with a slight modification, it would work too.
SELECT '1', * FROM table1
UNION
SELECT '2', * FROM table2
UNION
SELECT '3', * FROM table3
UNION
SELECT '4', * FROM table4
This might alleviate the outer query wrapper. It might work without the quotes, too.

input string to table

I am doing some debugging in SQL for oracle 10g. I have a big input string which is used in "IN Clause" i.e.
select * from table where col in ('str2','str3','str4','str5',...)
i want to convert the in clause to rows or table?
Is there a way to do this i.e.
select 'str2','str3','str4','str5', .. from dual
but this outputs multiple columns and i want multiple rows?
Edit:
Here is what i am trying to do. suppose i have an excel data in tmp_table1 (cant create in reality) and tmp_table1 is same as the IN clause, then the below statement will give the missing keys.
SELECT *
FROM tmp_table1
WHERE unique_id NOT IN (
SELECT unique_id
FROM table1
WHERE unique_id IN
('str1', 'str2', 'str3', 'str4'))
now #andriy-m solution works if the in string is less than 4000. but what if its greater?
You are probably looking for this solution.
You can UNION the values into multiple rows:
SELECT 'str2' AS col FROM dual
UNION
SELECT 'str3' FROM dual
UNION
SELECT 'str4' FROM dual
UNION
SELECT 'str5' FROM dual

using SELECT INTO with multiple rows

This is re
I want to create a table using the results of a query by utilizing SELECT INTO.
The syntax
SELECT *
INTO Persons_Backup
FROM Persons
is very close to what I want to achieve, with the difference being that I want the FROM to use a query as source.
My situation is a bit more complicated than these simple examples.
I need to create a table and insert multiple rows at the same time. If I could (I can't) use a previously created table the statement would look like this:
INSERT INTO Person_Backup12 (Col1, Col2, Col3)
Select 1, 'a','2001-01-01 12:00'
UNION ALL
Select 83, 'z','2011-09-30 13:27'
UNION ALL
Select 777, 'k','1997-04-25 09:27'
Can I do that while creating a table at the same time?
You can put your query into a common table expression or derived table then SELECT ... INTO from that.
;WITH cte (Col1, Col2, Col3) AS
(
Select 1, 'a','2001-01-01 12:00'
UNION ALL
Select 83, 'z','2011-09-30 13:27'
UNION ALL
Select 777, 'k','1997-04-25 09:27'
)
SELECT *
INTO NewTable
FROM cte
In this case you would probably need some explicit casts to get the desired column datatype (datetime rather than char etc.)
A CTE shouldn't be necessary:
Select 1 as 'Col1', 'a' as 'Col2','2001-01-01 12:00' as 'Col3'
INTO Person_Backup12
UNION ALL
Select 83, 'z','2011-09-30 13:27'
UNION ALL
Select 777, 'k','1997-04-25 09:27'
Worked fine for me in 2008 r2.
It is possible, yes:
SELECT *
INTO Persons_Backup
FROM
(
Select 1 AS Col1, 'a' AS Col2,'2001-01-01 12:00' AS Col3
UNION ALL
Select 83 AS Col1, 'z' AS Col2,'2011-09-30 13:27' AS Col3
UNION ALL
Select 777 AS Col1, 'k' AS Col2,'1997-04-25 09:27' AS Col3
) AS SomeQuery

Column names for a table formed by a UNION

Given a couple of simple tables like so:
create table R(foo text);
create table S(bar text);
If I were to union them together in a query, what do I call the column?
select T.????
from (
select foo
from R
union
select bar
from S) as T;
Now, in mysql, I can apparently refer to the column of T as 'foo' -- the name of the matching column for the first relation in the union. In sqlite3, however, that doesn't seem to work. Is there a way to do it that's standard across all SQL implementations?
If not, how about just for sqlite3?
Correction: sqlite3 does allow you to refer to T's column as 'foo' after all! Oops!
Try to give an alias to columns;
select T.Col1
from (
select foo as Col1
from R
union
select bar as Col1
from S) as T;
or If the name of column is not necessary then T.* will be enough.
Although there is no spelled rule, we can use the column names from the first subquery in the union query to fetch the union results.
you only need column aliases only in first select (tested in SQl Server 2008 R2)
select T.Col1
from (
select 'val1' as Col1
union
select 'val2'
union
select 'val3'
) as T;