I am new in pl/sql. i have write a pl/sql function using sql developer with proper
package name & body.
I function is showing this error:
image given below:
Here is the pl/sql code:
create or replace PACKAGE PAYROLL AS
FUNCTION save_payroll_transaction(transaction_data NVARCHAR2 ) RETURN nclob;
END PAYROLL;
Body:
create or replace PACKAGE BODY PAYROLL IS
FUNCTION save_payroll_transaction(transaction_data NVARCHAR2 ) RETURN nclob IS ret nclob;
xmlData XMLType;
BEGIN
xmlData:=XMLType(transaction_data);
INSERT INTO PAYROLLFILE SELECT x.* FROM XMLTABLE('/transaction'
PASSING xmlData
COLUMNS "salary_year" NUMBER(4,0) PATH "SALYR",
"salary_month" NUMBER(2,0) PATH "SALMT",
"employee_id" NUMBER PATH "EMPID",
"department_code" NUMBER PATH "DPTID",
"salary_head" VARCHAR2(2) PATH "SALHD",
"description" VARCHAR2(50) PATH "DESCR",
"amount" FLOAT(126) PATH "ALAMT",
"operator_id" NUMBER PATH "OPID",
"transaction_date" DATE PATH "TRADT") x;
ret:=to_char(sql%rowcount);
COMMIT;
RETURN '<result><status affectedRow='||ret||'>success</status></result>';
EXCEPTION
WHEN OTHERS THEN
RETURN '<result><status>Error</status></result>';
END save_payroll_transaction;
END PAYROLL;
please help.Thanks
XML path is a string, not an identifier, so you need to enclose it in single quotes:
"salary_year" NUMBER(4,0) PATH 'SALYR' -- not "SALYR"
Related
I am aware that this has been asked so many times, but my problem doesn't seem to go away. I've already put the delimiter in the correct places but i still keep on getting error 'Error(9,1): Encountered the symbol "/" ' at line 9. If I'm not mistaken, the delimiter that causes the error shold be there.
CREATE OR REPLACE PACKAGE FOR_CLASS_NOV2 AS
PROCEDURE PRINT_SNAME(S_NO S.SNO%TYPE);
FUNCTION FIND_MAX_QTY
RETURN NUMBER;
END;
/
CREATE OR REPLACE PACKAGE BODY FOR_CLASS_NOV2 AS
PROCEDURE PRINT_SNAME(S_NO S.SNO%TYPE) IS
S_SNAME S.SNAME%TYPE;
BEGIN
SELECT SNAME
INTO S_SNAME
FROM S
WHERE SNO = S_NO;
DBMS_OUT.PUT_LINE('SUPPLIER NAME IS: ' || S_NAME);
END PRINT_SNAME;
FUNCTION FIND_MAX_QTY()
RETURN NUMBER IS
M_QTY NUMBER;
BEGIN
SELECT AX(STY)
INTO M_QTY
FROM SP;
RETURN M_QTY;
END FIND_MAX_QTY;
END;
/
I think you should not use parenthesis in the function when there's no parameters.
Try replacing this:
FUNCTION FIND_MAX_QTY()
RETURN NUMBER IS
With this:
FUNCTION FIND_MAX_QTY
RETURN NUMBER IS
Also, you have a variable named S_SNAME but you're printing S_NAME.
I get the below error while executing code
create or replace
function contact_restriction_function(obj_schema varchar2, obj_name varchar2)
return varchar2 is
v_contact_info_visible hr_user_access.contact_info_visible%type;
begin
-- Here you can put any business logic for filtering
select nvl(max(contact_info_visible),'N')
into v_contact_info_visible
from hr_user_access
where user_name = user;
-- SQL filter / policy predicate
return ''''||v_contact_info_visible||''' = ''Y'' ';
end;
/
after show erros command i got this
show errors
Errors for FUNCTION CONTACT_RESTRICTION:
LINE/COL ERROR
-------- -----------------------------------------------------------------
3/1 PLS-00103: Encountered the symbol "?" when expecting one of the
following:
begin function pragma procedure subtype type
current cursor delete
exists prior external language
This is the remaining code:
begin
dbms_rls.add_policy(object_schema => 'HR' ,
object_name => 'EMPLOYEES' ,
policy_name => 'Contact_Restriction_Policy' ,
policy_function => 'contact_restriction_function' ,
sec_relevant_cols=>'EMAIL,PHONE_NUMBER'Contact Info ,
sec_relevant_cols_opt=>dbms_rls.all_rows);
end;
below is the actual code which i am executing before show errors:
create or replace function contact_restriction(obj_schema varchar2, obj_name varchar2)
return varchar2
is
v_contact_info_visible IN user_access.contact_info_visible%type;
begin
select nvl(max(contact_info_visible),'N')
into v_contact_info_visible
from user_access where username = user;
return 'v_contact-info_visible ='|| 'Y';
end;
Your original question shows an error message referring to "?", but the code yout posted a as comment would raise a similar error for `"IN"' instead:
2/24 PLS-00103: Encountered the symbol "IN" when expecting one of the following:
That is because you've used IN for a local variable; but IN, OUT and IN OUT are only applicable to stored procedure parameters. You could have declared the function with an explicit IN for example, though it is the default anyway:
create or replace function contact_restriction(obj_schema IN varchar2, ...
So that needs to be removed from the v_contact_info_visible declaration. You've linked to an example you're working from, but you've removed a lot of important quotes from that, which will still cause it to fail when executed as a part of a VPD; because v_contact_info_visible will be out of scope to the caller. And you have a typo, with a hyphen instead of an underscore.
You need something like:
create or replace function contact_restriction(obj_schema varchar2,
obj_name varchar2)
return varchar2 is
v_contact_info_visible user_access.contact_info_visible%type;
begin
select nvl(max(contact_info_visible),'N')
into v_contact_info_visible
from user_access
where username = user;
return ''''||v_contact_info_visible ||''' =''Y''';
end;
/
When called, that will return a string which is either 'N'='Y' or 'Y'='Y'. VPD will include that as a filter in the original query, which will either prevent any rows being returned (in the first case) or have no effect and allow all rows that match any other existing conditions to be returned (in the second case).
The syntax of the function header is incorrect. It should be:
create or replace function contact_restriction(obj_schema IN varchar2, obj_name IN varchar2)
return varchar2
is
I want to return a array in a function as my function looks like below,
CREATE OR REPLACE FUNCTION TEST
RETURN t_array
IS
strings t_array;
BEGIN
--do something
RETURN strings;
END:
But it gives a error t_array must be declare. I want to know where to declare it and how can i declare it?
When you are using a custom type, you have to declare that type first. For example:
CREATE OR REPLACE TYPE string_array IS TABLE OF varchar2(50);
Which creates a new type named string_array that is a table of varchars.
For more information check the official oracle dokumentation here
I want to create a function that returns the number of rows in a table called Rating with a where clause.Where am i going wrong before the declare statement and the end statement?
create or replace
FUNCTION get_movies(user IN NUMBER) RETURN NUMBER
IS
DECLARE cnt NUMBER;
BEGIN
SELECT count(*)
INTO cnt
FROM rating
where userid= user;
RETURN cnt;
END;
I will appreciate help.Thanks.
You should not have the DECLARE keyword. You only need that for an anonymous block (or a sub-block).
create or replace
FUNCTION get_movies(p_userid IN NUMBER) RETURN NUMBER
IS
cnt NUMBER;
BEGIN
...
user is a reserved word so I'd suggest not using that as your parameter name. In the where clause I'm not sure if it will use your parameter value, or the name of the user executing the function; which would error as that string value couldn't be implicitly converted to a number.
I have the following PL/SQL package:
CREATE OR REPLACE PACKAGE PKG_JCSJ
AS
TYPE record_organ_cant IS RECORD(CANT_CODE VARCHAR2(90),
ORGAN_ID VARCHAR2(90) ,
CANT_NAME VARCHAR2(90),
SUPP_TYPE VARCHAR2(20));
--TYPE array_organ_cant IS TABLE of PKG_JCSJ.record_organ_cant;
TYPE array_organ_cant IS TABLE of pub_organ_cant%ROWTYPE;
function fn_transe_organ_cant return PKG_JCSJ.array_organ_cant PIPELINED;
END PKG_JCSJ;
create or replace package body PKG_JCSJ is
function fn_transe_organ_cant return PKG_JCSJ.array_organ_cant PIPELINED
as
cursor cursor_organ_cant is select * from pub_organ_cant ;
record_o_c pub_organ_cant%rowtype;
record_o_c2 pub_organ_cant%rowtype;
cant_code VARCHAR2(90);
TYPE ref_cursor IS REF CURSOR;
array_column_value ref_cursor;
sp_cant_code VARCHAR2(90);
begin
open cursor_organ_cant;
loop
fetch cursor_organ_cant into record_o_c;
exit when cursor_organ_cant%notfound;
cant_code := record_o_c.cant_code;
if instr(cant_code, ',')>0 then
open array_column_value for select * from table(fn_split(cant_code));
loop
fetch array_column_value into sp_cant_code;
exit when array_column_value%notfound;
--DBMS_OUTPUT.put_line('---' || sp_cant_code);
record_o_c2.CANT_CODE := sp_cant_code;
record_o_c2.ORGAN_ID := record_o_c.ORGAN_ID;
record_o_c2.CANT_NAME := record_o_c.CANT_NAME;
record_o_c2.SUPP_TYPE := record_o_c.SUPP_TYPE;
--DBMS_OUTPUT.put_line('++++++' || record_o_c2.CANT_CODE);
PIPE ROW (record_o_c2);
end loop;
close array_column_value;
else
PIPE ROW (record_o_c);
end if;
end loop;
close cursor_organ_cant;
return;
end fn_transe_organ_cant;
begin
null;
end PKG_JCSJ;
Why is this statement failing?
TYPE array_organ_cant IS TABLE of PKG_JCSJ.record_organ_cant;
error info is ORA-06502: PL/SQL: numeric or value error. However, when I use the following statement, success!
TYPE array_organ_cant IS TABLE of pub_organ_cant%ROWTYPE;
record_organ_cant is same structure with TABLE pub_organ_cant, I have no idea why the former fails and the latter is successful, what's the difference?
then, package body as follow,
First of all, in your package body you don't have to use PKG_JCSJ. because its declared within the pacakge and should be accessible to any of its function like
create or replace package body PKG_JCSJ is
function fn_transe_organ_cant return array_organ_cant PIPELINED
....
Next, when you declare again you don't need PKG_JCSJ. like
CREATE OR REPLACE PACKAGE PKG_JCSJ
AS
TYPE record_organ_cant IS RECORD(CANT_CODE VARCHAR2(90),
ORGAN_ID VARCHAR2(90) ,
CANT_NAME VARCHAR2(90),
SUPP_TYPE VARCHAR2(20));
TYPE array_organ_cant IS TABLE of record_organ_cant;
function fn_transe_organ_cant return array_organ_cant PIPELINED;
END PKG_JCSJ;