how to run stored procedure in oracle with loop - oracle

i want to call the procedure through anon block like
begin t_maha_del_22_06_p('22.06.2020');end;
but i run it once and want call with loop to take a large date time
like from first 1-st to 15-th august. How can i do it ?
create table t_maha_delete_22_06
(dt date,text varchar2(100));
create or replace procedure my_sch.t_maha_del_22_06_p(p_dt in date default trunc(sysdate) -1) as
begin
delete from t_maha_delete_22_06
where trunc(dt) = p_dt;
commit;
insert into t_maha_delete_22_06
select
trunc(p_dt) dt,
'blablabla' text from dual
commit;
end;

You can do it in loop as follows:
begin
For dt in (select date '2020-08-01' + level - 1 as dates
From dual
Connect by level <= date '2020-08-15' - date '2020-08-01')
Loop
t_maha_del_22_06_p(dt.dates);
End loop;
end;
/

Related

how can i put a query in decode function?

create table ss( no number, filepath varchar2(300) )
I want to have 5 or less duplicate values of 'no' in this table
when select count(no) from ss where no=#{no} <5, insert into ss values({no},{filepath})
so duplicate values of 'no' can't be over 5.
how can i do this?
You could create a similar trigger to implement this logic:
CREATE OR REPLACE TRIGGER set_no_ss_tbl_trg
BEFORE INSERT ON ss_tbl
FOR EACH ROW
BEGIN
DECLARE
l_cnt_no NUMBER;
BEGIN
SELECT COUNT(1)
INTO l_exceeding
FROM g_piece
WHERE refdoss = :new.no;
IF l_cnt_no > 5 THEN
SELECT MIN(no)
INTO :new.no
FROM (SELECT COUNT(1), no
FROM ss_tbl
GROUP BY no
HAVING COUNT(1) + 1 <= 5);
END IF;
END;
END;

oracle loop over date

I am having difficulties setting up a loop in Oracle. I have a table where values are stored for several days. Now I want to get the average of these values for each day.
I was attempting to set up a loop like this:
DECLARE
BEGIN
For iDay in 01.03.20, 02.03.20, 03.03.20
LOOP
SELECT
avg(values)
FROM
table
WHERE
date = 'iDay'
END LOOP;
END
You can simply get the average value using the following query:
SELECT DATE,
AVG (values)
FROM table
WHERE DATE BETWEEN DATE '2020-03-01' AND DATE '2020-03-03';
Or if you want to use the loop then use the query in FOR loop IN clause as follows:
SQL> DECLARE
2 BEGIN
3 FOR DATAS IN (
4 SELECT DATE '2020-03-01' + LEVEL - 1 DT
5 FROM DUAL CONNECT BY
6 LEVEL <= DATE '2020-03-03' - DATE '2020-03-01' + 1
7 ) LOOP
8 DBMS_OUTPUT.PUT_LINE(DATAS.DT);
9 -- YOUR_CODE_HERE
10 END LOOP;
11 END;
12 /
01-MAR-20
02-MAR-20
03-MAR-20
PL/SQL procedure successfully completed.
SQL>
One option would be using Dynamic Query within PL/SQL :
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
v_result NUMBER;
BEGIN
For iDay in 0..2
LOOP
EXECUTE IMMEDIATE 'SELECT AVG(values) FROM mytable WHERE mydate = :i_Day '
INTO v_result;
USING iDay + date'2020-03-01';
DBMS_OUTPUT.PUT_LINE( v_result );
END LOOP;
END;
/
Why not simply
SELECT "date", avg(values)
FROM "table"
WHERE "date" between DATE '2020-03-01' and DATE '2020-03-03'
GROUP by "date";
Note, date and table are reserved words, most likely the query will without quotes.

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;
/

Are cursors necessary for queries in a procedure?

