I have the following SQL-Script:
PROCEDURE "P_OVERVIEW" ()
LANGUAGE SQLSCRIPT
READS SQL DATA
AS
BEGIN
DECLARE I INT = 0;
WHILE :I < 24 DO
SELECT A, B, COUNT(A) AS COUNT, LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP, -:I)) AS "DATE", I
FROM Table
WHERE LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP, -:I)) BETWEEN ENTRY_DATE AND VALID_UNTIL
GROUP BY A, B
ORDER BY B, A;
I := I+1;
END WHILE;
END;
I am using it in an .hdbprocedure-File on SAP Cloud Platform. Deploying is working fine.
When I call the procedure with CALL P_OVERVIEW() I am just getting the values when I = 0. Any Idea wy this happend?
There is a small typo in the code.
When referencing the value of a varible, I you have to put a colon : in front: :I.
The following should work
PROCEDURE "P_OVERVIEW" ()
LANGUAGE SQLSCRIPT
READS SQL DATA
AS
BEGIN
DECLARE I INT = 0;
WHILE :I < 24 DO
SELECT A, B, COUNT(A) AS COUNT, LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP, -:I)) AS "DATE", I
FROM Table
WHERE LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP, -:I)) BETWEEN ENTRY_DATE AND VALID_UNTIL
GROUP BY A, B
ORDER BY B, A;
I := :I+1;
END WHILE;
END;
As a remark: using a loop here is probably not the best approach.
You could just create a subquery, that generates the "last day of the month" for the past two years on the fly.
The SERIES_GENERATE_ generator table function is an easy option for that.
Related
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.
In SQL Server, I can just use 'RETURNS TABLE' and it will do the job. But I can't find how to do the same in Oracle SQL
I have the following SELECT statement that needs to be put in a function or procedure:
SELECT a.CodAcord, a.Descr
FROM FreqSoce f
LEFT JOIN Acord a ON a.CodAcord = f.CodAcord
WHERE f.codSoce = codSoce;
codSoce is an INTEGER IN parameter, and I need to return a.CodAcord and a.Descr as result from my function/procedure.
Is there a simple way to do this? Without having to deal with temp variables and/or advanced content...
EDIT: Aditional info:
- I need to return a.CodAcord and a.Descr, but when I did some research to know how to return more than one variable using SQL Functions or Procedures, all I could find was that this was only possible by returning a TABLE. If there's a way to return more than one item from a Function or Procedure, please let me know.
- The use of Functions or Procedures is strictly required.
- I'm using SQL Developer.
You can achieve a table as a return value from function by using a pipelined table function. Please see the example below:
-- Create synthetic case
CREATE TABLE Acord AS
SELECT rownum CodAcord, 'Description ' || rownum Descr
FROM dual CONNECT BY LEVEL <= 5;
CREATE TABLE FreqSoce AS
SELECT rownum CodSoce, rownum CodAcord
FROM dual CONNECT BY LEVEL <= 10;
-- Test dataset
SELECT a.CodAcord, a.Descr
FROM FreqSoce f
LEFT JOIN Acord a ON a.CodAcord = f.CodAcord
WHERE f.CodSoce = 10;
-- Here begins actual code
-- Create an object type to hold each table row
CREATE OR REPLACE TYPE typ_acord AS OBJECT(
CodAcord NUMBER,
Descr VARCHAR2(40)
);
/
-- Create a collection type to hold all result set rows
CREATE OR REPLACE TYPE tab_acord AS TABLE OF typ_acord;
/
-- Our function that returns a table
CREATE OR REPLACE FUNCTION getAcord(pCodSoce IN NUMBER)
RETURN tab_acord PIPELINED
AS
BEGIN
FOR x IN (SELECT a.CodAcord, a.Descr
FROM FreqSoce f
LEFT JOIN Acord a ON a.CodAcord = f.CodAcord
WHERE f.CodSoce = pCodSoce)
LOOP
PIPE ROW (typ_acord(x.CodAcord, x.Descr));
END LOOP;
END;
/
-- Testing the function (please note the TABLE operator)
SELECT * FROM TABLE(getAcord(5));
Take the following as a code template:
CREATE OR REPLACE PACKAGE tacord AS
TYPE ttabAcord IS TABLE OF ACord%ROWTYPE;
END tacord;
/
show err
CREATE OR REPLACE PACKAGE BODY tacord AS
BEGIN
NULL;
END tacord;
/
show err
CREATE OR REPLACE FUNCTION demo RETURN tacord.ttabAcord AS
to_return tacord.ttabAcord;
BEGIN
SELECT a.*
BULK COLLECT INTO to_return
FROM FreqSoce f
LEFT JOIN Acord a ON a.CodAcord = f.CodAcord
WHERE f.codSoce = codSoce
;
RETURN to_return;
END demo;
/
show err
Key points:
%ROWTYPE represents the datatype of a database table's record
BULK COLLECT INTO inserts a complete result set into a plsql data structure
Iteration over the table contents is availabel by the plsql collection methods, namely .FIRST, .LAST, .NEXT.
So i'm having a little trouble with some PL SQL statements. Essentially i'm trying to create a procedure that will check that when a new tuple is inserted, the procedure checks that there isn't another contract for the same person within the same dates ie. the dates of the new contract don't overlap the dates of the other.
Here is the code:
CREATE OR REPLACE PROCEDURE dateOrder
(name IN VARCHAR2, start IN DATE, end IN DATE)
IS
x number;
y number;
BEGIN
CREATE OR REPLACE VIEW PersonContracts AS
SELECT * FROM ContractInfo WHERE HasContract=name;
SELECT COUNT(*) INTO x FROM PersonContracts
WHERE start BETWEEN date_from AND date_to;
SELECT COUNT(*) INTO y from PersonContracts
WHERE end BETWEEN date_from AND date_to;
IF x > 0 THEN
dbms_output.put_line("overlaps.");
END IF;
IF Y > 0 THEN
dbms_output.put_line("overlaps.");
END IF;
END dateOrder;
/
BEGIN
dateOrder("John Smith", "08-oct-2014", "12-oct-2014");
END;
I have tried it with or without the view but i would prefer to keep the view if possible. I'm only new at PL!
You can't CREATE a VIEW inside a procedure using DDL (you would have to use EXECUTE IMMEDIATE to do so).
I would prefer to set the WHERE-Clause of the SELECT statement directly:
CREATE OR REPLACE PROCEDURE dateOrder (name IN VARCHAR2, start IN DATE, end IN DATE)
IS
x number;
y number;
BEGIN
SELECT COUNT(*) INTO x FROM ContractInfo WHERE HasContract=name
AND start BETWEEN date_from AND date_to;
SELECT COUNT(*) INTO y from ContractInfo WHERE HasContract=name
AND end BETWEEN date_from AND date_to;
IF x > 0 THEN
dbms_output.put_line("overlaps.");
END IF;
IF Y > 0 THEN
dbms_output.put_line("overlaps.");
END IF;
END dateOrder;
/
BEGIN
dateOrder("John Smith", "08-oct-2014", "12-oct-2014");
END;
So a few things will not work in you procedure. Take this as recommendation not as a solution:
It is not a good style to code a ddl within a procedure. And by the way to access the new view within this procedure is impossible!!
If you want to do so, put the Create View in a dynamic SQL statement like the code snippet below
All the DB Objects on which you want to access from the procedure, have to exist at compile time. So this code will never work unless you write all your Select statements also in dynamic SQL.
Don't name your parameters "start" or "end". Theese are reserved words and is therefor not allowed.
If you call the dateOrder procedure make sure that you will pass a valid date as parameters. In your example you will pass strings. Maybe this will work with your default NLS but in another environment/database it may not.
Check this out:
CREATE OR REPLACE PROCEDURE dateOrder
(name IN VARCHAR2, xstart IN DATE, xend IN DATE)
IS
x number;
y number;
BEGIN
execute immediate (
'CREATE OR REPLACE VIEW PersonContracts AS
SELECT * FROM ContractInfo ....'
);
-- that won't work, because the PersonContracts will be not there at compile time.
SELECT COUNT(*) INTO x FROM PersonContracts
WHERE start BETWEEN date_from AND date_to;
SELECT COUNT(*) INTO y from PersonContracts
WHERE end BETWEEN date_from AND date_to;
IF x > 0 THEN
dbms_output.put_line("overlaps.");
END IF;
IF Y > 0 THEN
dbms_output.put_line("overlaps.");
END IF;
END dateOrder;
BEGIN
dateOrder("John Smith", "08-oct-2014", "12-oct-2014");
END;
The view is unnecessary even if it was allowed. You want to examine only rows that has HasContract values that are equal to the name parameter. Fine, write the query just as you want it then add HasContract = name to the where clause. Don't over-think simple solutions.
Also, you can find out what you need in one query. The condition you want to catch is if there is any overlap between the interval defined by the start and stop dates and any existing start and stop dates. While we could painstakingly list out every conceivable arrangement that would lead to an overlap, let's look at the only two arrangements that don't lead to an overlap.
if the end date of one is less than or equal to the start date of the other or
if the start date of one is greater than or equal to the end data of the other.
Or, in equation form e1 <= s2 or s1 >= e2. A little Boolean magic and we can invert to e1 > s2 and s1 < e2. That gives us the simplified query:
select COUNT(*) into y
from ContractInfo
where HasContract = name
and p_end > date_from
and p_start < date_to;
If this query returns any non-zero answer, there will be an overlap somewhere. One simple query, one check afterwards. Easy.
In SAP HANA I wish to have a view which has a range of number from 1 to 10, or 1 to n where n is any number. So when I select from the view I can select n records to get the first n records from the range.
I was able to create a table with 1000 rows with a ID that increment's by using this stored procedure. Is there an easier way?
DROP PROCEDURE "DEMO_PROC";
CREATE PROCEDURE "DEMO_PROC"(
IN ID INTEGER )
LANGUAGE SQLSCRIPT AS
/*********BEGIN PROCEDURE SCRIPT ************/
BEGIN
DECLARE
START_ID INTEGER;
DROP TABLE TEST_TABLE;
CREATE COLUMN TABLE "TEST_TABLE" (ID INTEGER, NAME VARCHAR(10));
START_ID := 0;
WHILE START_ID < 1000 DO
START_ID := START_ID + 1;
INSERT INTO "TEST_TABLE" VALUES(:START_ID, '');
END WHILE;
END;
CALL "DEMO_PROC"(1);
SELECT * FROM "TEST_TABLE";
Using a generator is the prefered way:
INSERT INTO "TEST_TABLE" SELECT GENERATED_PERIOD_START as ID, '' as NAME from SERIES_GENERATE_INTEGER(1,1,1001);
is much easier and faster.
I think for loop is easier than while.
FOR START_ID IN 1..1000 DO
INSERT INTO "TEST_TABLE" VALUES(START_ID,'');
END FOR;
I have a Oracle DB with a table called myC. In this table I have a few row, two of them called myCheight, myCwidth.
I need to read these values and compare them like in IF myCheight > myCwidth DO switch the values.
I tried to read values from one row but didnt get it to work. I use Oracles Oracle SQL Developer.
This is what i came up with so far:
set serveroutput on;
DECLARE
cursor h is select * from MyC;
type htype is table of h%rowtype index by number;
stage_tab htype;
master_tab htype;
BEGIN
open h;
loop
fetch h bulk collect into stage_tab limit 500;
for i in 1 .. stage_tab.count loop
master_tab(stage_tab(i).id) := stage_tabe(i);
end loop;
exit when h%notfound;
end loop;
close h;
end;
Can't you just do this?
UPDATE myC
SET myCheight = myCwidth,
myCwidth = myCheight
WHERE myCheight > myCwidth