Oracle INSERT INTO with select and values - sql

I'm trying to insert some values into a table from a select statement and some hardcoded values, but I'm not sure what the syntax would be. When I try I get an error saying there are not enough values so I know it's not reading my select statement correctly. Any help is appreciated.
insert into INSERT_STG
values(
(select code,
acct,
to_char(sysdate, 'mmddyy'),
amt
from schema.table),
'Partners',
'city',
'st',
'Y',
null,
);

insert into INSERT_STG
(select code,
acct,
to_char(sysdate, 'mmddyy'),
amt ,
'Partners',
'city',
'st',
'Y',
null
from schema.table);
Problems:
You had extra comma after null
You can't combine hardcoded values and the select like you did. The hard coded values have to be generated as part of the select.
This should work assuming: INSERT_STG has 9 columns of the datatypes in schema.table in the order of the select and string and support null on last column.

Get rid of the "values" line and ensure you're inserting the same count of values as the table INSERT_STG has. Otherwise, explicitly specify columns of target table to insert.

Related

Is there any way to output the result of a column of each row to show a different value in OracleDB using PL/SQL?

I have a Select Statement that has a column which is a code value. For e.g. instead of Java its JV, and instead of Python its PY. However, instead of outputting the coded value, I would like to display them as Java or Python i.e. their full description. Is there a way to do this with PL/SQL?
Any help would be greatly appreciated, thanks!
you can use case expression
select
case
when myColumn = 'JV' then 'Java'
when myColumn = 'PY' then 'Python'
end as myColumn
from yourTable
In oracle you can use decode as well.
decode(col, 'JV', 'Java',
'PY', 'Python'
'No Match'
)
Given that you are using Oracle, I would recommend using the DECODE function:
SELECT
col,
DECODE(col, 'JV', 'Java',
'PY', 'Python',
'Not found') AS col_out
FROM yourTable;
You can use either the "DECODE" function, or a "CASE" construct, as follows:
select DECODE(my_column,
'JV','Java',
'PY','Python',
'no_match_found') my_column_alias
from my_table;
select
case my_column
when 'JV' then 'Java'
when 'PY' then 'Python'
else 'no_match_found'
end my_column_alias
from my_table;
While there are several ways of hard coding the for the sample data given none are the proper method for more than a very limited set. The proper method is to create a lookup table. In this case contains the code and corresponding name. Yes, it is quite overkill for 2 languages, but how about the TIOBE Index or the 700 listed in Wikipedia, or the estimated approximately 9000 claimed in the HOPL list. See here and additional links more.
Moreover it is a standard technique to OP underlying question: Is there a way to give detail about a given code value.
It is easily extended (just add row to a table) and applicable across virtually all domains (abet with slight modifications).
-- create language lookup
create table language_lookup(
code varchar2(8)
, name varchar2(200)
, description varchar2(4000)
, constraint language_lookup_pk
primary key (code)
);
insert into language_lookup(code, name)
select 'JV','Java' from dual union all
select 'PY','Python' from dual ;
-- your table
create table your_table ( id integer
, lang_code varchar2(8)
--...
, constraint your_table_pk
primary key (id)
, constraint your_table_2_language_lookup_fk
foreign key (lang_code)
references language_lookup(code)
) ;
insert into your_table (id, lang_code)
select 1,'JV' from dual union all
select 2,'PY' from dual;
select yt.lang_code, ll.name
from your_table yt
join language_lookup ll
on ll.code = yt.lang_code
where yt.lang_code in ('JV','PY')
;
-- now modify to include plsql
insert into language_lookup(code, name)
values ( 'PLSQL', 'Oracle''s Programming Language extension for SQL');
insert into your_table (id,lang_code)
values (3,'PLSQL');
select yt.lang_code, ll.name
from your_table yt
join language_lookup ll
on ll.code = yt.lang_code
;
Try that with your hard coded values. Then add 30 - 40 more ...

Is there a SQL SELECT to rename one column preserving column order? [duplicate]

