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.
Related
I am trying to execute the below plsql program, but facing expression of wrong type. Could anyone let me know what might be the error?
CREATE OR REPLACE PROCEDURE CLN_TBL (CTRLM IN VARCHAR2, CTG IN VARCHAR,SBCT IN NUMBER, RTDT IN NUMBER )
AS
V_SQL VARCHAR(2000);
V_TABLE VARCHAR(30);
CURSOR TBL_CUR
IS
SELECT TGT_TABLE_NAME FROM ODS_USER.CLNP WHERE CONTROLM=CTRLM AND APPL_CTGY=CTG AND APPL_SUB_CTGY= SBCT;
L_TGT_TABLE_NAME TBL_CUR%ROWTYPE;
BEGIN
OPEN TBL_CUR;
LOOP
FETCH TBL_CUR INTO L_TGT_TABLE_NAME;
V_TABLE:= L_TGT_TABLE_NAME ;
EXIT WHEN TBL_CUR%NOTFOUND;
V_SQL:='DELETE FROM '||V_TABLE||' WHERE RPT_DT_ID'||'=:1';
EXECUTE IMMEDIATE V_SQL using RTDT;
END LOOP;
COMMIT;
CLOSE TBL_CUR;
END;
As Exhausted said you cant assign row variable to varchar so You should take TGT_TABLE_NAME from row variable, like below should work;
CREATE OR REPLACE PROCEDURE CLN_TBL (CTRLM IN VARCHAR2, CTG IN VARCHAR,SBCT IN NUMBER, RTDT IN NUMBER )
AS
V_SQL VARCHAR(2000);
V_TABLE VARCHAR(30);
CURSOR TBL_CUR
IS
SELECT TGT_TABLE_NAME FROM ODS_USER.CLNP WHERE CONTROLM=CTRLM AND APPL_CTGY=CTG AND APPL_SUB_CTGY= SBCT;
L_TGT_TABLE_NAME TBL_CUR%ROWTYPE;
BEGIN
OPEN TBL_CUR;
LOOP
FETCH TBL_CUR INTO L_TGT_TABLE_NAME;
V_TABLE:= L_TGT_TABLE_NAME.TGT_TABLE_NAME ;
EXIT WHEN TBL_CUR%NOTFOUND;
V_SQL:='DELETE FROM '||V_TABLE||' WHERE RPT_DT_ID'||'=:1';
EXECUTE IMMEDIATE V_SQL using RTDT;
END LOOP;
COMMIT;
CLOSE TBL_CUR;
END;
I am creating a stored procedure in oracle that is selecting records from login table -
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number)
as
begin
select * from login;
end;
Whenever I am going to compile this it shows an error:
PLS-00428: an INTO clause is expected in this SELECT statement
What does this mean? I do not understand this.
You have to store the result of your SELECT statement into a variable, you can use sys_refcursor to display the result.
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number, result_out OUT SYS_REFCURSOR)
as
l_query varchar2(1000) := Null;
begin
l_query := 'select * from login';
open result_out
for l_query;
end;
above code will give you the output
PLS-00428: an INTO clause is expected in this SELECT statement
That means you need an INTO close when you issue bare SELECT from PL/SQL.
:D
More constructively: where do you think the result of your select would go in that code fragment?
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number)
as
begin
select * from login;
end;
You have to retrieve it somehow in order to be processed by your PL/SQL code. Assuming you have several rows to collect, you should use BULK COLLECT INTO:
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number)
as
type my_tbl_type IS TABLE OF login%ROWTYPE;
my_tbl my_tbl_type;
begin
select * BULK COLLECT INTO my_tbl from login;
-- do whatever you
-- need here
-- on `my_tbl`.
end;
As a final note, maybe are you looking for an explicit CURSOR instead? You should definitively take a look at PL/SQL 101: Working with Cursors. This is an interesting discussion both about SELECT ... INTO ... and CURSOR manipulation.
I have several Tables that contains just id and description in that particular schema, i wonder if it is possible to write one generic function which will read something like:
create or replace FUNCTION tab_lookup (key_field char,key_value char,from_table char,return_field char) RETURN char IS
a varchar2(1000);
BEGIN
select &return_field into a
from &from_table
where &key_field=key_value;
return(a);
exception
when others
then
return('*ERR*');
END;
i want to use it at inside package application which only 50 users will be using.
Modified your version, by changing it using Dynamic SQL and changed the Input parameters' Datatype as VARCHAR2
CREATE OR REPLACE FUNCTION tab_lookup (key_field VARCHAR2,
key_value VARCHAR2,
from_table VARCHAR2,
return_field VARCHAR2,
return_type VARCHAR2)
RETURN VARCHAR2 IS
result_a varchar2(1000);
query_string VARCHAR2(4000);
/*version 0.1*/
BEGIN
query_string := 'SELECT '||return_field||
'FROM '||from_table||
'WHERE '||key_field || ' = :key_value ';
IF(return_type = 'SQL') THEN
result_a := query_string;
ELSE
//this line will not work in forms 6i remove the using key_value word
EXECUTE IMMEDIATE query_string USING key_value into result_a;
END IF;
RETURN (result_a);
EXCEPTION
// add DBMS_ASSERT Exceptions
WHEN
NO_DATA_FOUND THEN
RETURN(NULL);
WHEN
TOO_MANY_ROWS THEN
RETURN('**ERR_DUPLICATE**');
WHEN OTHERS
THEN
RETURN('*ERR_'||SQLERRM);
END;
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;
Hey I am trying to write a procedure in which the user can insert which columns he would like to get as parameter input. As of right now when I run a test script I get this error:
error -1 message error in ct_cu_act_medrecon_pg.spm_search_patientmedrecs =>ORA-00933: SQL command not properly ended
The error is refering to the order by part in the select statement, and when I remove that I get an error saying:
error -1 message error in ct_cu_act_medrecon_pg.spm_search_patientmedrecs =>ORA-00904: "D"."P_INSERTDT_IN": invalid identifier
Here is the spec:
procedure spm_search_patientmedrecs (
p_columnsort_in in varchar2, --which is sort column
p_medmed_in in varchar2, --first column
p_planid_in in varchar2, --second column
p_detmed_in in varchar2, --third column
p_insertdt_in in varchar2, --fourth column
p_ascdesc_in in varchar2, --asc or desc in order by
p_return_cur_out out sys_refcursor,
p_err_code_out out number,
p_err_mesg_out out varchar2
);
Here is the procedure body:
procedure spm_search_patientmedrecs (
p_columnsort_in in varchar2,
p_medmed_in in varchar2,
p_planid_in in varchar2,
p_detmed_in in varchar2,
p_insertdt_in in varchar2,
p_ascdesc_in in varchar2,
p_return_cur_out out sys_refcursor,
p_err_code_out out number,
p_err_mesg_out out varchar2)
is
lv_sql varchar2(32767);
begin
lv_sql := '';
lv_sql := 'select h.p_medmed_in,
h.p_planid_in,
d.p_detmed_in,
d.p_insertdt_in
from membermedicalreconcilationhdr h,
membermedicalreconcilationdet d
where h.membermedreconciliationhdrskey =
d.membermedreconciliationhdrskey
order by h.p_columnsort_in p_ascdesc_in';
p_err_code_out := 0;
OPEN p_return_cur_out FOR lv_sql;
exception
when others then
p_err_code_out := -1;
p_err_mesg_out := 'error in ct_cu_act_medrecon_pg.spm_search_patientmedrecs =>'||sqlerrm;
end spm_search_patientmedrecs;
Here is my test script:
set serveroutput on
declare
type tempcursor is ref cursor;
v_cur_result tempcursor;
errcode number;
errmesg varchar2(1000);
begin
ct_cu_act_medrecon_pg.spm_search_patientmedrecs
('primarymemberplanid',
'membermedreconciliationhdrskey',
'primarymemberplanid',
'membermedreconciliationdetskey',
'inserteddt',
'ASC',
v_cur_result,
errcode,
errmesg
);
-- dbms_output.put_line(v_cur_result);
dbms_output.put_line('error '||errcode||' message '||errmesg);
end;
First off, I know how I'm handeling the error isnt the best way to do it but thats how the person asking me to do this wanted it.
Now I dont know if this is a possible thing to do in Oracle PL/SQL, but if it is I would greatly appreciate some help in pointing me in the right direction. If you guys need any more information feel free to ask and I will assist as best I can (Ive only been working with SQL and PL/SQL for 2 months). Thanks in advance.
Dynamic SQL means assembling strings which are executed as SQL statements. Your string hardcodes the parameter names, whereas what you actually need is the contents of the parameters.
Something like this:
lv_sql := 'select h.'||p_medmed_in||',
h.'||p_planid_in||',
d.'||p_detmed_in||',
d.'||p_insertdt_in||'
from membermedicalreconcilationhdr h,
membermedicalreconcilationdet d
where h.membermedreconciliationhdrskey =
d.membermedreconciliationhdrskey
order by h.'||p_columnsort_in||' '|| p_ascdesc_in;