Oracle Select which produce Insert into - sql

I have query like
SELECT 'Insert INTO FOO(ID,NAME) values ('||id|| ',' ||NAME||');' as query
FROM FOO
This is just an example as I can't provide real query example. So, DON'T pay attention how it works just believe that it works as it should.
Question:
If NULL values exists in selected rows it just produces '' in INSERT INTO query. So I am getting an error while trying to execute insert query. How can I force SELECT query to return NULL instead of ''?
Example:
I have
Insert INTO FOO(ID,NAME) values (12,);
I want to have
Insert INTO FOO(ID,NAME) values (12,NULL);

Try using CASE EXPRESSION :
SELECT 'Insert INTO FOO(ID,NAME)
values ('||CASE WHEN id = '''' then 'NULL' else id end||','|| CASE WHEN NAME = '''' THEN null else NAME end || ');' as query
FROM FOO

I am guessing that NAME is a string, so it should be surrounded by single quotes. So, this might be the query you want:
SELECT 'Insert INTO FOO(ID,NAME) values (' || id || ', ''' || NAME ||''');' as query
FROM FOO;
If the issue is id, then you can do:
SELECT 'Insert INTO FOO(ID,NAME) values (' || (CASE WHEN id IS NULL THEN 'NULL' ELSE CAST(id AS VARCHAR2(255)) END) ||
', ''' || NAME ||''');' as query
FROM FOO;

Related

How can combine the values of multiple columns to a single comma separated column in Oracle SQL?

I need to create a column that has all the values from the previous columns combined and separated using commas ', '
I can't use listagg since I'm trying to combine multiple columns instead of rows.
below is an example of how the result column should look like, thanks.
Use string concatenation:
select trim(leading ',' from
(case when column1 is not null then ',' || column1 end) ||
(case when column2 is not null then ',' || column2 end) ||
(case when column3 is not null then ',' || column3 end)
)
This is also using a string concatenation but slightly different
SELECT
SUBSTR(
REPLACE(
', ' || NVL(column1,'!!') || ', ' || NVL(column2,'!!') || ', ' || NVL(column3,'!!')
,', !!','')
,3,100)

Match a concatenated field to a list of variables

Good Afternoon,
I'm trying to match a list of address fields (concatenated to give value ALL_ADDRESS) to a separate table that contains Suffixes, potentially hundreds of rows long.
my desired output is to show those entries where suffixes are part of the ALL_address variable (i.e PARIS STREET)
this works fine when I concatenate without a join, but when I begin to join I get an error:
select s.suffix,
x.key,
x.B_ADDR1_TX,
x.B_ADDR2_TX,
x.B_ADDR3_TX,
x.b_addr_city,
x.b_addr_postcd,
x.b_addr_cntry,
x.b_addr_state_cd,
x.B_ADDR1_TX || ' ' || x.B_ADDR2_TX || ' ' || x.B_ADDR3_TX || ' ' || x.b_addr_city || ' ' || x.b_addr_postcd || ' ' || x.b_addr_cntry || ' ' || x.b_addr_state_cd as All_Address
from test_table AS x
JOIN suffix_list AS s
WHERE
x.All_Address LIKE CONCAT('%',s.suffix,'%') ;
any help is greatly appreciated
I'm not sure what you are trying to do. But proper syntax requires an on clause for a join:
from test_table x join
suffix_list s
on x.All_Address LIKE CONCAT('%', s.suffix, '%')
As I recall, Oracle doesn't support as for table aliases, so your query might have other syntax problems as well.
In Oracle, this would more typically be written as:
from test_table x join
suffix_list s
on x.All_Address LIKE '%' || s.suffix || '%'
Haven't been using Oracle for a while but:
CREATE table t1 (
a varchar(5),
b varchar(5),
c varchar(5));
INSERT INTO t1 VALUES ('one','two','three');
INSERT INTO t1 VALUES ('two','nine','five');
INSERT INTO t1 VALUES ('two','one','one');
CREATE TABLE t2 (filter varchar(5));
INSERT INTO t2 VALUES ('one');
INSERT INTO t2 VALUES ('six');
WITH t1new AS (SELECT t1.*, a || ' ' || b || ' ' || c as address FROM t1)
SELECT t1new.*
FROM t1new,t2
WHERE address like CONCAT(CONCAT('%', t2.filter),'%')
Above example runs in liveql Oracle.

Vertica. Count of Null and Not-Null of all columns of a Table

How can we get null and non-null counts of all columns of a Table in Vertica? Table can have n number of columns and for each column we need to get count of nulls and non-nulls values of that table.
For Example.
Below Table has two columns
column1 Column2
1 abc
pqr
3
asd
5
If its a specific column then we can check like
SELECT COUNT(*) FROM table where column1 is null;
SELECT COUNT(*) FROM table where column1 is not null;
Same query for column2
I checked system tables like projection_storage and others but I cant figure out a generic query which gives details by hard coding only TABLE NAME in the query.
Hello #user2452689: Here is a dynamically generated VSQL statement which meets your requirement of counting nulls & not nulls in N columns. Notice that this writes a temporary SQL file out to your working directory, and then execute it via the \i command. You only need to change the first two variables per table. Hope this helps - good luck! :-D
--CHANGE SCHEMA AND TABLE PARAMETERS ONLY:
\set table_schema '\'public\''
\set table_name '\'dim_promotion\''
---------
\o temp_sql_file
\pset tuples_only
select e'select \'' || :table_schema || e'\.' || :table_name || e'\' as table_source' as txt
union all
select * from (
select
', sum(case when ' || column_name || ' is not null then 1 else 0 end) as ' || column_name || '_NOT_NULL
, sum(case when ' || column_name || ' is null then 1 else 0 end) as ' || column_name || '_NULL' as txt
from columns
where table_schema = :table_schema
and table_name = :table_name
order by ordinal_position
) x
union all
select ' from ' || :table_schema || e'.' || :table_name || ';' as txt ;
\o
\pset tuples_only
\i temp_sql_file
You can use:
select count(*) as cnt,
count(column1) as cnt_column1,
count(column2) as cnt_column2
from t;
count() with a column name or expression counts the number of non-NULL values in the column/expression.
(Obviously, the number of NULL values is cnt - cnt_columnX.)
select column1_not_null
,column2_not_null
,column3_not_null
,cnt - column1_not_null as column1_null
,cnt - column2_not_null as column2_null
,cnt - column3_not_null as column3_null
from (select count(*) as cnt
,count (column1) as column1_not_null
,count (column2) as column2_not_null
,count (column3) as column3_not_null
from mytable
) t

SQL insert query with values like JUM'A ABDUL FATTAH causing missing , error

I am trying to insert data into a table which is derived dynamically.
I am selecting data from MASTER_TABLE which has col1 and col2. Depending on the values in col1 and col2, I am inserting the data in table SLAVE_col1value_col2value.
my sql query looks like:
insert_query_str :='INSERT INTO SLAVE_'
|| Col1
|| '_'
|| Col2
|| ' VALUES ( '''
|| CUST_NAME
|| ''','''
|| APPLICATION_DATA
|| ''')';
EXECUTE IMMEDIATE insert_query_str;
However, name like JUM'A ABDUL FATTAH cause the insert query string to look like this
INSERT INTO SLAVE_XYZ_ABC VALUES ( 'JUM'A ABDUL FATTAH','APPSPECIFICDATA');
and I get a missing comma error on the line.
I know if I double quote the name then this error can be sorted out but when I try that it hardcodes the || too and I don't get the real name in the query.
I am using Oracle SQL 11g and Toad.
Thank you in advance
Use placeholders together with USING clause:
insert_query_str :='INSERT INTO SLAVE_'
|| Col1
|| '_'
|| Col2
|| ' VALUES ( :cust_name, :app_data )';
EXECUTE IMMEDIATE insert_query_str USING CUST_NAME, APPLICATION_DATA;

Transposing a table through select query

I have a table like:
Key type value
---------------------
40 A 12.34
41 A 10.24
41 B 12.89
I want it in the format:
Types 40 41 42 (keys)
---------------------------------
A 12.34 10.24 XXX
B YYY 12.89 ZZZ
How can this be done through an SQL query. Case statements, decode??
What you're looking for is called a "pivot" (see also "Pivoting Operations" in the Oracle Database Data Warehousing Guide):
SELECT *
FROM tbl
PIVOT(SUM(value) FOR Key IN (40, 41, 42))
It was added to Oracle in 11g. Note that you need to specify the result columns (the values from the unpivoted column that become the pivoted column names) in the pivot clause. Any columns not specified in the pivot are implicitly grouped by. If you have columns in the original table that you don't wish to group by, select from a view or subquery, rather than from the table.
You can engage in a bit of wizardry and get Oracle to create the statement for you, so that you don't need to figure out what column values to pivot on. In 11g, when you know the column values are numeric:
SELECT
'SELECT * FROM tbl PIVOT(SUM(value) FOR Key IN ('
|| LISTAGG(Key, ',') WITHIN GROUP (ORDER BY Key)
|| ');'
FROM tbl;
If the column values might not be numeric:
SELECT
'SELECT * FROM tbl PIVOT(SUM(value) FOR Key IN (\''
|| LISTAGG(Key, '\',\'') WITHIN GROUP (ORDER BY Key)
|| '\'));'
FROM tbl;
LISTAGG probably repeats duplicates (would someone test this?), in which case you'd need:
SELECT
'SELECT * FROM tbl PIVOT(SUM(value) FOR Key IN (\''
|| LISTAGG(Key, '\',\'') WITHIN GROUP (ORDER BY Key)
|| '\'));'
FROM (SELECT DISTINCT Key FROM tbl);
You could go further, defining a function that takes a table name, aggregate expression and pivot column name that returns a pivot statement by first producing then evaluating the above statement. You could then define a procedure that takes the same arguments and produces the pivoted result. I don't have access to Oracle 11g to test it, but I believe it would look something like:
CREATE PACKAGE dynamic_pivot AS
-- creates a PIVOT statement dynamically
FUNCTION pivot_stmt (tbl_name IN varchar2(30),
pivot_col IN varchar2(30),
aggr IN varchar2(40),
quote_values IN BOOLEAN DEFAULT TRUE)
RETURN varchar2(300);
PRAGMA RESTRICT_REFERENCES (pivot_stmt, WNDS, RNPS);
-- creates & executes a PIVOT
PROCEDURE pivot_table (tbl_name IN varchar2(30),
pivot_col IN varchar2(30),
aggr IN varchar2(40),
quote_values IN BOOLEAN DEFAULT TRUE);
END dynamic_pivot;
CREATE PACKAGE BODY dynamic_pivot AS
FUNCTION pivot_stmt (
tbl_name IN varchar2(30),
pivot_col IN varchar2(30),
aggr_expr IN varchar2(40),
quote_values IN BOOLEAN DEFAULT TRUE
) RETURN varchar2(300)
IS
stmt VARCHAR2(400);
quote VARCHAR2(2) DEFAULT '';
BEGIN
IF quote_values THEN
quote := '\\\'';
END IF;
-- "\||" shows that you are still in the dynamic statement string
-- The input fields aren't sanitized, so this is vulnerable to injection
EXECUTE IMMEDIATE 'SELECT \'SELECT * FROM ' || tbl_name
|| ' PIVOT(' || aggr_expr || ' FOR ' || pivot_col
|| ' IN (' || quote || '\' \|| LISTAGG(' || pivot_col
|| ', \'' || quote || ',' || quote
|| '\') WITHIN GROUP (ORDER BY ' || pivot_col || ') \|| \'' || quote
|| '));\' FROM (SELECT DISTINCT ' || pivot_col || ' FROM ' || tbl_name || ');'
INTO stmt;
RETURN stmt;
END pivot_stmt;
PROCEDURE pivot_table (tbl_name IN varchar2(30), pivot_col IN varchar2(30), aggr_expr IN varchar2(40), quote_values IN BOOLEAN DEFAULT TRUE) IS
BEGIN
EXECUTE IMMEDIATE pivot_stmt(tbl_name, pivot_col, aggr_expr, quote_values);
END pivot_table;
END dynamic_pivot;
Note: the length of the tbl_name, pivot_col and aggr_expr parameters comes from the maximum table and column name length. Note also that the function is vulnerable to SQL injection.
In pre-11g, you can apply MySQL pivot statement generation techniques (which produces the type of query others have posted, based on explicitly defining a separate column for each pivot value).
Pivot does simplify things greatly. Before 11g however, you need to do this manually.
select
type,
sum(case when key = 40 then value end) as val_40,
sum(case when key = 41 then value end) as val_41,
sum(case when key = 42 then value end) as val_42
from my_table
group by type;
Never tried it but it seems at least Oracle 11 has a PIVOT clause
If you do not have access to 11g, you can utilize a string aggregation and a grouping method to approx. what you are looking for such as
with data as(
SELECT 40 KEY , 'A' TYPE , 12.34 VALUE FROM DUAL UNION
SELECT 41 KEY , 'A' TYPE , 10.24 VALUE FROM DUAL UNION
SELECT 41 KEY , 'B' TYPE , 12.89 VALUE FROM DUAL
)
select
TYPE ,
wm_concat(KEY) KEY ,
wm_concat(VALUE) VALUE
from data
GROUP BY TYPE;
type KEY VALUE
------ ------- -----------
A 40,41 12.34,10.24
B 41 12.89
This is based on wm_concat as shown here: http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php
I'm going to leave this here just in case it helps, but I think PIVOT or MikeyByCrikey's answers would best suit your needs after re-looking at your sample results.