SQL script to UNION a large number of tables - sql

I have to do a union of a large number of disjoint daily tables from 2012-12-17 to 2012-10-30 in this example. The code for this gets ugly here is the snippet:
CREATE table map
with (appendonly=true, compresstype = quicklz)
AS
SELECT * FROM final_map_12_17
UNION ALL
SELECT * FROM final_map_12_16
UNION ALL
SELECT * FROM final_map_12_15
UNION ALL
SELECT * FROM final_map_12_14
UNION ALL
....
SELECT * FROM final_map_10_30;
Can I do this type of thing with a sequence or PL/PGSQL function instead of writing out each individual select by hand?

You can loop over date range in plpgsql function like this:
create or replace function add_map(date_from date, date_to date)
returns void language plpgsql as $$
declare
day date;
begin
for day in
select generate_series(date_from, date_to, '1 day')
loop
execute 'insert into map select * from final_map_'||
to_char(extract(month from day), '09')|| '_' ||
to_char(extract(day from day), '09');
end loop;
end; $$;
Calling the function:
-- create table map (....);
select add_map('2012-11-30', '2012-12-02');
is equivalent to:
insert into map select * from final_map_11_30;
insert into map select * from final_map_12_01;
insert into map select * from final_map_12_02;

There isn't a SQL function that would do this.
I would recommend that you put the list of tables in Excel. Then put in a formula such as:
="select * from "&a1&" union all"
Copy this formula down. Voila! You almost have the view defniition.
Copy the column with these statements into the SQL command tool. Add the create view at the top. Remove the union all at the end. And voila. You can easily create the view.

Have a think about redefining your list of tables as a partitioned table, with a single master table and multiple child tables. http://www.postgresql.org/docs/9.2/static/ddl-partitioning.html
Alternatively, maintain a view to union all the tables together, and when you add a new table to the schema add it to the view also.

Related

Oracle SQL: how to pass parameter to a function that is used in a view through a column in a view?

Let's say we have such view that uses function with hard-coded parameter:
CREATE OR REPLACE VIEW x AS
SELECT t.some_value
FROM table(function(p1 => '1')) t;
If I'd like to pass that parameter to a function through a view, what are possible options? Please mind that using global or context/bind variables is not an option. So far I've came up with an option to use a table that holds all available parameter values (keys) that could be passed to a view:
CREATE OR REPLACE VIEW x AS
SELECT st.input_param,
t.some_value
FROM some_table st
table(function(p1 => st.input_param)) t;
However, I am wondering if there are any other possible options?
You can't pass a parameter to a view but you can use the next alternative:
CREATE TYPE RECORDS_VARCHAR AS TABLE OF VARCHAR2(100);
create or replace function virtual_table( input_param number )
return RECORDS_VARCHAR
PIPELINED
is
begin
FOR a IN (
select '1' AS VALUE from dual where input_param = 2
UNION ALL
select '8' AS VALUE from dual
) loop
pipe row (a.VALUE);
end loop;
return;
end;
SELECT * FROM TABLE(virtual_table(2)); --1,8
SELECT * FROM TABLE(virtual_table(1)); --8

How to store multiple rows in a variable in pl/sql function?

I'm writing a pl/sql function. I need to select multiple rows from select statement:
SELECT pel.ceid
FROM pa_exception_list pel
WHERE trunc(pel.creation_date) >= trunc(SYSDATE-7)
if i use:
SELECT pel.ceid
INTO v_ceid
it only stores one value, but i need to store all values that this select returns. Given that this is a function i can't just use simple select because i get error, "INTO - is expected."
You can use a record type to do that. The below example should work for you
DECLARE
TYPE v_array_type IS VARRAY (10) OF NUMBER;
var v_array_type;
BEGIN
SELECT x
BULK COLLECT INTO
var
FROM (
SELECT 1 x
FROM dual
UNION
SELECT 2 x
FROM dual
UNION
SELECT 3 x
FROM dual
);
FOR I IN 1..3 LOOP
dbms_output.put_line(var(I));
END LOOP;
END;
So in your case, it would be something like
select pel.ceid
BULK COLLECT INTO <variable which you create>
from pa_exception_list
where trunc(pel.creation_Date) >= trunc(sysdate-7);
If you really need to store multiple rows, check BULK COLLECT INTO statement and examples. But maybe FOR cursor LOOP and row-by-row processing would be better decision.
You may store all in a rowtype parameter and show whichever column you want to show( assuming ceid is your primary key column, col1 & 2 are some other columns of your table ) :
SQL> set serveroutput on;
SQL> declare
l_exp pa_exception_list%rowtype;
begin
for c in ( select *
from pa_exception_list pel
where trunc(pel.creation_date) >= trunc(SYSDATE-7)
) -- to select multiple rows
loop
select *
into l_exp
from pa_exception_list
where ceid = c.ceid; -- to render only one row( ceid is primary key )
dbms_output.put_line(l_exp.ceid||' - '||l_exp.col1||' - '||l_exp.col2); -- to show the results
end loop;
end;
/
SET SERVEROUTPUT ON
BEGIN
FOR rec IN (
--an implicit cursor is created here
SELECT pel.ceid AS ceid
FROM pa_exception_list pel
WHERE trunc(pel.creation_date) >= trunc(SYSDATE-7)
)
LOOP
dbms_output.put_line(rec.ceid);
END LOOP;
END;
/
Notes from here:
In this case, the cursor FOR LOOP declares, opens, fetches from, and
closes an implicit cursor. However, the implicit cursor is internal;
therefore, you cannot reference it.
Note that Oracle Database automatically optimizes a cursor FOR LOOP to
work similarly to a BULK COLLECT query. Although your code looks as if
it fetched one row at a time, Oracle Database fetches multiple rows at
a time and allows you to process each row individually.

