Oracle issue joining pipelined data - oracle

am trying to set up a test CASE, which calls a pipelined function that returns dates based on a DATE range. My goal is to have a row for each date.
I am getting the following error (see below) when I am trying to create the procedure. I know I have to JOIN the passed in p_id with the results returned back from the function but I can't seem to figure out how since there is no link to JOIN them with.
Can someone provide the correct code to fix this issue and explain what I did wrong. Thanks in advance to all who answer and your expertise.
Errors: PROCEDURE CREATE_DATA
Line/Col: 8/10 PL/SQL: SQL Statement ignored
Line/Col: 11/13 PL/SQL: ORA-00936: missing expression
CREATE OR REPLACE TYPE nt_date IS TABLE OF DATE;
CREATE OR REPLACE FUNCTION generate_dates_pipelined(
p_from IN DATE,
p_to IN DATE
)
RETURN nt_date PIPELINED DETERMINISTIC
IS
v_start DATE := TRUNC(LEAST(p_from, p_to));
v_end DATE := TRUNC(GREATEST(p_from, p_to));
BEGIN
LOOP
PIPE ROW (v_start);
EXIT WHEN v_start >= v_end;
v_start := v_start + INTERVAL '1' DAY;
END LOOP;
RETURN;
END generate_dates_pipelined;
CREATE TABLE data(
d_id NUMBER(6),
d_date DATE
);
CREATE OR REPLACE PROCEDURE create_data (
p_id IN NUMBER,
p_start_date IN DATE,
p_end_date IN DATE
)
IS
BEGIN
INSERT INTO data (d_id, d_date)
VALUES
(p_id,
TABLE( generate_dates_pipelined(p_start_date, p_end_date)
) c
);
END;
EXEC create_data (1, DATE '2021-08-21', DATE '2021-08-30');

Your table expects a single DATE per row; you are trying to provide a collection of dates per row.
You need to INSERT for each row of the collection and can do that with a SELECT statement:
CREATE OR REPLACE PROCEDURE create_data (
p_id IN NUMBER,
p_start_date IN DATE,
p_end_date IN DATE
)
IS
BEGIN
INSERT INTO data (d_id, d_date)
SELECT p_id,
COLUMN_VALUE
FROM TABLE(generate_dates_pipelined(p_start_date, p_end_date));
END;
/

Related

PLS-00382: expression is of wrong type - when using an array as a var in oracle function

I get 'expression is of wrong type' error when I'm trying to execute the following sql procedure:
DECLARE
v_date TIMESTAMP(6) := to_date('03-11-2011', 'dd-mm-yyyy');
BEGIN
for newiD in (select tabA.id from tabA where tabA.prop = 1)
loop
insert into tabB values (newId, 'testval', 'testval', newId, v_date);
end loop;
END;
Is there another way I can declare the array I'm trying to store into the newId variable?
Update: The data type of the tabA.id and the next tabB.newId is the same - NUMBER(19,0)
A better way, with no loops and just SQL, could be:
DECLARE
v_date TIMESTAMP(6) := to_date('03-11-2011', 'dd-mm-yyyy');
BEGIN
insert into tabB /* here it would be better to list the columns */
select tabA.id, 'testval', 'testval', tabA.id, v_date
from tabA
where tabA.prop = 1
END;
In this case newId is a record wich has one field: id. That's why you need to put newId.id in your insert clause.

PLS-00306: wrong number or types of arguments in call to 'ADD_MONTHS'

