pl/sql block, weird errors with simple script - oracle

I'm writing very simple block code in pl/sql:
DECLARE
dateof21 DATE;
dzien number;
dzien_tyg number;
BEGIN
dateof21:= '2001-01-01';
WHILE dateof21 != '2101-01-01' LOOP
SELECT EXTRACT(day from date dateof21) INTO dzien from dual;
select to_char(date dateof21,'D') INTO dzien_tyg from dual;
if ((dzien=13) AND (dzien_tyg=5)) THEN
dbms_output.put_line(to_char(dateof21));
end if;
dateof21:= dateof21+1;
END LOOP;
END;
but i'm getting very annoying errors:
ORA-06550: linia 8, kolumna 26:
PL/SQL: ORA-00936: brak wyrażenia
ORA-06550: linia 8, kolumna 2:
PL/SQL: SQL Statement ignored
ORA-06550: linia 9, kolumna 17:
PL/SQL: ORA-00936: brak wyrażenia
ORA-06550: linia 9, kolumna 2:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
I really tried to find whats wrong, but everything seems just fine. Can anybody help? it should write on output all fridays which are 13th day of month btw.

There are many things wrong in your code:
First: you should use proper date literals. I prefer ANSI literals, like DATE '2001-01-01' but you can also use the to_date()function: `to_date('2001-01-01', 'yyyy-mm-dd')
Second: to_char() returns a varchar value, not a number. So you can't assign the result of that to a number variable, you need to use to_number(to_char(dateof21,'D')).
You also don't need to use select ... into to call a function.
And finally: the extract() method does not require the use of a date prefix: EXTRACT(day from dateof21)
Putting that all together, gives us:
DECLARE
dateof21 DATE;
dzien number;
dzien_tyg number;
BEGIN
dateof21 := date '2001-01-01';
-- alternatively: dateof21 := to_date('2001-01-01', 'yyyy-mm-dd');
WHILE dateof21 <> DATE '2101-01-01'
LOOP
dzien := EXTRACT(day from dateof21);
dzien_tyg := to_number(to_char(dateof21,'D'));
if ((dzien=13) AND (dzien_tyg=5)) THEN
dbms_output.put_line(to_char(dateof21, 'yyyy-mm-dd'));
end if;
dateof21 := dateof21+1;
END LOOP;
END;
/
Note that the return value of to_char(dateof21,'D') depends on your NLS settings. You can't rely on it to always return 5 for a friday in all configurations (e.g. on my computer it returns 6).

Related

I don't understand what is the problem in my store procedure

Create or replace PROCEDURE SSp_EmpHoursInfo
(p_EHrsInfo OUT SYS_REFCURSOR)
AS
BEGIN
OPEN p_EHrsInfo FOR
Select
a.personid, a.first_name, a.last_name,c.hoursworked,d.carecentername, c.break
from person a
join employee b on a.personid = b.empersonid
join employee_assigned_care_center c on b.empersonid = c.empersonid
join care_center d on c.empersonid = d.carecenterid
where hoursworked> 10;
END SSp_EmpHoursInfo;
Everytime I am trying to call the store procedure it is giving me this error msg:
Error starting at line : 225 in command -
BEGIN SSp_EmpHoursInfo (hoursworked> 10); END;
Error report -
ORA-06550: line 1, column 3:
PLS-00201: identifier 'HOURSWORKED' must be declared
ORA-06550: line 1, column 52:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
The problem is very obvious.
BEGIN SSp_EmpHoursInfo (hoursworked> 10); END; is not the proper way of calling it.
The procedure parameter must be the sys_refcursor.
You must use:
DECLARE
OUTPUT_CUR SYS_REFCURSOR;
BEGIN
SSp_EmpHoursInfo (OUTPUT_CUR );
-- USE CURSOR ACCORDINGLY
END;
/

Can I use INSERT ALL with multiple recordtype variables?

Given a PL/SQL block where I have access to 2 recordtype variables, I want to insert these 2 records into the same table in a single statement, but my attempts to use INSERT ALL have failed thus far. Is it possible to use INSERT ALL with record variables at all?
Here is some code that works, using dedicated inserts:
DECLARE
mProperty1 MyTable%ROWTYPE;
mProperty2 MyTable%ROWTYPE;
...
BEGIN
...
INSERT INTO MyTable VALUES mProperty1;
INSERT INTO MyTable VALUES mProperty2;
...
END;
If I try to convert the statement to a INSERT ALL though, it fails with an error message:
DECLARE
mProperty1 MyTable%ROWTYPE;
mProperty2 MyTable%ROWTYPE;
...
BEGIN
...
INSERT ALL
INTO MyTable VALUES mProperty1
INTO MyTable VALUES mProperty2
SELECT 1 FROM DUAL;
...
END;
ORA-06550: line 14, column 60:
PLS-00382: expression is of wrong type
ORA-06550: line 13, column 60:
PLS-00382: expression is of wrong type
ORA-06550: line 13, column 60:
PL/SQL: ORA-00904: : invalid identifier
ORA-06550: line 12, column 7:
PL/SQL: SQL Statement ignored
Am I missing something obvious? Is there a way to make this statement work?
I have tried using different methods but succeeded with the only following:
Method 1:
Using particular record names from %ROWTYPE field
DECLARE
mProperty1 MyTable%ROWTYPE;
mProperty2 MyTable%ROWTYPE;
BEGIN
mProperty1.COL1 := 1;
mProperty2.COL1 := 2;
INSERT ALL
INTO MyTable VALUES (mProperty1.col1)
INTO MyTable VALUES (mProperty2.col1)
SELECT 1 FROM DUAL;
END;
/
-- may be bad choice but you can use like this
Method 2:
If you are concerned about performance then you can also use this method:
DECLARE
TYPE mProperty_TYPE IS TABLE OF MyTable%ROWTYPE;
mProperty mProperty_TYPE;
BEGIN
mProperty := mProperty_TYPE();
mProperty.EXTEND(2);
mProperty(1).COL1 := 3;
mProperty(2).COL1 := 4;
FORALL I IN 1..mProperty.COUNT
INSERT INTO MyTable VALUES mProperty(I);
END;
/
db<>fiddle demo

Apex button and procedure

Im trying to create button in Apex which execute given procedure. As input values I gave two date fields called Poczatek and Koniec. The button should execute this procedure on submit. It perfectly works in Oracle, but throw a lot of errors in Apex.
set serveroutput on
create or replace procedure KORELACJA(:Poczatek, :Koniec)
IS
miasto VARCHAR(25);
korelacja NUMBER;
cursor c1 is
SELECT TEMP.nazwa, corr(TEMP.temperatura, WILGOTNOSC.wilg)
FROM TEMP INNER JOIN WILGOTNOSC
on TEMP.nazwa = WILGOTNOSC.nazwa
and TEMP.data = WILGOTNOSC.data
WHERE TEMP.data between to_date(:Poczatek, 'YYYY-MM-DD') and to_date(:Koniec, 'YYYY-MM-DD')
GROUP BY TEMP.nazwa;
BEGIN
DBMS_OUTPUT.put_line(RPAD('Miasto',10)||RPAD('Korelacja',10));
open c1;
FOR i IN 1..6
LOOP
commit;
fetch c1 into miasto, korelacja;
DBMS_OUTPUT.put_line(RPAD(miasto,10)||RPAD(korelacja,10));
END LOOP;
close c1;
END KORELACJA;
/
Errors look like this:
1 error has occurred
ORA-06550: line 2, column 5: PL/SQL: ORA-00922: missing or invalid option
ORA-06550: line 2, column 1: PL/SQL: SQL Statement ignored ORA-06550: line 6,
column 11: PLS-00103: Encountered the symbol "NUMBER" when expecting one of the
following: := . ( # % ; ORA-06550: line 9, column 18: PLS-00103: Encountered the symbol "JOIN" when
expecting one of the following: , ; for group having intersect minus order start
union where connect
Anyone knows the solution?
I'd suggest you to leave the procedure in the database; call it from Apex.
As you said that it works OK, I'm not going to examine the code. Just modify the first line:
create or replace procedure KORELACJA(par_Poczatek in date,
par_Koniec in date)
is ...
Then, in Apex process, call the procedure as
korelacja(:p1_poczatek, :p2_koniec);
Note that you might need to apply TO_DATE function to those items, using appropriate format mask, such as
korelacja(to_date(:p1_poczatek, 'dd.mm.yyyy',
to_date(:p1_koniec , 'dd.mm.yyyy');
If you insist on keeping the procedure in Apex' process (I wouldn't recommend it), the you don't need CREATE PROCEDURE but an anonymous PL/SQL block. It won't accept any parameters - use Apex items directly.
declare
miasto VARCHAR(25);
korelacja NUMBER;
cursor ...
WHERE TEMP.data between to_date(:p1_Poczatek, 'YYYY-MM-DD') ...
begin
...
end;

Stored Procedure + Call Not working

My Stored Procedure compiles, however; when I try to call it i get the following error:
Encountered the symbol "IS" when expecting one of the following:
constant exception <an identifier>
<a double-quoted delimited-identifier> table long double ref
char time timestamp interval date binary national character
nchar
The symbol "IS" was ignored.
ORA-06550: line 2, column 48:
PLS-00103: Encountered the symbol "," when expecting one of the following:
:= ( ; not null range default character
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
My Stored Procedure is as follows:
create or replace procedure p_xml
(v_utc_offset in XML_HOURS_LOAD.UTCOFFSET%type
, v_data_date in XML_HOURS_LOAD.DATA_DATE%type
, v_data_type in XML_HOURS_LOAD.DATA_TYPE%type
, v_posted_value in XML_HOURS_LOAD.POSTEDVALUE%type
, v_utc_value in XML_HOURS_LOAD.UTCVALUE%type
, v_hour in XML_HOURS_LOAD.HOUR%type
, v_data_code in XML_HOURS_LOAD.DATA_CODE%type
)
AS
BEGIN
if v_utc_offset >= 4 THEN
INSERT INTO xml_hours_Load (UTCOffset, Data_date, Data_Type, PostedValue, UTCValue, Hour, Data_Code)
VALUES(v_utc_offset, v_data_date, v_data_type, v_posted_value, v_utc_value, v_hour, v_data_code);
COMMIT;
END IF;
END;
How I am calling it (and what is returning the error listed above) is as follows:
DECLARE
v_utc_offset is XML_HOURS_LOAD.UTCOFFSET%type,
v_data_date is XML_HOURS_LOAD.DATA_DATE%type,
v_data_type is XML_HOURS_LOAD.DATA_TYPE%type,
v_posted_value is XML_HOURS_LOAD.POSTEDVALUE%type,
v_utc_value is XML_HOURS_LOAD.UTCVALUE%type,
v_hour is XML_HOURS_LOAD.HOUR%type,
v_data_code is XML_HOURS_LOAD.DATA_CODE%type;
CURSOR cXmlHoursLoadCursor is (SELECT utcoffset, data_date, data_type, postedvalue, utcvalue, hour, data_code
from xml_hours_load);
BEGIN
FOR v in cXmlHoursLoadCursor LOOP
p_xml(v.utcoffset, v.data_date, v.data_type, v.postedvalue, v.utcvalue, v.hour, v.data_code);
COMMIT;
END LOOP;
END;
Thanks in Advance!
In your anonymous block, the CURSOR is the only variable you should declare using the IS keyword. Remove it from the others.
DECLARE
v_utc_offset XML_HOURS_LOAD.UTCOFFSET%type;
v_data_date XML_HOURS_LOAD.DATA_DATE%type;
v_data_type XML_HOURS_LOAD.DATA_TYPE%type;
v_posted_value XML_HOURS_LOAD.POSTEDVALUE%type;
v_utc_value XML_HOURS_LOAD.UTCVALUE%type;
v_hour XML_HOURS_LOAD.HOUR%type;
v_data_code XML_HOURS_LOAD.DATA_CODE%type;
CURSOR cXmlHoursLoadCursor is (SELECT utcoffset, data_date, data_type, postedvalue, utcvalue, hour, data_code
from xml_hours_load);
BEGIN
FOR v in cXmlHoursLoadCursor LOOP
p_xml(v.utcoffset, v.data_date, v.data_type, v.postedvalue, v.utcvalue, v.hour, v.data_code);
COMMIT;
END LOOP;
END;

Executing Stored Procedure - Oracle PL SQL

I'm currently trying to execute a stored procedure in Oracle PL SQL. I keep running into the same error for the below with execution.
I've tried both execution with the same error
SET SERVEROUTPUT ON;
EXEC get_phone(200.00,500.00);
OR
SET SERVEROUTPUT ON;
DECLARE
c_minprice products.price%type;
c_maxprice products.price%type;
BEGIN
c_minprice := get_phone(200);
c_maxprice := get_phone(500);
END;
ERROR from executing the above:
c_minprice := get_phone(200);
*
ERROR at line 5:
ORA-06550: line 5, column 15:
PLS-00306: wrong number or types of arguments in call to 'GET_PHONE'
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored
ORA-06550: line 6, column 15:
PLS-00306: wrong number or types of arguments in call to 'GET_PHONE'
ORA-06550: line 6, column 1:
PL/SQL: Statement ignored
****Sample Snip-its form my code:
CREATE OR REPLACE PROCEDURE get_phone
(
c_minprice IN products.price%type,
c_maxprice IN products.price%type,
i_result OUT VARCHAR2
) AS
--Checking if starting price range is valid or not
IF c_minprice IS NULL THEN
i_result := 'Starting price range should be valid and cannot be empty';
RAISE V_MINPRICE; -- Raising exception if starting price is null
END IF;
--Checking if end price range is valid or not
IF c_maxprice IS NULL THEN
i_result := 'End price range should be valid and cannot be empty';
RAISE V_MAXPRICE; -- Raising exception if end price is null
END IF;
Your procedure requires three parameters so you have to pass in three parameters
DECLARE
l_result varchar2(100);
BEGIN
get_phone( 200, 500, l_result );
END;
/
should work. Of course, your procedure seems rather pointless. And if the goal is simply to return a result, you really ought to be using a function rather than a procedure with an out parameter.
get_phone expects 3 arguments, c_minprice, c_maxprice and i_result. You are only passing it one number. Pass it the rest of the arguments.

Resources