Delete all data from a table after selecting all data from the same table

All i want is to select all rows from a table and once it is selected and displayed, the data residing in table must get completely deleted. The main concern is that this must be done using sql only and not plsql. Is there a way we can do this inside a package and call that package in a select statement? Please enlighten me here.
Dummy Table is as follows:
ID NAME SALARY DEPT
==================================
1 Sam 50000 HR
2 Max 45000 SALES
3 Lex 51000 HR
4 Nate 66000 DEV
Any help would be greatly appreciated.
select * from Table_Name;
Delete from Table_Name
To select the data from a SQL query try using a pipelined function.
The function can define a cursor for the data you want (or all the data in the table), loop through the cursor piping each row as it goes.
When the cursor loop ends, i.e. all data has been consumed by your query, the function can perform a TRUNCATE table.
To select from the function use the following syntax;
SELECT *
FROM TABLE(my_function)
See the following Oracle documentation for information pipelined functions - https://docs.oracle.com/cd/B28359_01/appdev.111/b28425/pipe_paral_tbl.htm
This cannot be done inside a package, because " this must be done using sql only and not plsql". A package is PL/SQL.
However it is very simple. You want two things: select the table data and delete it. Two things, two commands.
select * from mytable;
truncate mytable;
(You could replace truncate mytable; with delete from mytable;, but this is slower and needs to be followed by commit; to confirm the deletion and end the transaction.)
Without pl/sql it's not possible.
Using pl/sql you can create a function which will populate a row, and then delete
Here is example :
drop table tempdate;
create table tempdate as
select '1' id from dual
UNION
select '2' id from dual
CREATE TYPE t_tf_row AS OBJECT (
id NUMBER
);
CREATE TYPE t_tf_tab IS TABLE OF t_tf_row;
CREATE OR REPLACE FUNCTION get_tab_tf RETURN t_tf_tab PIPELINED AS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
FOR rec in (select * from tempdate) LOOP
PIPE ROW(t_tf_row(rec.id));
END LOOP;
delete from tempdate ; commit;
END;
select * from table(get_tab_tf) -- it will populate and then delete
select * from tempdate --you can check here result of deleting
you can use below query
select * from Table_demo delete from Table_demo
The feature you seek is SERIALIZABLE ISOLATION LEVEL. This feature enables repeatable reads, which in particular guarantee that both SELECTand DELETEwill read and process the same identical data.
Example
Alter session set isolation_level=serializable;
select * from tempdate;
--- now insert from other session a new record
delete from tempdate ;
commit;
-- re-query the table old records are deleted, new recor preserved.

How to use query created in stored procedure in other procedure