I have created the below procedure to only retain the last two months data only and delete the rest one against a table in oracle, below is the procedure but i am getting exception, please advise how to overcome from this
create or replace package TEST_TABLE AS
PROCEDURE TEST_TABLE;
END TEST_TABLE;
create or replace PACKAGE BODY TEST_TABLE AS
PROCEDURE TEST_TABLE IS
BEGIN
FOR cc IN
(
SELECT partition_name, high_value
FROM user_tab_partitions
WHERE table_name = 'TEST_TABLE'
)
LOOP
BEGIN
IF sysdate >= ADD_MONTHS(cc.high_value,2) THEN
EXECUTE IMMEDIATE
'ALTER TABLE TEST_TABLE DROP PARTITION ' || cc.partition_name;
Dbms_Output.Put_Line('Dropping partition is completed.');
END IF;
END;
END LOOP;
EXCEPTION WHEN Others THEN Dbms_Output.Put_Line( SQLERRM );
END TEST_TABLE;
END TEST_TABLE;
The error that I am getting is:
Error(12,6): PL/SQL: Statement ignored
Error(12,20): PLS-00306: wrong number or types of arguments in call to 'ADD_MONTHS'
Firstly, It's insane to call table name, package name and procedure name all by TEST_TABLE as being done by you, as if there's no other name available. I've named them appropriately.
HIGH_VALUE cannot be directly used in DATE related functions as it's of LONG TYPE. There's a simple method to convert it to date using dynamic SQL(EXECUTE IMMEDIATE)
CREATE OR replace PACKAGE BODY PKG_test_table AS
PROCEDURE pr_test_table
IS
v_high_value DATE;
BEGIN
FOR cc IN (
SELECT partition_name,
high_value
FROM user_tab_partitions
WHERE table_name = 'TEST_TABLE'
) LOOP
BEGIN
EXECUTE IMMEDIATE 'BEGIN :v_high_val := '|| cc.high_value || '; END;'
USING OUT v_high_value;
IF
SYSDATE >= add_months(v_high_value,2)
THEN
EXECUTE IMMEDIATE 'ALTER TABLE TEST_TABLE DROP PARTITION '
|| cc.partition_name;
dbms_output.put_line('Dropping partition is completed.');
END IF;
END;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(sqlerrm);
END pr_TEST_TABLE;
END PKG_test_table;
/
Calling the procedure
BEGIN
PKG_test_table.pr_test_table;
END;
/
Your procedure does not accept any parameter. You can't pass any arguments to it.
The HIGH_VALUE column from USER_TAB_PARTITIONS is a long data type, I'm not going copy code from another web site, but if you google "oracle convert high value to date" you should get some ideas on how to create a function that you can use to convert the 'long' to a date.
My reputation is too low to post this as a comment, so I added it as an answer, it should help though it is not a good answer :(
As the error says it all ADD_MONTHS takes a DATE and you are passing in as LONG.
Try something like this and it should be ok.
Example:
DECLARE
DT LONG(1000) := 'TO_DATE('||''''||'2018-08-01 00:00:00'||''''||',' ||''''|| 'SYYYY-MM-DD HH24:MI:SS'||''''||','||''''||'NLS_CALENDAR=GREGORIAN'||''''||')';
BEGIN
DBMS_OUTPUT.PUT_LINE(DT);
EXECUTE IMMEDIATE
'BEGIN
DBMS_OUTPUT.PUT_LINE(TO_CHAR(ADD_MONTHS('||DT||',2),'||''''||'YYYY-MM-DD HH24:MI:SS'||''''||
')); END;';
END;
Output:
TO_DATE('2018-08-01 00:00:00','SYYYY-MM-DD HH24:MI:SS','NLS_CALENDAR=GREGORIAN')
2018-10-01 00:00:00
Oracle does not allow functions over long such as cast, substr, add_months over long type however … read below.
Long type
describe user_tab_partitions;
...
SUBPARTITION_COUNT NUMBER
HIGH_VALUE LONG
HIGH_VALUE_LENGTH NUMBER
...
Function to convert long to varchar2
FUNCTION long_to_varchar2 ( p_table_owner IN VARCHAR2,p_table_name IN VARCHAR2, p_partition_name IN VARCHAR2) RETURN VARCHAR2
is
l_tmp long;
BEGIN
select high_value
into l_tmp
from all_tab_partitions
where table_owner = p_table_owner
and table_name = p_table_name
and partition_name = p_partition_name ;
RETURN l_tmp;
END long_to_varchar2;
3.Use your new function
select tpar."OWNER",tpar."TABLE_NAME",tpar."PART_MIN",tpar."PART_MIN_HV",tpar."PART_MAX",tpar."PART_MAX_HV",tpar."NR_PART"
,pkey.column_name as partitioned_by
,ptab.partitioning_type as partition_type
,ptab.status
from
(select p1.table_owner as owner
,p1.table_name
,pmin.partition_name as part_min
,to_date(substr(long_to_varchar2(p1.table_owner,p1.table_name,pmin.partition_name),11,10),'yyyy-mm-dd') as part_min_hv
,pmax.partition_name as part_max
,to_date(substr(long_to_varchar2(p1.table_owner,p1.table_name,pmax.partition_name),11,10),'yyyy-mm-dd') as part_max_hv
,p1.nr_part+1 as nr_part
from (select min(part.partition_position) as minp
,max(part.partition_position) as maxp
,count(*) as nr_part
,part.table_name
,part.table_owner
from all_tab_partitions part,
dba_tables tbls
where part.table_name=tbls.table_name
and part.table_owner=tbls.owner
and part.PARTITION_NAME <> 'P_CURRENT'
group by part.table_name, part.table_owner) p1
,all_tab_partitions pmin
,all_tab_partitions pmax
where p1.table_name = pmin.table_name
and p1.table_owner = pmin.table_owner
and p1.minp=pmin.partition_position
and p1.table_name = pmax.table_name
and p1.table_owner = pmax.table_owner
and p1.maxp = pmax.partition_position) tpar
,ALL_PART_KEY_COLUMNS pkey
,ALL_PART_TABLES ptab
where tpar.owner=pkey.owner
and tpar.table_name=pkey.name
and tpar.owner=ptab.owner
and tpar.table_name=ptab.table_name
and pkey.object_type='TABLE';
The only issue is that you will be doing an implicit varchar2 to date conversion and I see no way of doing it otherwise.

How to set default value of date input parameter in procedure if they are null?

I am having a Procedure , which is accepting fromDate and toDate input parameters I need to set default values as First date of Last month and last date of previous month respectively. I am trying to use below code, but still proper default dates are not being set. Please let me know if below code is valid or i can do something to correct it:
create or replace PROCEDURE "TEST"
(
fromdate_in IN varchar2,
todate_in IN varchar2,
type_in IN number DEFAULT 01
)
is
V_date varchar2(3000);
begin
select to_date(fromdate_in) into V_date from dual; -- Correct date entered
Exception WHEN Others THEN
select to_char(trunc(trunc(sysdate, 'MM') - 1, 'MM'),'DD/MM/RRRR') into V_date from dual; -- if fromdate_in --is null then set V_date to first date of Previous month
-- calculations using V_date
end TEST;
Please note , I have shown only how I am setting first date of Previous month to From Date for simplicity.
If you want to set a default value for a parameter, do so as in this example:
SQL> create or replace procedure p_test
2 (fromdate_in in varchar2
3 default to_char(trunc(add_months(sysdate, -1), 'mm'), 'dd/mm/rrrr'))
4 is
5 begin
6 dbms_output.put_Line('fromdate_in = ' || fromdate_in);
7 end;
8 /
Procedure created.
As you wanted, its default value is the first day of the previous month. Works like this:
SQL> set serveroutput on;
SQL> exec p_test;
fromdate_in = 01/08/2018
PL/SQL procedure successfully completed.
SQL> exec p_test('24/09/2018');
fromdate_in = 24/09/2018
PL/SQL procedure successfully completed.
SQL>
Note that you should really consider switching to DATE datatype parameter instead of VARCHAR2 - you're dealing with dates anyway, so - why bother with invalid values passed to the procedure? If it is a string, what will prevent someone from passing e.g. '99/66/x-FZ' to the procedure? Now you have to worry about it, write exception handler ... everything because you didn't set parameters' datatype to DATE.
You could do it much easier like this:
create or replace PROCEDURE "TEST"
(
fromdate_in IN DATE,
todate_in IN DATE,
type_in IN number DEFAULT 1
)
is
V_date DATE;
begin
V_date := NVL(fromdate_in, TRUNC(ADD_MONTHS(SYSDATE, -1), 'MM'));
-- calculations using V_date
end TEST;
If you are forced to use VARCHAR2 for fromdate_in then convert the value to DATE:
V_date := NVL(TO_DATE(fromdate_in, 'DD/MM/YYYY'), TRUNC(ADD_MONTHS(SYSDATE, -1), 'MM'));

ORACLE - Return cursor from stored procedure

I need to return a cursor object from a stored procedure, but I need to process data first.
For example, let's consider this simple stored procedure:
create or replace PROCEDURE TEST
(
query_str IN VARCHAR2,
CURSOR_ OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN CURSOR_ FOR query_str;
END;
This procedure returns data as is, with no postprocessing.
The improvement I need is the following:
process data coming from the execution of query_str;
return the processed data in the form of a cursor.
Anyone could suggest me a way to accomplish this?
Thanks
It is difficult to do what you are suggesting with dynamic SQL (unless all the statements you are passing to the procedure all have a similar output format). If you can know what the SELECT will be then you can store it in a collection and process it:
CREATE TABLE TEST_DATA ( id, name, dt ) AS
SELECT 1, 'A', SYSDATE FROM DUAL UNION ALL
SELECT 2, 'B', DATE '2017-01-01' FROM DUAL;
CREATE TYPE processed_data_obj AS OBJECT(
id INTEGER,
etag VARCHAR2(20)
);
/
CREATE OR REPLACE TYPE processed_data_table AS TABLE OF processed_data_obj;
/
CREATE OR REPLACE PROCEDURE TEST
(
CURSOR_ OUT SYS_REFCURSOR
)
AS
processed PROCESSED_DATA_TABLE;
BEGIN
-- Process it in the select statement
SELECT processed_data_obj(
id,
name || '_' || ROUND( ( dt - DATE '1970-01-01' ) * 24*60*60 )
)
BULK COLLECT INTO processed
FROM test_data;
-- Process it more in PL/SQL
FOR i IN 1 .. processed.COUNT LOOP
processed[i].etag := processed[i].etag || '_' || i;
END LOOP;
OPEN cursor_out FOR
SELECT *
FROM TABLE( processed );
END;
/

ORACLE PL/SQL for each passing tablename to procedure

I need to do a FOR EACH loop in a procedure, but I need to pass the table name dynamically.
This is the declaration
CREATE OR REPLACE PROCEDURE MIGRATE_PRIMITIVES_PROPS
(
FromTable IN VARCHAR2,
ToTable IN VARCHAR2
)
When I try and do this
FOR EachRow IN (SELECT * FROM FromTable) It says the table isn't valid
The table coming into the procedure is dynamic, columns are added and deleted all the time so I can't spell out the columns and use a cursor to populate them.
You have to use dynamic SQL to query a table whose name you don't know at compile time. You can do that with a dynamic cursor:
as
l_cursor sys_refcursor;
begin
open l_cursor for 'select * from ' || fromtable;
loop
fetch l_cursor into ...
... but then it breaks down because you can't define a record type to fetch into based on a weak ref cursor; and you don't know the column names or types you're actually interested in - you're using select * and have specific names to exclude, not include. You mentioned an inner loop that works and gets the column names, but there is no way to refer to a field in that cursor variable dynamically either.
So you have to work a bit harder and use the dbms_sql package instead of native dynamic SQL.
Here's a basic version:
create or replace procedure migrate_primitives_props
(
fromtable in varchar2,
totable in varchar2
)
as
l_cursor pls_integer;
l_desc_tab dbms_sql.desc_tab;
l_columns pls_integer;
l_value varchar2(4000);
l_status pls_integer;
begin
l_cursor := dbms_sql.open_cursor;
-- parse the query using the parameter table name
dbms_sql.parse(l_cursor, 'select * from ' || fromtable, dbms_sql.native);
dbms_sql.describe_columns(l_cursor, l_columns, l_desc_tab);
-- define all of the columns
for i in 1..l_columns loop
dbms_sql.define_column(l_cursor, i, l_value, 4000);
end loop;
-- execute the cursor query
l_status := dbms_sql.execute(l_cursor);
-- loop over the rows in the result set
while (dbms_sql.fetch_rows(l_cursor) > 0) loop
-- loop over the columns in each row
for i in 1..l_columns loop
-- skip the columns you aren't interested in
if l_desc_tab(i).col_name in ('COL_NAME', 'LIB_NAME', 'PARTNAME',
'PRIMITIVE', 'PART_ROW')
then
continue;
end if;
-- get the column value for this row
dbms_sql.column_value(l_cursor, i, l_value);
-- insert the key-value pair for this row
execute immediate 'insert into ' || totable
|| '(key, value) values (:key, :value)'
using l_desc_tab(i).col_name, l_value;
end loop;
end loop;
end;
/
I've assumed you know the column names in your ToTable but still used a dynamic insert statement since that table name is unknown. (Which seems strange, but...)
Creating and populating sample tables, and then calling the procedure with their names:
create table source_table (col_name varchar2(30), lib_name varchar2(30),
partname varchar2(30), primitive number, part_row number,
col1 varchar2(10), col2 number, col3 date);
create table target_table (key varchar2(30), value varchar2(30));
insert into source_table (col_name, lib_name, partname, primitive, part_row,
col1, col2, col3)
values ('A', 'B', 'C', 0, 1, 'Test', 42, sysdate);
exec migrate_primitives_props('source_table', 'target_table');
End up with the target table containing:
select * from target_table;
KEY VALUE
------------------------------ ------------------------------
COL1 Test
COL2 42
COL3 2015-05-22 15:29:31
It's basic because it isn't sanitising the inputs (look up the dbms_assert package), and isn't doing any special handling for different data types. In my example my source table had a date column; the target table gets a string representation of that date value based on the calling session's NLS_DATE_FORMAT setting, which isn't ideal. There's a simple but slightly hacky way to get a consistent date format, and a better but more complicated way; but you may not have date values so this might be good enough as it is.

Resources