I'm trying create package with oracle, although I have read many examples in docs oracle and built code same this tutorial but I still error this.
The following code:
create table manage_emplyee
(
f_name varchar(20),
l_name varchar(20)
);
// Specification
create or replace package fn2
as
procedure manage_emplyee(v_fname in VARCHAR2, v_lname in VARCHAR2);
procedure manage_emplyee_delete(v_fname in VARCHAR2);
end;
/
create or replace package body fn2
as
--Procedure Implementation
procedure manage_emplyee(v_fname in VARCHAR2, v_lname in VARCHAR2)
is
begin
insert into manage_emplyee VALUES (v_lname, v_lname);
end manage_emplyee;
// body
procedure manage_emplyee_delete (v_fname in VARCHAR2)
is
begin
delete manage_emplyee where v_fname = v_fname;
end manage_emplyee_delete;
end fn2;
/
Error
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
begin end function pragma procedure
Please help me fix it, thanks so much !
There are many things incorrect, so here is a correct version.
Run the table script first -
create table manage_emplyee
(
f_name varchar(20),
l_name varchar(20)
);
Run the Spec script after that
create or replace package fn2
as
procedure manage_emplyee(v_fname VARCHAR2,
v_lname VARCHAR2);
procedure manage_emplyee_delete(v_fname VARCHAR2);
end;
/
Run the body script after that
create or replace package body fn2
as
procedure manage_emplyee(v_fname VARCHAR2, v_lname VARCHAR2)
is
begin
insert into manage_emplyee VALUES (v_lname, v_lname);
end ;
procedure manage_emplyee_delete (v_fname VARCHAR2)
is
begin
delete from manage_emplyee where f_name = v_fname;
end ;
end;
/
The syntax for delete from table_name was incorrect.
I am sure, you want to match f_name from table with v_fname from the input variable to delete the data. Earlier you were matching v_fname with v_fname in your code, which will always be true (except for when its passed NULL) and you would end up loosing all your test data
*NOTE -
Adding IN explicitly is not required, the default type is IN for parameters in PLSQL procedures
Related
CREATE OR REPLACE PACKAGE APPS.XX_PROC_ADI_test AS
PROCEDURE XX_INS_DATA_ADI (
p_primary_uom_flag VARCHAR2,
p_list_line_type_code VARCHAR2,
p_price_break_type_code VARCHAR2,
p_arithmetic_operator VARCHAR2,
p_operand NUMBER,
p_start_date_active DATE,
p_end_date_active DATE,
p_product_precedence NUMBER
);
END;
SET VERIFY OFF
WHENEVER SQLERROR EXIT FAILURE ROLLBACK;
CREATE OR REPLACE PACKAGE APPS.XX_PROC_ADI_test AS
PROCEDURE XX_INS_DATA_ADI (
p_primary_uom_flag VARCHAR2,
p_list_line_type_code VARCHAR2,
p_price_break_type_code VARCHAR2,
p_arithmetic_operator VARCHAR2,
p_operand NUMBER,
p_start_date_active DATE,
p_end_date_active DATE,
p_product_precedence NUMBER
)IS
lc_sqlerrm VARCHAR2(2000);
lc_error_msg VARCHAR2 (2000);
l_responsibility_id NUMBER := apps.fnd_global.resp_id;
l_resp_application_id NUMBER := apps.fnd_global.resp_appl_id;
l_org_id NUMBER := apps.fnd_global.org_id;
l_user_id NUMBER := apps.fnd_global.user_id;
BEGIN
INSERT INTO QP_LIST_LINES(
primary_uom_flag,
list_line_type_code,
price_break_type_code,
arithmetic_operator,
operand,
start_date_active,
end_date_active,
product_precedence
)
VALUES (
p_primary_uom_flag,
p_list_line_type_code,
p_price_break_type_code,
p_arithmetic_operator,
p_operand,
p_start_date_active,
p_end_date_active,
p_product_precedence
);
COMMIT;
EXCEPTION WHEN OTHERS THEN
lc_sqlerrm := SUBSTR(SQLERRM,1,1999);
raise_application_error (-20001, 'OTHER_EXCEPTION - MSG | ' || lc_sqlerrm);
END;
END XX_INS_DATA_ADI;
END;
If you're going to use this (and your tool supports it), use it at the beginning:
SET VERIFY OFF
WHENEVER SQLERROR EXIT FAILURE ROLLBACK;
Then create
package specification first
package body next
create or replace package xx_proc_adi_test as
procedure xx_ins_data_adi(...);
end xx_proc_adi_test;
/
create or replace package BODY xx_proc_adi_Test as
procedure xx_ins_data_adi(...) is
...
begin
...
exception
...
end xx_ins_data_adi;
end xx_proc_adi_test;
/
After executing it shows:
Compilation errors for PACKAGE BODY TEMP_PACKAGE
Error: PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
begin end function pragma procedure
Line: 17
create or replace package body TEMP_PACKAGE is
procedure insert_temp
(aaCode number,aaName varchar2, aaAddress varchar2,
aaPhone varchar2, aaState varchar2 )
is
begin
INSERT INTO temp_employee_azizbek
(code, aadName, aadAddress, aadPhone, aadState )
VALUES (aaCode, aaName,aaAddress,aaPhone,aaState);
end;
What is my mistake?
an extra end is missing, and you need package definition (header) besides package body :
create or replace package TEMP_PACKAGE is
procedure insert_temp(aaCode number,aaName varchar2, aaAddress varchar2,
aaPhone varchar2, aaState varchar2 );
end TEMP_PACKAGE;
/
create or replace package body TEMP_PACKAGE is
procedure insert_temp(aaCode number,aaName varchar2, aaAddress varchar2,
aaPhone varchar2, aaState varchar2 ) is
begin
INSERT INTO temp_employee_azizbek
(code, aadName, aadAddress, aadPhone, aadState )
VALUES (aaCode, aaName,aaAddress,aaPhone,aaState);
end;
end TEMP_PACKAGE;
/
I am having difficulty with writing my stored procedure within a package. Below is my stored procedure that compiles fine outside of the package, but I believe needs to be written differently within the package body. I admit this is my first time using a package...
create or replace PROCEDURE SP_COMMENT(P_MEMBER_ID IN VARCHAR2, P_MEMBER_LASTNAME IN
VARCHAR2, P_MEMBER_FIRSTNAME IN VARCHAR2, P_MEMBER_STARTDATE IN DATE,
P_MEMBER_ENDDATE IN DATE, P_PRODUCT_CAT_CODE IN VARCHAR2, P_COMMENT IN VARCHAR2,
COMMENT_CURSOR out sys_refcursor)
AUTHID CURRENT_USER
IS
BEGIN
EXECUTE IMMEDIATE
'INSERT INTO TEST
(
MEMBER_ID,
MEMBER_LASTNAME,
MEMBER_FIRSTNAME,
MEMBER_STARTDATE,
MEMBER_ENDDATE,
PRODUCT_CAT_CODE,
COMMENTS
)
VALUES
(
p_member_id,
p_member_lastname,
p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment)';
commit;
open COMMENT_CURSOR for select * from sconti.TEST;
END;
Below is the package that I started, and which is not working:
CREATE OR REPLACE
PACKAGE COMMENT_TEST IS
PROCEDURE SP_COMMENT(P_MEMBER_ID IN VARCHAR2, P_MEMBER_LASTNAME IN VARCHAR2,
P_MEMBER_FIRSTNAME IN VARCHAR2, P_MEMBER_STARTDATE IN DATE,
P_MEMBER_ENDDATE IN DATE, P_PRODUCT_CAT_CODE IN VARCHAR2, P_COMMENT IN VARCHAR2,
COMMENT_CURSOR out sys_refcursor) IS
BEGIN
EXECUTE IMMEDIATE
'INSERT INTO TEST
(
MEMBER_ID,
MEMBER_LASTNAME,
MEMBER_FIRSTNAME,
MEMBER_STARTDATE,
MEMBER_ENDDATE,
PRODUCT_CAT_CODE,
COMMENTS
)
VALUES
(
p_member_id,
p_member_lastname,
p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment)';
commit;
open COMMENT_CURSOR for select * from sconti.TEST;
END;
END COMMENT_TEST;
I look forward to any response to assist me....thanks!
I can't say for certain (because you haven't shared the error you're getting), but most basic error is a lack of understanding of the specification/body.
You've put the code into the package specification, rather than the body. The specification should just have the procedure declarations (i.e. no begin and end), where as the body has the full content of the procedure.
While it won't affect compilation, there is another problem: the SQL inside the string can't access the parameters supplied to the procedure. If you must use dynamic SQL (and there's absolutely no reason to in this case), then you need a using clause to bind the variable into the dynamic statement. In addition, making the SQL static will allow the SQL statement to be validated at compile-time, which has obvious advantages.
A revised packaged (specification and body) is below.
CREATE OR REPLACE PACKAGE comment_test IS
PROCEDURE sp_comment (p_member_id IN VARCHAR2,
p_member_lastname IN VARCHAR2,
p_member_firstname IN VARCHAR2,
p_member_startdate IN DATE,
p_member_enddate IN DATE,
p_product_cat_code IN VARCHAR2,
p_comment IN VARCHAR2,
comment_cursor OUT SYS_REFCURSOR);
END comment_test;
/
CREATE OR REPLACE PACKAGE BODY comment_test IS
PROCEDURE sp_comment (p_member_id IN VARCHAR2,
p_member_lastname IN VARCHAR2,
p_member_firstname IN VARCHAR2,
p_member_startdate IN DATE,
p_member_enddate IN DATE,
p_product_cat_code IN VARCHAR2,
p_comment IN VARCHAR2,
comment_cursor OUT SYS_REFCURSOR) IS
BEGIN
INSERT INTO test (member_id,
member_lastname,
member_firstname,
member_startdate,
member_enddate,
product_cat_code,
comments)
VALUES (p_member_id,
p_member_lastname,
p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment);
COMMIT;
OPEN comment_cursor FOR SELECT * FROM sconti.test;
END;
END comment_test;
/
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;
I have a table non_employee with emp_no as the primary key and a package with a procedure to insert in to this table.
I need to be able to autoincrement the emp_no when the procedure is run. I tried creating a sequence within the procedure like this, but getting errors. please see below and advice.
CREATE OR REPLACE PACKAGE BODY WFDDEV."ERD" IS
create SEQUENCE #seq_emp_nmbr;
PROCEDURE INS_NON_EMPLOYEES
(
in_DATE_ADDED DATE,
in_EMPLOYEE_NAME VARCHAR2,
in_DEPT_ID VARCHAR2,
in_SUB_DEPARTMENT VARCHAR2,
in_LOCATION VARCHAR2,
in_WORK_TEL_NO VARCHAR2,
in_TOUR VARCHAR2,
in_REST_DAYS VARCHAR2,
in_HOME_ADDRESS VARCHAR2,
in_CITY VARCHAR2,
in_STATE VARCHAR2,
in_ZIP VARCHAR2,
in_HOME_TEL_NO VARCHAR2,
in_GENDER VARCHAR2,
in_RACE VARCHAR2,
in_DATE_OF_BIRTH DATE,
in_AGE VARCHAR2,
in_HIRE_DATE DATE,
in_UNION_AFFILIATION VARCHAR2,
in_TITLE VARCHAR2,
in_NON_EE_INDICATOR VARCHAR2
) IS
BEGIN
INSERT INTO WFDDEV.NON_EMPLOYEES
(
EMP_NO,
DATE_ADDED,
EMPLOYEE_NAME,
DEPT_ID,
SUB_DEPARTMENT,
LOCATION,
WORK_TEL_NO,
TOUR,
REST_DAYS,
HOME_ADDRESS,
CITY,
STATE,
ZIP,
HOME_TEL_NO,
GENDER,
RACE,
DATE_OF_BIRTH,
AGE,
HIRE_DATE,
UNION_AFFILIATION,
TITLE,
NON_EE_INDICATOR
)
VALUES
(
emp_no.NEXTVAL,
in_DATE_ADDED,
in_EMPLOYEE_NAME,
in_DEPT_ID,
in_SUB_DEPARTMENT,
in_LOCATION,
in_WORK_TEL_NO,
in_TOUR,
in_REST_DAYS,
in_HOME_ADDRESS,
in_CITY,
in_STATE,
in_ZIP,
in_HOME_TEL_NO,
in_GENDER,
in_RACE,
in_DATE_OF_BIRTH,
in_AGE,
in_HIRE_DATE,
in_UNION_AFFILIATION,
in_TITLE,
in_NON_EE_INDICATOR
);
END;
Im getting PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
begin end function pragma procedure subtype type
error with this...
You need to create the sequence just once outside of the package as a separate database object. Then, in the insert statement in your package body you can reference the sequence to get the next value.
Try-
EXECUTE IMMEDIATE 'CREATE SEQUENCE SEQ_NAME START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 1000000 NOCYCLE NOCACHE ORDER';
This should be inside a Procedure or Function body and not in the Declaration section i.e. this should be treated as a executable statement.
Creating a sequence using Dynamic SQL is a bad idea and I am not sure why you would want to do that. However, if you are creating a sequence dynamically then remember to drop it as well after finishing up, using
EXECUTE IMMEDIATE 'DROP SEQUENCE SEQ_NAME';
This way you would atleast not run into errors (like ORA-00955) while calling the package procedure.