the below oracel procedure keeps showing me "pls-00103 encountered the symbol create" and i am not ablt to figure out why.. please help
create or replace procedure myproc
(
otherdate in varchar2
)
as
mystringdate varchar2(20);
begin
create or replace function checkdate(givdate in varchar2) return number
as
givedate1 date;
begin
givedate1 := todate(givdate);
return1;
exception
when others then
return 0;
end;
mystringdate := ltrim(rtrim(otherdate));
if checkdate(mystringdate,'dd-mm-yyyy')= 1 then
DBMS_OUTPUT.PUT_LINE('it is a date format');
else
DBMS_OUTPUT.PUT_LINE('it is not a date format');
endif
end myproc;
i tried more like \ symbol and all but not working. please help
This is weird, you started creating a proc and then inside begin you started creating function, you need to remove create function from there. Why you want to do that?
Remove this:
create or replace function checkdate(givdate in varchar2) return number
as
givedate1 date;
begin
givedate1 := todate(givdate);
return1;
exception
when others then
return 0;
end;
Related
This function is inside a package, but when I call the function the following error appears: PL/SQL "all_search" is not a procedure or is undefined. Someone can help me?
CREATE OR REPLACE PACKAGE employee_tab IS
FUNCTION all_search (ID_EMP in NUMBER) RETURN O_T_EMPL PIPELINED;
END employee_tab;
/
CREATE OR REPLACE TYPE O_T_EMPL AS TABLE OF O_EMPLOYEE;
/
CREATE OR REPLACE PACKAGE BODY employee_tab IS
FUNCTION all_search (ID_EMP in NUMBER) RETURN O_T_EMPL PIPELINED
IS
TAB_OBJC_EMP O_T_EMPL;
MY_QUERY_SEARCH VARCHAR2(400);
REF_C SYS_REFCURSOR;
MAX_ROW NUMBER := 25;
BEGIN
MY_QUERY_SEARCH := 'SELECT *
FROM EMPLOYEES
WHERE EMPLOYEE_ID = ID_EMP';
open REF_C for MY_QUERY_SEARCH using ID_EMP;
loop
--
fetch REF_C bulk collect into TAB_OBJC_EMP limit MAX_ROW;
exit when TAB_OBJC_EMP.count = 0;
for i in 1..TAB_OBJC_SEE.count
--
loop
pipe row(O_EMPLOYEE(TAB_OBJC_EMP(i).V_O_EMP_ID,
TAB_OBJC_EMP(i).V_O_HIRE_ID,
TAB_OBJC_EMP(i).V_O_DEP_ID)
);
end loop;
--
END loop;
--
CLOSE REF_C;
RETURN;
--
END all_search;
END employee_tab;
/
call function: employee_tab.all_search(1);
You need to assign the result of the function to something, try something like:
DECLARE
l_emp_id NUMBER;
BEGIN
SELECT EMP_ID
INTO l_emp_id
FROM TABLE(employee_tab.all_search(1))
WHERE rownum = 1;
DBMS_OUTPUT.put_line(TO_CHAR(EMP_ID));
END;
/
i am trying to call a function from stored procedure in Oracle, but not getting any idea how to do.
my function has two IN parameter and one OUT parameter.
in my procedure i am using out sys refcursor . Any refrence or example will help me a lot.
Here is a simple example for calling function inside procedure. Also as mentioned by APC using OUT in function is a bad practice. Instead you can return your required output. And I'm not sure how you are using sys_refcursor, so modify your procedure accordingly
CREATE OR REPLACE FUNCTION SUM_OF_2(NUM1 IN NUMBER,NUM2 IN NUMBER) RETURN NUMBER
IS
RESULT_SUM NUMBER;
BEGIN
RESULT_SUM:=NUM1+NUM2;
RETURN RESULT_SUM;
END;
CREATE OR REPLACE PROCEDURE CALL_FUNCTON(NUM1 NUMBER,NUM2 NUMBER)
AS
V_FINAL_RESULT NUMBER;
BEGIN
V_FINAL_RESULT:=SUM_OF_2(NUM1,NUM2);
DBMS_OUTPUT.PUT_LINE(V_FINAL_RESULT);
END;
BEGIN
CALL_FUNCTON(5,10);
END;
/
CHECK DEMO HERE
Not sure on what your requirement is , maybe you are just trying the code for education purposes. Generally I have not seen much code which uses OUT parameter with functions, in case you want to return multiple values to the caller object then you could use a procedure with more then one OUT variables. There are some limitation on how an oracle function with OUT parameter would differ from a normal function.
CREATE OR REPLACE FUNCTION temp_demo_func(out_var1 OUT NUMBER)
RETURN VARCHAR2 IS
BEGIN
out_var1 := 1;
RETURN 'T';
EXCEPTION
WHEN OTHERS THEN
RETURN 'F';
END temp_demo_func;
/
CREATE OR REPLACE PROCEDURE temp_demo_proc
(
in_var1 NUMBER
,cur_refcur_out OUT SYS_REFCURSOR
) IS
res VARCHAR2(1);
out_var1 NUMBER;
BEGIN
res := temp_demo_func(out_var1 => out_var1);
dbms_output.put_line(out_var1);
OPEN cur_refcur_out FOR
SELECT in_var1
,out_var1
,res
FROM dual;
END;
/
set serveroutput on
declare
cur_refcur_out Sys_Refcursor;
in_var1 number := 22;
begin
temp_demo_proc(in_var1 => in_var1
,cur_refcur_out => cur_refcur_out);
end;
/
I want to handle exception using oracle as I haven't done it before. below is my stored procedure.
create or replace
PROCEDURE GET_VALID_LATLONG
(
P_XYCORDINATE IN VARCHAR2,
P_SAPID IN VARCHAR2,
OUTR4GSTATENAME OUT SYS_REFCURSOR
)
AS
v_counter number:=0;
BEGIN
DBMS_OUTPUT.ENABLE;
OPEN OUTR4GSTATENAME FOR
SELECT DISTINCT(R4GSTATECODE),R4GSTATENAME
FROM R4G_LB.R4GSTATEBOUNDARY_EVW
WHERE SDE.ST_INTERSECTS(SDE.ST_GEOMETRY('POINT
('||P_XYCORDINATE||')', 3),SHAPE) = 1;
END GET_VALID_LATLONG;
how to handle the exception?
UPDATE
I added like this, is it fine when error occurs ??
create or replace
PROCEDURE GET_VALID_LATLONG
(
P_XYCORDINATE IN VARCHAR2,
P_SAPID IN VARCHAR2,
OUTR4GSTATENAME OUT SYS_REFCURSOR
)
AS
v_counter number:=0;
BEGIN
DBMS_OUTPUT.ENABLE;
OPEN OUTR4GSTATENAME FOR
SELECT DISTINCT(R4GSTATECODE),R4GSTATENAME
FROM R4G_LB.R4GSTATEBOUNDARY_EVW
WHERE SDE.ST_INTERSECTS(SDE.ST_GEOMETRY('POINT
('||P_XYCORDINATE||')', 3),SHAPE) = 1;
EXCEPTION
WHEN OTHERS THEN
NULL;
END GET_VALID_LATLONG;
A list of pre-defined exceptions can be found in the Oracle docs, for example here.
https://docs.oracle.com/cd/A97630_01/appdev.920/a96624/07_errs.htm
You can use this list of exception names in your exception clause, for example
WHEN TOO_MANY_ROWS THEN
NULL; -- whatever you wish to do here.
I have the following code
CREATE OR REPLACE FUNCTION slow_function (p_in IN NUMBER)
RETURN NUMBER
AS
BEGIN
DBMS_LOCK.sleep(1);
RETURN p_in;
END;
/
CREATE OR REPLACE PACKAGE cached_lookup_api AS
FUNCTION get_cached_value (p_id IN NUMBER)
RETURN NUMBER;
PROCEDURE clear_cache;
END cached_lookup_api;
/
CREATE OR REPLACE PACKAGE BODY cached_lookup_api AS
TYPE t_tab IS TABLE OF NUMBER
INDEX BY BINARY_INTEGER;
g_tab t_tab;
g_last_use DATE := SYSDATE;
g_max_cache_age NUMBER := 10/(24*60); -- 10 minutes
-- -----------------------------------------------------------------
FUNCTION get_cached_value (p_id IN NUMBER)
RETURN NUMBER AS
l_value NUMBER;
BEGIN
IF (SYSDATE - g_last_use) > g_max_cache_age THEN
-- Older than 10 minutes. Delete cache.
g_last_use := SYSDATE;
clear_cache;
END IF;
BEGIN
l_value := g_tab(p_id);
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- Call function and cache data.
l_value := slow_function(p_id);
g_tab(p_id) := l_value;
END;
RETURN l_value;
END get_cached_value;
-- -----------------------------------------------------------------
-- -----------------------------------------------------------------
PROCEDURE clear_cache AS
BEGIN
g_tab.delete;
END;
-- -----------------------------------------------------------------
END cached_lookup_api;
/
I want to pass two parameters "pi_value1" and "pi_value2" both of varchar2 to the function slow_function instead of "p_in". Is is possible to cache the results with two in parameters in oracle 10g .
the above code works fine with 1 parameter.
Please any one explain?
You'd need to create a two-dimensional array type to cache the values. Something along the lines of this (omitting the cache expiration code since that isn't changing)
CREATE OR REPLACE PACKAGE BODY cached_lookup_api
AS
TYPE t_pi_value2_tbl IS TABLE OF NUMBER
INDEX BY VARCHAR2(100);
TYPE t_cache IS TABLE OF t_pi_value2_tbl
INDEX BY VARCHAR2(100);
g_cache t_cache;
FUNCTION get_cached_value( p_pi_value1 IN VARCHAR2,
p_pi_value2 IN VARCHAR2 )
IS
BEGIN
RETURN g_cache(p_pi_value1)(p_pi_value2);
EXCEPTION
WHEN no_data_found
THEN
g_cache(p_pi_value1)(p_pi_value2) := slow_function( p_pi_value1, p_pi_value2 );
RETURN g_cache(p_pi_value1)(p_pi_value2);
END;
END;
Please help in finding the error while creating the package with overloading functions.
Error shown:
PL-00103: Encountered the symbol "NUMOU"when expecting one of the following: language.
PL-00103: Encountered the symbol "Function" when expecting one of the following: end not pragma final instantiable order overriding static member constructor map.
code:
create or replace package over_load as
function print_it(numin varchar2) return number is numou number;
begin
numou := to_number(numin, '999,999.00');
dbms_output.put_line(numou);
return numou;
exception
when others then
dbms_output.put_line('Wrong string format');
return numou;
end;
function print_it(datin date) return varchar2 is datout varchar2(30);
dumcha varchar2(30);
dumdat date;
begin
dumcha := to_char(datin);
dumdat := to_date(dumcha,'FXDD-MON-YYYY');
datout := to_char(datin, 'fmMOn, DD YYYY');
dbms_output.put_line(datout);
return datout;
Exception
when others then
dbms_output.put_line('Wrong input date format');
return '0';
end;
end;
In Oracle RDBMS You MUST specify package (for public methods) and package body containing implementation for all public methods and optionally You can add some private methods.
Package specification:
create or replace package over_load as
function print_it(numin varchar2) return number;
function print_it(datin date) return varchar2;
end;
And body:
create or replace package body over_load as
function print_it(numin varchar2) return number is
numou number;
begin
numou := to_number(numin, '999,999.00');
dbms_output.put_line(numou);
return numou;
exception
when others then
dbms_output.put_line('Wrong string format');
return numou;
end;
function print_it(datin date) return varchar2 is
datout varchar2(30);
dumcha varchar2(30);
dumdat date;
begin
dumcha := to_char(datin);
dumdat := to_date(dumcha,'FXDD-MON-YYYY');
datout := to_char(datin, 'fmMOn, DD YYYY');
dbms_output.put_line(datout);
return datout;
exception
when others then
dbms_output.put_line('Wrong input date format');
return '0';
end;
end;
NOTE THAT:
To enable output from DBMS_OUTPUT in SQL*Plus You must enable serveroutput:
SQL> set serveroutput on size 30000;