oracle stored procedure using date parameter - oracle

Is it possible to use a date parameter for a stored procedure?
for example, date 20171201 I need to execute a case A in a dateparameter.prc
and date 20171202 execute a case B in a dateparameter.prc which is the same procedure above.
I am googling and investigating some books but I still haven't found a solution.
Can anyone know about it?
Thanks

Yes, it is possible.
SQL> set serveroutput on
SQL> create procedure dt_demo(p_d date) as
2 begin
3 dbms_output.put_line('p_d = ' || p_d);
4 end;
5 /
Procedure created
SQL> exec dt_demo(date '2017-12-02');
p_d = 02.12.17
PL/SQL procedure successfully completed

Is it possible to use a date parameter for a stored procedure?
Yes. A simple example that takes a date as an IN date parameter and passes it directly to an OUT date parameter is:
CREATE PROCEDURE your_procedure(
in_value IN DATE,
out_return OUT DATE
)
IS
BEGIN
out_return := in_value;
END;
/

Your procedure is functioning correctly. But
The expression "DATE '2017-12-02'" represents the ISO date standard.
The expression "dbms_output.put_line('p_d = ' || p_d)" represents the regular Oracle date processing, which precedes the ISO specification.
How the date is formatted (displayed) during dbms_ouput converts the date to a string. Since in this case there is an implicit conversion the resulting format is controlled by the NLS_DATA_FORMAT setting. It looks like yours is set to "dd-mm-yy". To see the difference insert/run the following before your exec statement:
alter session set nls_date_format = 'yyyy-mm-dd" ;
Also see Oracle Date Format for Oracle 11g or as appropriate for your version.

Related

wrong number or types of arguments in call to 'VENTAS_MAYOR'