sql procedure creates temporary table which should used in other sql procedure.
I tried
CREATE or replace FUNCTION f_createquery()
RETURNS TABLE ( kuupaev date
) AS $f_createquery$
-- actually this is big and time consuming select statement which should evaluated only once:
select current_date as kuupaev
$f_createquery$ LANGUAGE sql STABLE;
CREATE or replace FUNCTION f_usequery()
RETURNS TABLE ( kuupaev date
) AS $f_usequery$
-- big query tehing is used several times in query:
select kuupaev from tehing
union all
select kuupaev+1 from tehing
union all
select kuupaev+2 from tehing
$f_usequery$ LANGUAGE sql STABLE;
with tehing as (
select * from f_createquery() _
)
select * from f_usequery() _
but got error
ERROR: relation "tehing" does not exist
tehing contains temporary data created by stored procedure and it does not exist in database. How to allow other stored procedure to use it ?
How to fix it for Postgres 9.1+ ?
Is there simething like
external table (kuupaev date)
which allows to define external tables?
In real applicaton select statement in f_createquery is big and time consuming and should evaluated only once.
Replacing select * from tehing to dynamic sql in f_usequery() probably works but this prevents procedure compiling at run time. Is there better solution or can tehing passed in better way to other stored procedure, e.q. like parameter ?
Or should f_createquery create temporary table with fixed name tehing?
If f_createquery() produces the temporary table tehing then it should not return anything. Your function should look like:
CREATE FUNCTION f_createquery() RETURNS void AS $f_createquery$
CREATE TEMPORARY TABLE tehing AS
SELECT current_date AS kuupaev; -- complex query here
$f_createquery$ LANGUAGE sql STABLE;
You can then use the tehing table in the current session like any other table:
CREATE FUNCTION f_usequery() RETURNS TABLE (kuupaev date) AS $f_usequery$
SELECT kuupaev FROM tehing
UNION ALL
SELECT kuupaev+1 FROM tehing
UNION ALL
SELECT kuupaev+2 FROM tehing;
$f_usequery$ LANGUAGE sql STABLE;
Note that the temporary table is dropped at the end of the session.
You can also integrate the 2 functions so you can call f_usequery() without worrying about calling another function first:
CREATE FUNCTION f_usequery() RETURNS TABLE (kuupaev date) AS $f_usequery$
BEGIN
PERFORM 1 FROM information_schema.tables WHERE table_name = 'tehing';
IF NOT FOUND THEN -- temp table tehing does not exist
CREATE TEMPORARY TABLE tehing AS
SELECT current_date AS kuupaev; -- etc, etc
END IF;
RETURN QUERY
SELECT kuupaev FROM tehing
UNION ALL
SELECT kuupaev+1 FROM tehing
UNION ALL
SELECT kuupaev+2 FROM tehing;
END; $f_usequery$ LANGUAGE plpgsql STABLE;
Note that this is now a plpgsql function so the syntax is slightly different.
The construct
with tehing as (
select * from f_createquery() _
)
select * from f_usequery() _
won't work, because you re-declare tehing as the result of the CTE. Instead, f_usequery() works with the tehing temporary table and you can select from it or do further analysis with the result from f_usequery():
SELECT f_createquery(); -- this creates the tehing temporary table
SELECT * FROM f_usequery(); -- this operates on the tehing table and returns some results
SELECT *
FROM tableX
JOIN f_usequery() USING (kuupaev)
WHERE kuupaev < '2015-09-19';
Simple case: CTE
If you can do everything in a single query, you typically don't need a temp table nor a function at all. A CTE does the job:
WITH tehing AS (
SELECT current_date AS kuupaev -- expensive query here
)
SELECT kuupaev FROM tehing
UNION ALL
SELECT kuupaev+1 FROM tehing
UNION ALL
SELECT kuupaev+2 FROM tehing;
If you need a temp table
A temporary table only makes sense if you actually have to run multiple queries using the result of the expensive query. Or if you need to create an index on the table or something.
Just try to create the temp table:
CREATE TEMP TABLE tehing AS
SELECT current_date AS kuupaev; -- expensive query here
If the table already exists you get an error, and that's fine as well.
Then run your queries using tehing.
If you really need a function that avoids this error (which I doubt):
CREATE FUNCTION f_create_tbl()
RETURNS text AS
$func$
BEGIN
IF to_regclass('pg_temp.tehing') IS NULL THEN -- temp table does not exist
CREATE TEMP TABLE tehing AS
SELECT current_date AS kuupaev; -- expensive query here
RETURN 'Temp table "tehing" created.';
ELSE
RETURN 'Temp table "tehing" already exists';
END IF;
END
$func$ LANGUAGE plpgsql;
Call:
SELECT f_create_tbl();
The function cannot be declared STABLE, it's a VOLATILE function (default).
A test like this would be subtly inaccurate:
PERFORM 1 FROM information_schema.tables WHERE table_name = 'tehing';
It would find any table named "tehing" in the search path. But you are only interested if a temporary table of that name exists. The temporary schema is first in the search path by default:
How does the search_path influence identifier resolution and the "current schema"
Related:
How to check if a table exists in a given schema

PL/SQL - Returning a table from a function

I am creating a function that returns a table as a result. This function is basically doing comparison between two tables, and returns data set that if two tables contain different data.
P_min_id and p_max_id are ignorable, and will be implemented in the future once this function is working.
I modified the original code from http://www.adp-gmbh.ch/ora/plsql/coll/return_table.html. The error messages are commented in the following code.
create or replace function return_objects(
p_min_id in number,
p_max_id in number
)
return t_nested_table as
v_ret t_nested_table;
begin
select * // Error(8,7): PL/SQL: SQL Statement ignored
into
v_ret
from
(
select
*
from
(
select * from
(
select * from SCHEMA.TEST
minus
select * from SCHEMA.TEST_1
)
union all
select * from
(
select * from SCHEMA.TEST_1
minus
select * from SCHEMA.TEST
)
)
)
return v_ret;
end return_objects;
It would be appreciated if you can fix my code, but I want to know why this code should not work. Please give me some keyword, so that I can research. or relevant website for reference would be also appreciated.
Add a semicolon after the end of the SQL statement and use bulk collect to populate the nested table. Here's a working example:
create or replace type t_nested_table is table of varchar2(100);
create or replace function return_objects(
p_min_id in number,
p_max_id in number
)
return t_nested_table as
v_ret t_nested_table;
begin
select *
bulk collect into v_ret
from
(
select 'A' from dual union all
select 'B' from dual
);
return v_ret;
end;
/
select return_objects(1,2) from dual;
UPDATE
Based on the question edits there is also a privilege issue. Your user probably has access to the tables through a role, but to create a function those privileges should be granted directly to your user.