I'm rather new to Oracle and I was asked to write a procedure to query some data from a table. I built it with 2 arguments, a cursor and a number. Essentially I have:
PROCEDURE PROC_NAME (
cursor_name IN OUT NOCOPY MY_DEFINED_CURSOR_TYPE,
a_number IN NUMBER);
AS
BEGIN
OPEN CURSOR_NAME FOR
SELECT
column
FROM
table
WHERE
table.dat_value > (SYSDATE - a_number);
END PROC_NAME;
It works like a charm, and I'm able to fetch the column from the cursor. My problem is that the requester doesn't want to pass in a cursor, they just want to pass in the number. I've never created a procedure that doesn't use a cursor to return the values of a query and the examples I have seen only ever do it that way. Is this possible?
You can use a collection:
CREATE PROCEDURE PROC_NAME (
a_number IN NUMBER,
numbers OUT SYS.ODCINUMBERLIST
)
AS
BEGIN
SELECT number_value
BULK COLLECT INTO numbers
FROM table_name
WHERE date_value > (SYSDATE - a_number);
END PROC_NAME;
Also, if you don't want to pass in a cursor then you can just pass one out:
CREATE OR REPLACE PROCEDURE PROC_NAME (
a_number IN NUMBER,
numbers OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN numbers FOR
SELECT number_value
FROM table_name
WHERE date_value > (SYSDATE - a_number);
END PROC_NAME;
Use a function instead ? But it's just a "stylistic" difference compared to procedure out parameter. Anyway the returned value have to be implicitly passed (unlike in SQL Server as noted by #ShannonSeverance).
function f(
p_days in number
) return my_defined_cursor_type is
v_cur my_defined_cursor_type;
begin
open v_cur for
select
column
from
table
where
table.dat_value > (sysdate - p_days);
return v_cur;
end;
/
Usage
declare
v_cur my_defined_cursor_type := f(42);
begin
-- use v_cur as you like
end;
If you want to apply some PL/SQL logic, but remain using select for querying the data (i.e not pass in a cursor - use pipelined functions.
You need to define the TYPEs of the result row and table; FETCH the cursor and PIPE the results in the function.
CREATE or replace type MY_DEFINED_ROW_TYPE as object
(
txt VARCHAR2(30)
);
/
create or replace type MY_DEFINED_TABLE_TYPE as table of MY_DEFINED_ROW_TYPE
/
create or replace function FUN_NAME( a_number IN NUMBER) return
MY_DEFINED_TABLE_TYPE
PIPELINED
as
cur MY_DEFINED_CURSOR_TYPE;
v_txt varchar2(30);
begin
OPEN cur
FOR
SELECT
column
FROM table
WHERE table.dat_value > (SYSDATE - a_number);
LOOP
FETCH cur INTO v_txt;
EXIT WHEN cur%NOTFOUND;
pipe row(v_txt);
END LOOP;
return;
end;
/
The usage:
select * from table (FUN_NAME(2));

Arithmetic operations on Date values in Oracle

I am struggling with some case...
there is table, in which I have employee attendance records, for example:
for empID=1;
empID time Type date
-------------------------------
1 9:22 in sameday
1 11:23 out sameday
1 14:35 in sameday
1 16:21 out sameday
particularly, I want some fn/procedure that will take an EmpID and DATE parameters, and then based on this data if I'll write: select proc(EmployeeID, Date) from dual(or maybe some other table?) it should do such a work:
take first couples in table (table is ordered be ascending as default order), then calculate FROM first OUT (11:23) to first IN(9:22) time, save that time somewhere (int tempResult) and then calculate second couple, and calculate second tempResult and in finalResult, it should count the total time, like finalResult+=finalResult+tempResult (if it has been an iterations in loop);
I think it would be done someway like, in foreach (or whatever it is in pl/sql oracle) take first select with top result, then, second.. and so forth... and on each iteration calculate desire goal.
so.. logics is ok with me I think :), but the problem is that I'm not that familiar with PL/SQL, if it had been written in Java it should have come easy to me.
I will pay lots of Thanks to some one who will help me...
its crucial for me to day.
thanks in advance.
I have Date and Time is separate columns, like:
date time
----------------------
11-09-2013 12:34
so, I made little change like this
FOR rec IN
( SELECT t.EID, to_char(t.devent_date, 'DD.MM.YY') ||' '|| t.RegTime, t.acttype from turnicate_ie t WHERE t.EID = i_emp_id ORDER BY t.EID
)
LOOP
but it states that package or function is in incorrect state...
(t.devent_date is 11.09.2013, t.RegTime is 16:23)
The below will give you some idea of using plsql:
you need to use many logic of calculating total working hours, like multiple inputs within same time, multiple empId etc.
create table my_test ( empId number, log_time date, type varchar2(3));
INSERT INTO my_test VALUES( 1, to_date('11/sep/2013 09:22:00 am', 'dd/mon/yyyy hh:mi:ss am'), 'in');
INSERT INTO my_test VALUES( 1, to_date('11/sep/2013 11:23:00 am', 'dd/mon/yyyy hh:mi:ss am'), 'out');
INSERT INTO my_test VALUES( 1, to_date('11/sep/2013 02:35:00 pm', 'dd/mon/yyyy hh:mi:ss pm'), 'in');
INSERT INTO my_test VALUES( 1, to_date('11/sep/2013 04:21:00 pm', 'dd/mon/yyyy hh:mi:ss pm'), 'out');
CREATE OR REPLACE
FUNCTION total_hours(
i_emp_id IN NUMBER)
RETURN VARCHAR2
IS
l_total_seconds NUMBER := 0;
in_time DATE;
l_total_time VARCHAR2(20);
BEGIN
FOR rec IN
( SELECT log_time, type FROM my_test WHERE empid = i_emp_id ORDER BY log_time
)
LOOP
IF rec.TYPE = 'in' AND in_time IS NULL THEN
in_time := rec.log_time;
END IF;
IF rec.TYPE = 'out' AND in_time IS NOT NULL THEN
l_total_seconds := (rec.log_time - in_time)*24*60*60 + l_total_seconds;
in_time := NULL;
END IF;
END LOOP;
SELECT TO_CHAR(TRUNC(l_total_seconds/3600), 'FM999999990')
|| 'hh '
|| TO_CHAR(TRUNC(mod(l_total_seconds,3600)/60), 'FM00')
|| 'mm '
|| TO_CHAR(mod(l_total_seconds,60), 'FM00')
||'ss'
INTO l_total_time
FROM dual;
RETURN l_total_time;
END;
/
SELECT total_hours(1) from dual;

Resources