I made a procedure called 'VENTAS_MAYOR' with a parameter 'FECHITA' type DATE
CREATE OR REPLACE PROCEDURE VENTAS_MAYOR (FECHITA IN DATE)
IS
V_FECHA DATE;
V_CANTIDAD NUMBER;
V_DESCRIPCION VARCHAR2(50);
BEGIN
SELECT
A.FECHAEMISION_BOL,
B.CANTIDAD,
C.DESCRIPCION
INTO
V_FECHA,
V_CANTIDAD,
V_DESCRIPCION
FROM BOLETA A JOIN DETALLE B ON (A.COD_BOLETA = B.COD_DETALLE)
JOIN PRODUCTO C ON (B.COD_DETALLE = C.CODPRODUCTO)
WHERE A.FECHAEMISION_BOL = FECHITA
ORDER BY CANTIDAD DESC;
DBMS_OUTPUT.PUT_LINE(V_CANTIDAD || V_DESCRIPCION);
END VENTAS_MAYOR;
But when i am going to execute the function/procedure with a parameter i get that error...
SET SERVEROUTPUT ON;
EXECUTE VENTAS_MAYOR(05/2019);
I don't know why i'm getting this error , i'm using only a parameter.... !
You are passing the string as a parameter but oracle is expecting a date.
You need to pass a date as follows:
VENTAS_MAYOR(date'2019-05-01');
Since you need to pass input in date format. Correct way of calling this procedure is,
SET SERVEROUTPUT ON;
EXECUTE VENTAS_MAYOR(to_date('05/01/2019','mm/dd/yyyy');
Also, make sure datatype of FECHAEMISION_BOL column in BOLETA table. It should be DATE or TIMESTAMP. If not, you need to modify the WHERE condition as follows -
to_date(A.FECHAEMISION_BOL,'mm/dd/yyyy') = FECHITA

ORA-01858 while executing created procedure

I have created a procedure to find a date between today and expiry_date.
The table column format is already in date format.
While creating procedure it is created successfully without errors but during execution of procedure as below it is showing
ORA-01858: a non-numeric character was found where a numeric was expected
ALTER SESSION SET NLS_DATE_FORMAT ='dd-mm-yyyy';
CREATE OR REPLACE PROCEDURE flow (
today IN DATE,
expiry_date IN DATE
) AS
BEGIN
FOR rec in (
SELECT *
FROM flow4
WHERE englishcalendar BETWEEN 'englishcalendar.today'
AND 'englishcalendar.expiry_date')
LOOP
dbms_output.put_line(rec.englishcalendar);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(sqlerrm);
END;
/
EXEC FLOW('01-02-2017','03-04-2018');
/
I had also tried adding to_date in both procedure and procedure execution but i got same error
I also tried with this reference too
Getting Error - ORA-01858: a non-numeric character was found where a numeric was expected
NOTE englishcalendar contains continuous day of 2019 in 'dd-mm-yyyy' date format
'englishcalendar.today' is a string not a date. Likewise 'englishcalendar.expiry_date'. Oracle is attempting to convert these strings to dates and failing, because they are not dates.
Simple solution: reference the parameters as identifiers not strings:
...
FOR rec in (
SELECT *
FROM flow4
WHERE englishcalendar BETWEEN today
AND expiry_date)
...

Converting an Oracle Stored Procedure DATE Input Parameter

I am very new to Oracle and have a question about input parameters to a stored procedure. Basically its a stored procedure being called from an external system passing in a date formatted as MM/DD/YYYY.
Oracle doesn't seem to like the MM/DD/YYYY format as it gives me a "not a valid month" error. (I think it wants like a DD-MMM-YYYY?) whatever the default is.
is there a way to convert the date as it comes into the procedure without getting an error?
such as:
create procedure test_proc
(
v_input_date IN DATE := to_char(v_input_date, 'MM/DD/YYYY')
)
I know the above code likely makes no actual sense but hopefully it will convey what I'd like to do. The user would call the procedure something like
BEGIN
test_proc('01/01/2018')
END
You may try with ANSI type date 'yyyy-mm-dd' formatting like in the following sample :
SQL>create or replace procedure test_proc( v_input_date date ) is
v_diff int;
begin
v_diff := trunc(sysdate)-v_input_date;
dbms_output.put_line(v_diff||' days difference...');
end;
/
SQL> set serveroutput on;
SQL> begin
test_proc(date '2018-03-21');
end;
/
2 days difference...
Your problem is not in the procedure, it is in the code calling the procedure.
'01/01/2018' is not a date it is a string but your procedure expects a date; however, Oracle tries to be helpful and will implicitly try to convert the string to a date using the TO_DATE( string_value, format_model ) function. Since it does not have a specified format model, it will use the default format for a date which is the NLS_DATE_FORMAT session parameter and if this format mask does not match the format of the string then you will get an error.
(Note: session parameters are per-user-session and can be changed by each user so you should not rely on them being the same for each user or even the same between sessions for the same user!)
You can see the format of the NLS_DATE_FORMAT session parameter using the query:
SELECT VALUE
FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = 'NLS_DATE_FORMAT';
And your code to call the procedure is implicitly being converted to something like:
BEGIN
test_proc(
TO_DATE(
'01/01/2018',
( SELECT VALUE FROM NLS_SESSION_PARAMETERS WHERE PARAMETER = 'NLS_DATE_FORMAT' )
)
);
END;
To generate a date you should explicitly convert the string to a date either by:
Using an ANSI literal
BEGIN
test_proc( DATE '2018-01-01' );
END;
Or by specifying the format mask used in the conversion
BEGIN
test_proc( TO_DATE( '01/01/2018', 'MM/DD/YYYY' ) );
END;

Define variable in Oracle SQL developer

I'm trying to define standard values in variables in ORACLE SQL Developer, but it keeps asking me to enter a value. How can I avoid that and put as default value for v_mode ='X1','X2' and set COB_DATE to 14 July so that there is NO popup?
variable COB_DATE date
variable v_mode varchar(20);
exec :COB_DATE := '14-JUL-2016';
exec :v_mode := 'MAG';
select *
FROM DF_RISK_SIT2_OWNER.recon_ts_rs
WHERE SRC_HUB = 'DBRS'
AND TRD_SRC_SYS in :v_mode
AND DSET_COB_DT = :COB_DATE
but I get the error: Bind Variable "COB_DATE" is NOT DECLARED
SQL> help var
VARIABLE
--------
Declares a bind variable that can be referenced in PL/SQL, or
lists the current display characteristics for a single variable
or all variables.
VAR[IABLE] [variable [type]]
where type represents one of the following:
NUMBER CHAR CHAR (n [CHAR|BYTE])
NCHAR NCHAR (n) VARCHAR2 (n [CHAR|BYTE])
NVARCHAR2 (n) CLOB NCLOB
REFCURSOR BINARY_FLOAT BINARY_DOUBLE
As you can see there is no DATE type here. I guess the whole
variable COB_DATE date
is ignored.
As a workaround you can define COB_DATE as varchar2 and convert it to DATE in the sql
variable COB_DATE varchar2(30)
variable v_mode varchar2(20)
exec :COB_DATE := '14-JUL-2016';
exec :v_mode := 'MAG';
select *
FROM DF_RISK_SIT2_OWNER.recon_ts_rs
WHERE SRC_HUB = 'DBRS'
AND TRD_SRC_SYS in :v_mode
AND DSET_COB_DT = TO_DATE(:COB_DATE, 'DD-MON-YYYY')
or rely on implicit conversion using your original query
You have to use "Run Script (F5)" not "Run Statement (Control+Enter") - I have circled the toolbar icon in red:
for Oracle SQL Developer:
define defVar= 'AA%'
Select...
where somefield like '&&defVar';

PL/SQL insert date with procedure

I want to insert date in my dates table by passing date as ('19-JUN-1997') parameter to procedure. Could anyone give me an example how to do it? It seems that I am doing something wrong with trying to insert date by putting variable in TO_DATE(my_var).
SET SERVEROUTPUT ON
BEGIN
p_date('14-MAR-2017');
END;
CREATE OR REPLACE PROCEDURE p_date(
v_date IN Dates.date1%type) IS
BEGIN
INSERT INTO Dates
(date1)
VALUES
(TO_DATE(v_date ));
END;
If you know the format of the date (e.g., dd-mon-yyyy) then the safest thing to do is make your insert statement like thus:
INSERT INTO Dates (date1)
VALUES (TO_DATE(v_date, 'DD-MON-YYYY');
In order for your example to work, the character string date format must be in the default date format in the database. You can get the value of the database default format using this:
SELECT value
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT'
However, if the default format is changed and you are relying on it, your code will break. It also tells the reader of the code what the date format you're expecting is.

Resources