here is what I'm trying to do- I have a table with lots of columns and want to create a view with one of the column reassigned based on certain combination of values in other columns, e.g.
Name, Age, Band, Alive ,,, <too many other fields)
And i want a query that will reassign one of the fields, e.g.
Select *, Age =
CASE When "Name" = 'BRYAN ADAMS' AND "Alive" = 1 THEN 18
ELSE "Age"
END
FROM Table
However, the schema that I now have is Name, Age, Band, Alive,,,,<too many>,, Age
I could use 'AS' in my select statment to make it
Name, Age, Band, Alive,,,,<too many>,, Age_Computed.
However, I want to reach the original schema of
Name, Age, Band, Alive.,,,, where Age is actually the computed age.
Is there a selective rename where I can do SELECT * and A_1 as A, B_1 as b? (and then A_1 completely disappears)
or a selective * where I can select all but certain columns? (which would also solve the question asked in the previous statement)
I know the hacky way where I enumerate all columns and create an appropriate query, but I'm still hopeful there is a 'simpler' way to do this.
Sorry, no, there is not a way to replace an existing column name using a SELECT * construct as you desire.
It is always better to define columns explicitly, especially for views, and never use SELECT *. Just use the table's DDL as a model when you create the view. That way you can alter any column definition you want (as in your question) and eliminate columns inappropriate for the view. We use this technique to mask or eliminate columns containing sensitive data like social security numbers and passwords. The link provided by marc_s in the comments is a good read.
Google BigQuery supports SELECT * REPLACE:
A SELECT * REPLACE statement specifies one or more expression AS identifier clauses. Each identifier must match a column name from the SELECT * statement.
In the output column list, the column that matches the identifier in a REPLACE clause is replaced by the expression in that REPLACE clause.
A SELECT * REPLACE statement does not change the names or order of columns. However, it can change the value and the value type.
Select *, Age = CASE When "Name" = 'BRYAN ADAMS' AND "Alive" = 1 THEN 18
ELSE "Age"
END
FROM tab
=>
SELECT * REPLACE(CASE WHEN Name = 'BRYAN ADAMS' AND Alive = 1 THEN 18
ELSE Age END AS Age)
FROM Tab
Actually, there is a way to do this in MySQL. You need to use a hack to select all but one column as posted here, then add it separately in the AS statement.
Here is an example:
-- Set-up some example data
DROP TABLE IF EXISTS test;
CREATE TABLE `test` (`ID` int(2), `date` datetime, `val0` varchar(1), val1 INT(1), val2 INT(4), PRIMARY KEY(ID, `date`));
INSERT INTO `test` (`ID`, `date`, `val0`, `val1`, `val2`) VALUES
(1, '2016-03-07 12:20:00', 'a', 1, 1001),
(1, '2016-04-02 12:20:00', 'b', 2, 1004),
(1, '2016-03-01 10:09:00', 'c', 3, 1009),
(1, '2015-04-12 10:09:00', 'd', 4, 1016),
(1, '2016-03-03 12:20:00', 'e', 5, 1025);
-- Select all columns, renaming 'val0' as 'yabadabadoo':
SET #s = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'val0,', '')
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'test' AND TABLE_SCHEMA =
'<database_name>'), ', val0 AS `yabadabadoo` FROM test');
PREPARE stmt1 FROM #s;
EXECUTE stmt1;

Rename single column in SELECT * in SQL, select all but a column

here is what I'm trying to do- I have a table with lots of columns and want to create a view with one of the column reassigned based on certain combination of values in other columns, e.g.
Name, Age, Band, Alive ,,, <too many other fields)
And i want a query that will reassign one of the fields, e.g.
Select *, Age =
CASE When "Name" = 'BRYAN ADAMS' AND "Alive" = 1 THEN 18
ELSE "Age"
END
FROM Table
However, the schema that I now have is Name, Age, Band, Alive,,,,<too many>,, Age
I could use 'AS' in my select statment to make it
Name, Age, Band, Alive,,,,<too many>,, Age_Computed.
However, I want to reach the original schema of
Name, Age, Band, Alive.,,,, where Age is actually the computed age.
Is there a selective rename where I can do SELECT * and A_1 as A, B_1 as b? (and then A_1 completely disappears)
or a selective * where I can select all but certain columns? (which would also solve the question asked in the previous statement)
I know the hacky way where I enumerate all columns and create an appropriate query, but I'm still hopeful there is a 'simpler' way to do this.
Sorry, no, there is not a way to replace an existing column name using a SELECT * construct as you desire.
It is always better to define columns explicitly, especially for views, and never use SELECT *. Just use the table's DDL as a model when you create the view. That way you can alter any column definition you want (as in your question) and eliminate columns inappropriate for the view. We use this technique to mask or eliminate columns containing sensitive data like social security numbers and passwords. The link provided by marc_s in the comments is a good read.
Google BigQuery supports SELECT * REPLACE:
A SELECT * REPLACE statement specifies one or more expression AS identifier clauses. Each identifier must match a column name from the SELECT * statement.
In the output column list, the column that matches the identifier in a REPLACE clause is replaced by the expression in that REPLACE clause.
A SELECT * REPLACE statement does not change the names or order of columns. However, it can change the value and the value type.
Select *, Age = CASE When "Name" = 'BRYAN ADAMS' AND "Alive" = 1 THEN 18
ELSE "Age"
END
FROM tab
=>
SELECT * REPLACE(CASE WHEN Name = 'BRYAN ADAMS' AND Alive = 1 THEN 18
ELSE Age END AS Age)
FROM Tab
Actually, there is a way to do this in MySQL. You need to use a hack to select all but one column as posted here, then add it separately in the AS statement.
Here is an example:
-- Set-up some example data
DROP TABLE IF EXISTS test;
CREATE TABLE `test` (`ID` int(2), `date` datetime, `val0` varchar(1), val1 INT(1), val2 INT(4), PRIMARY KEY(ID, `date`));
INSERT INTO `test` (`ID`, `date`, `val0`, `val1`, `val2`) VALUES
(1, '2016-03-07 12:20:00', 'a', 1, 1001),
(1, '2016-04-02 12:20:00', 'b', 2, 1004),
(1, '2016-03-01 10:09:00', 'c', 3, 1009),
(1, '2015-04-12 10:09:00', 'd', 4, 1016),
(1, '2016-03-03 12:20:00', 'e', 5, 1025);
-- Select all columns, renaming 'val0' as 'yabadabadoo':
SET #s = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'val0,', '')
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'test' AND TABLE_SCHEMA =
'<database_name>'), ', val0 AS `yabadabadoo` FROM test');
PREPARE stmt1 FROM #s;
EXECUTE stmt1;

How to select all values and hide NULL values in SQL?

so in my database some rows have NULL values, and when I select * from table, that NULL values also shows as text "null". So I want to hide all NULL values. Does anyone have idea for query? Thanks!
this is my input in DB:
db.execSQL("CREATE TABLE IF NOT EXISTS table (name VARCHAR, kg VARCHAR, pod VARCHAR,reps VARCHAR, time VARCHAR );");
db.execSQL("INSERT INTO table VALUES('name 1',NULL,NULL , NULL , '"+s+"');");
db.execSQL("INSERT INTO table VALUES(NULL,'S 1','"+ee5+"' , '"+ee+"' , '"+s+"');");
db.execSQL("INSERT INTO table VALUES(NULL,'S 2','"+ee6+"' , '"+ee2+"', '"+s+"');");
db.execSQL("INSERT INTO table VALUES(NULL,'S 3','"+ee7+"' , '"+ee3+"', '"+s+"');");
db.execSQL("INSERT INTO table VALUES(NULL,'S 4','"+ee8+"' , '"+ee4+"', '"+s+"');");
It depends on the data type of the column.
-- If the data type is integer:
SELECT COALESCE(the_column, 0)
FROM the_table;
-- or, if the column is a char or varchar type:
SELECT COALESCE(the_column, 'some text here')
FROM the_table;
-- or, if it is a date:
SELECT COALESCE(the_column, '1900-01-01')
FROM the_table;
BTW: some databases have the IFNULL() function which does the same thing.
This is what you are looking for:
SELECT x, y, etc, CASE WHEN field IS NOT NULL THEN field ELSE '' END AS hehe FROM table;
Edit: Addin to your comments, this is pretty trivial once you know how to do it for one column. Apply the same for all columns. In SO dont expect homeworks to get done, instead expect a help to solve your problem which ultimately you yourself have to do.
Btw, here is how..
SELECT COALESCE(name, ''), COALESCE(kg, ''), COALESCE(pod, ''), COALESCE(reps, ''),
COALESCE(time, '')
FROM table
You have three good methods in this thread (including mine), and I personally feel the other two are more intuitive. Use any by applying the same logic as I have shown.
in SQLite you should be able to do something like this:
SELECT col1, col2, ..., ifnull(coln, '') FROM TABLE
SELECT* FROM TableName
WHERE ValueCol IS NOT NULL
http://www.w3schools.com/sql/sql_null_values.asp
if you're working with sqlite for android
look at this Article

Comma separated list of values as different rows, not as fields for one row, for an SQL INSERT

I want to make many inserts in a single SQL statement, pretty much like
INSERT INTO xxx SELECT field FROM zzz
But with values I don't have in a table, and are just a list of literals.
Normal way would be executing one SQL query for each value, but if there exists a standard SQL statement, or MySQL-specific, it would be great.
insert into xxx (fields) values (values1), (values2), (values3)
eg insert into mytable (name, desc) values ('name1','desc1'), ('name2','desc2'), ('name3','desc3'), ('name4','desc4')
insert into xxx(afield)
select 'a'
union
select 'b'
union
select 'x'
will give you a table like that :
afield
a
b
x