Hi stack overflow i am new to sql and im trying to create a procedure to add in new consultant. i will be re using the code in a apex application with a button to Execute the task. Also I have implemented sequence to add to new entry of consultant. Unfortunately I have the error
Compilation failed,line 10 (11:48:18)
PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following: ; is with authid as cluster order using external deterministic parallel_enable pipelined result_cache accessible
could i get some guidance to address this issue as im new to sql thanks guys and research is not helping.
original code
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2)
RETURN NUMBER IS
new_cst NUMBER;
BEGIN
SELECT CONSULTANT_IDSEQ.NEXTVAL
INTO new_cst
FROM DUAL;
INSERT INTO LDS_CONSULTANT (CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES (new_cst, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
RETURN(new_cst);
END;
removed the return
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2)
BEGIN
new_cst NUMBER;
SELECT CONSULTANT_IDSEQ.NEXTVAL
INTO new_cst
FROM DUAL;
INSERT INTO LDS_CONSULTANT (CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES (new_cst, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
END;
Note: I am ignoring the apparent mismatch between the declared parameter names and the ones used in the INSERT statement.
As documented in the manual you need the AS (or IS) keyword that starts the actual procedure part - after which the variable declaration needs to be written:
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2)
AS --<< HERE
new_cst NUMBER;
BEGIN
SELECT CONSULTANT_IDSEQ.NEXTVAL
INTO new_cst
FROM DUAL;
INSERT INTO LDS_CONSULTANT
(CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES
(new_cst, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
END;
However, the SELECT INTO is not required at all, you can use nextval directly in the INSERT statement. So you can simplify the procedure to:
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2)
AS --<< Still needed!
BEGIN
INSERT INTO LDS_CONSULTANT
(CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES
(CONSULTANT_IDSEQ.NEXTVAL, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
END;
If you want to return the generated ID from the procedure you need an OUT parameter:
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2,
p_consultant_id out integer) --<< HERE
AS
BEGIN
-- Assign the value to the OUT parameter
p_consultant_id := CONSULTANT_IDSEQ.NEXTVAL;
INSERT INTO LDS_CONSULTANT
(CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES
(p_consultant_id, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
END;
/
Following is the syntax of procedre-
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
And you forget 'IS' keyword
following is the code-
create or replace procedure hirecst
(CST_NAME VARCHAR2,
START_DATE DATE,
LEAVE_DATE DATE,
LOCATION VARCHAR2,
SPECIALIST_AREA VARCHAR2)
IS
new_cst NUMBER;
begin
SELECT CONSULTANT_IDSEQ.NEXTVAL
INTO new_cst
FROM DUAL;
INSERT INTO LDS_CONSULTANT (CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES (new_cst, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
end;
;
Related
It seems that my procedure does not accept the value which I am trying to insert into the customer table. But why? What is wrong with the cust_id column?
BEGIN
do_new_customer('650707-1111', 'Tito', 'Ortiz', 'qwerTY');
do_new_customer('560126-1148', 'Margreth', 'Andersson', 'olle85');
do_new_customer('840317-1457', 'Mary', 'Smith', 'asdfgh');
do_new_customer('861124-4478', 'Vincent', 'Ortiz', 'qwe123');
COMMIT;
END;
This is my procedure:
create or REPLACE procedure do_new_customer
(p_cust_id in varchar2,
p_first_name in varchar2,
P_last_name in varchar2,
P_passwd in varchar2)
as
v_cust_id number(6);
begin
insert into Customer (cust_id, first_name, last_name, passwd)
values (v_cust_id, P_First_name, P_Last_name, P_passwd);
Commit;
end;
The value is null: I think you forgot the step of converting p_cust_id to a number and storing it in v_cust_id
create or REPLACE procedure do_new_customer
(p_cust_id in varchar2, p_first_name in varchar2, P_last_name in varchar2, P_passwd in varchar2)
as
--YOU DIDN'T SET THIS TO A VALUE SO IT'S NULL
v_cust_id number(6);
begin
insert into Customer( cust_id, first_name, last_name, passwd)
--STILL NULL WHEN YOU CAME TO USE IT
values(v_cust_id, P_First_name,P_Last_name,P_passwd);
Commit;
end;
Try putting SET v_cust_id = p_cust_id::int; or similar operation to cast the varchar to something big numeric, and give v_cust_num a value?
Or as Tim says, pass it directly as p_cust_id, let Postgres figure out the conversion.
Or make p_cust_id match the type of the column in the table and get rid of v_cust_id all together
the variable v_cust_id is not set for the Insert Operation but defined that why you get null.
What you surely want to do is to insert p_cust_id into the customer Table so
instead this :
insert into Customer (cust_id, first_name, last_name, passwd)
values (v_cust_id, P_First_name, P_Last_name, P_passwd);
rewrite the insert:
insert into Customer (cust_id, first_name, last_name, passwd)
values (P_cust_id, P_First_name, P_Last_name, P_passwd);
The fonction will be :
create or REPLACE procedure do_new_customer
(p_cust_id in varchar2,
p_first_name in varchar2,
P_last_name in varchar2,
P_passwd in varchar2)
as
v_cust_id number(6);
begin
insert into Customer (cust_id, first_name, last_name, passwd)
values (P_cust_id, P_First_name, P_Last_name, P_passwd);
Commit;
end;
I think the issue here is that you are passing v_cust_id as the cust_id column value, yet this value does not get defined anywhere. I don't see any reason why you aren't just using all the inputs in the VALUES clause:
CREATE OR REPLACE PROCEDURE do_new_customer (p_cust_id IN VARCHAR2,
p_first_name IN VARCHAR2, P_last_name IN VARCHAR2, P_passwd IN VARCHAR2)
AS
BEGIN
INSERT INTO Customer (cust_id, first_name, last_name, passwd)
VALUES
(p_cust_id, p_first_name, P_last_name, P_passwd);
COMMIT;
END;
create or REPLACE procedure do_new_customer
(p_cust_id in varchar2,
p_first_name in varchar2,
P_last_name in varchar2,
P_passwd in varchar2)
as
v_cust_id number(6);
begin
insert into Customer values (p_cust_id, P_First_name, P_Last_name, P_passwd);
Commit;
end;
-- there is no use of this variable- 'v_cust_id number(6)';
-- create procedure as-
CREATE OR REPLACE PROCEDURE do_new_customer
(
p_cust_id IN VARCHAR2,
p_first_name IN VARCHAR2,
P_last_name IN VARCHAR2,
P_passwd IN VARCHAR2
)
AS
BEGIN
INSERT INTO Customer (cust_id, first_name, last_name, passwd)
VALUES (p_cust_id, p_first_name, P_last_name, P_passwd);
COMMIT;
END;
Bellow is my procedure.
CREATE OR REPLACE PROCEDURE "CUSTOMER_INCREMENTAL" (
ID VARCHAR2,
TITLE VARCHAR2,
SHORT_NAME VARCHAR2,
ACCOUNT_OFFICER VARCHAR2,
LEGAL_ID VARCHAR2,
LOCAL_REF_23 VARCHAR2,
LOCAL_REF_90 VARCHAR2,
COUNTRY VARCHAR2,
BIRTH_INCORP_DATE VARCHAR2,
LOCAL_REF_1 VARCHAR2,
CUSTOMER_STATUS VARCHAR2,
INDUSTRY VARCHAR2,
LEGAL_ID_2 VARCHAR2,
NET_MONTHLY_IN VARCHAR2,
GENDER VARCHAR2,
MARITAL_STATUS VARCHAR2,
OCCUPATION VARCHAR2,
EMPLOYERS_NAME VARCHAR2,
OFF_PHONE VARCHAR2,
EMPLOYERS_ADD VARCHAR2,
NO_OF_DEPENDENTS VARCHAR2,
INTRODUCER VARCHAR2,
DATE_TIME DATE,
LAST_KYC_REVIEW_DATE VARCHAR2,
JOB_TITLE VARCHAR2,
EMPLOYMENT_STATUS VARCHAR2,
LOCAL_REF_22 VARCHAR2,
LOCAL_REF_99 VARCHAR2,
LEGAL_EXP_DATE VARCHAR2,
DATE_OF_BIRTH VARCHAR2,
NATIONALITY VARCHAR2,
GIVEN_NAMES VARCHAR2,
FAMILY_NAME VARCHAR2,
LOCAL_REF_87 VARCHAR2,
NAME_2 VARCHAR2,
STREET VARCHAR2,
ADDRESS VARCHAR2,
TOWN_COUNTRY VARCHAR2,
LOCAL_REF_20 VARCHAR2,
LOCAL_REF_21 VARCHAR2,
COUNTRY_2 VARCHAR2,
POST_CODE VARCHAR2,
PHONE_1 VARCHAR2,
FAX_1 VARCHAR2,
SMS_1 VARCHAR2,
EMAIL_1 VARCHAR2,
LOCAL_REF_66 VARCHAR2,
LOCAL_REF_67 VARCHAR2,
LOCAL_REF_68 VARCHAR2,
LOCAL_REF_69 VARCHAR2,
LOCAL_REF_70 VARCHAR2,
LOCAL_REF_71 VARCHAR2,
LOCAL_REF_81 VARCHAR2,
LOCAL_REF_72 VARCHAR2,
RELATION_CODE VARCHAR2,
BASEL_SG VARCHAR2,
LEGAL_DOC_NAME VARCHAR2,
LEGAL_ID_3 VARCHAR2,
LEGAL_ISS_AUTH VARCHAR2,
LEGAL_EXP_DATE_2 VARCHAR2,
DATE_TIME_2 VARCHAR2,
LOCAL_REF_4 VARCHAR2,
MNEMONIC VARCHAR2,
PEP VARCHAR2,
RESIDENCE VARCHAR2,
OTHER_NATIONALITY VARCHAR2,
FADCA_CUS_POB VARCHAR2,
RUN_DATE DATE,
TABLE_NAME VARCHAR2
) IS
BEGIN
INSERT INTO NDB_AML_CUSTOMER
(ID, TITLE, SHORT_NAME, ACCOUNT_OFFICER, LEGAL_ID, LOCAL_REF_23,
LOCAL_REF_90, COUNTRY, BIRTH_INCORP_DATE,LOCAL_REF_1, CUSTOMER_STATUS,
INDUSTRY, LEGAL_ID_2, NET_MONTHLY_IN, GENDER, MARITAL_STATUS,
OCCUPATION, EMPLOYERS_NAME, OFF_PHONE, EMPLOYERS_ADD, NO_OF_DEPENDENTS,
INTRODUCER, DATE_TIME, LAST_KYC_REVIEW_DATE, JOB_TITLE,
EMPLOYMENT_STATUS, LOCAL_REF_22, LOCAL_REF_99, LEGAL_EXP_DATE,
DATE_OF_BIRTH, NATIONALITY, GIVEN_NAMES, FAMILY_NAME, LOCAL_REF_87,
NAME_2, STREET, ADDRESS, TOWN_COUNTRY, LOCAL_REF_20, LOCAL_REF_21,
COUNTRY_2, POST_CODE, PHONE_1, FAX_1, SMS_1, EMAIL_1, LOCAL_REF_66,
LOCAL_REF_67, LOCAL_REF_68, LOCAL_REF_69, LOCAL_REF_70,
LOCAL_REF_71, LOCAL_REF_81, LOCAL_REF_72, RELATION_CODE, BASEL_SG,
LEGAL_DOC_NAME, LEGAL_ID_3, LEGAL_ISS_AUTH, LEGAL_EXP_DATE_2,
DATE_TIME_2, LOCAL_REF_4, MNEMONIC, PEP, RESIDENCE, OTHER_NATIONALITY,
FADCA_CUS_POB)
SELECT ID, TITLE, SHORT_NAME, ACCOUNT_OFFICER, LEGAL_ID, LOCAL_REF_23,
LOCAL_REF_90, COUNTRY, BIRTH_INCORP_DATE, LOCAL_REF_1,
CUSTOMER_STATUS, INDUSTRY, LEGAL_ID_2, NET_MONTHLY_IN, GENDER,
MARITAL_STATUS, OCCUPATION, EMPLOYERS_NAME, OFF_PHONE,
EMPLOYERS_ADD, NO_OF_DEPENDENTS, INTRODUCER, DATE_TIME,
LAST_KYC_REVIEW_DATE, JOB_TITLE, EMPLOYMENT_STATUS, LOCAL_REF_22,
LOCAL_REF_99, LEGAL_EXP_DATE, DATE_OF_BIRTH, NATIONALITY,
GIVEN_NAMES, FAMILY_NAME, LOCAL_REF_87, NAME_2, STREET, ADDRESS,
TOWN_COUNTRY, LOCAL_REF_20, LOCAL_REF_21, COUNTRY_2, POST_CODE,
PHONE_1, FAX_1, SMS_1, EMAIL_1, LOCAL_REF_66, LOCAL_REF_67,
LOCAL_REF_68, LOCAL_REF_69, LOCAL_REF_70, LOCAL_REF_71,
LOCAL_REF_81, LOCAL_REF_72, RELATION_CODE, BASEL_SG,
LEGAL_DOC_NAME, LEGAL_ID_3, LEGAL_ISS_AUTH, LEGAL_EXP_DATE_2,
DATE_TIME_2, LOCAL_REF_4, MNEMONIC, PEP, RESIDENCE,
OTHER_NATIONALITY, FADCA_CUS_POB
FROM NDB_CUSTOMER_NEW
WHERE DATE_TIME > (SELECT RUN_DATE
FROM CHECK_POINT
WHERE TABLE_NAME = (SELECT TABLE_NAME
FROM ALL_TABLES
WHERE TABLE_NAME='NDB_CUSTOMER_NEW'));
UPDATE CHECK_POINT
SET RUN_DATE = SYSDATE;
COMMIT;
END;
/
When I execute I'm getting
Wrong number or types of arguments
You've declared lots of formal parameters for your procedure, but you don't ever use them. All of the values you're inserting into NDB_AML_CUSTOMER are coming from NDB_CUSTOMER_NEW.
So your procedure declaration just needs to be:
CREATE OR REPLACE PROCEDURE "CUSTOMER_INCREMENTAL"
IS
BEGIN
INSERT INTO ...
UPDATE ...
COMMIT;
END;
The subquery against ALL_TABLES isn't necessary; all it can really do is check the table exists, but if it doesn't then the parser will have thrown an error before it executes that subquery. The subquery to get the RUN_DATE could just be:
SELECT RUN_DATE FROM CHECK_POINT WHERE TABLE_NAME = 'NDB_CUSTOMER_NEW'
But assuming that means you have multiple records with different run dates for each target table, your update needs to only touch that same single record:
UPDATE CHECK_POINT SET RUN_DATE = SYSDATE WHERE TABLE_NAME = 'NDB_CUSTOMER_NEW';
You also potentially have some issues with the tracking you're doing. Firstly sessions could execute this at the same time and see the same RUN_DATE value, potentially causing data duplication or constraint violations. Secondly you're leaving a (small) gap between when the time you ran the insert and the time you record when you update RUN_DATE; if records are added to NDB_CUSTOMER_NEW with DATE_TIME in that gap they will never be processed. You might want to read about for update and current of to manage that more safely.
I am trying to write a stored procedure in oracle that inserts new data into a table, but before the data is inserted i want it to check if a client is taking treatments that could be conflicting with one another. I have been trying to apply IF statement to get this working, but have been unsucceful, here is the code i have at the moment:
CREATE OR REPLACE PROCEDURE FPRESC (
FP_ID VARCHAR2,
FTREAT_ID VARCHAR2,
FCLIENT_ID VARCHAR2,
FDOC_ID VARCHAR2)
AS
V_CLIENT_ID PRESCRIPTION.CLIENT_ID%TYPE;
V_CONFLICT TREATMENTS.CONFLICT%TYPE;
BEGIN
IF (FCLIENT_ID = V_CLINET_ID AND FTREAT_ID = V_CONFLICT)
BEGIN
DBMS_OUTPUT.PUT_LINE('CONFLICT');
END;
ELSE
BEGIN
INSERT INTO PRESCRIPTION (P_ID, TREAT_ID, CLIENT_ID, DOC_ID)
VALUES (FP_ID, FTREAT_ID, FCLIENT_ID, FDOC_ID);
END FPRESC;
and an example of how i would call the procedure:
EXEC FPRESC ('P00011', 'T016', 'C00017', 'D006');
Would appreciate some help on what i should change or how i can approach this.
CREATE OR REPLACE PROCEDURE FPRESC (
FP_ID VARCHAR2,
FTREAT_ID VARCHAR2,
FCLIENT_ID VARCHAR2,
FDOC_ID VARCHAR2)
AS
V_CLIENT_ID PRESCRIPTION.CLIENT_ID%TYPE;
V_CONFLICT TREATMENTS.CONFLICT%TYPE;
BEGIN
IF FCLIENT_ID = V_CLIENT_ID AND FTREAT_ID = V_CONFLICT THEN
DBMS_OUTPUT.PUT_LINE('CONFLICT');
ELSE
INSERT INTO PRESCRIPTION (P_ID, TREAT_ID, CLIENT_ID, DOC_ID)
VALUES (FP_ID, FTREAT_ID, FCLIENT_ID, FDOC_ID);
END IF;
END FPRESC;
Try this
CREATE OR REPLACE PROCEDURE FPRESC (
FP_ID VARCHAR2,
FTREAT_ID VARCHAR2,
FCLIENT_ID VARCHAR2,
FDOC_ID VARCHAR2)
AS
V_CLIENT_ID PRESCRIPTION.CLIENT_ID%TYPE;
V_CONFLICT TREATMENTS.CONFLICT%TYPE;
BEGIN
IF FCLIENT_ID = V_CLIENT_ID AND FTREAT_ID = V_CONFLICT THEN
BEGIN
DBMS_OUTPUT.PUT_LINE('CONFLICT');
END;
ELSE
BEGIN
INSERT INTO PRESCRIPTION (P_ID, TREAT_ID, CLIENT_ID, DOC_ID)
VALUES (FP_ID, FTREAT_ID, FCLIENT_ID, FDOC_ID);
END;
END IF;
END FPRESC;
I am trying to find the Date and time when QTY for Item A and Loc UK gets updated.
Can I have where clause in trigger??
CREATE TABLE tr_test (
item VARCHAR2 (10),
loc VARCHAR2 (10),
WHEN DATE,
qty );
insert into TR_TEST values ('A','UK',to_date('01/01/2013','MM/DD/YYYY',100);
insert into TR_TEST values ('B','US',to_date('01/01/2013','MM/DD/YYYY',200);
CREATE OR REPLACE TRIGGER "TEST_TRIGGER"
BEFORE UPDATE
ON tr_test
FOR EACH ROW
BEGIN
IF UPDATING ('QTY')
THEN
INSERT INTO tr_test_1
(item, loc, WHEN, qty)
SELECT :OLD.item, :OLD.loc, SYSDATE, :OLD.qty
FROM DUAL;
INSERT INTO tr_test_1
(item`enter code here`, loc, WHEN, oh)
SELECT :OLD.item, :OLD.loc, SYSDATE, :NEW.qty
FROM DUAL;
END IF;
END;
/
I have to write a stored proc for the below requirement.
Desired output : TAX table
CATEGORY_ID TAX_PERCENTAGE FROM_DATE CREATE_DATE TAX_ID TO_DATE
-------------------------------------------------------------------------------------
1 10 4/1/2012 12/19/2013 8:54:20 PM 61 31-MAR-13
1 12.5 4/1/2013 12/19/2013 8:54:44 PM 62 31-dec-9998
When a new CATEGORY_ID say 1 with tax_percentage 10 for business year(from_date) 4/1/2012 is inserted, then a default value should be placed in to_date column say 31-dec-9998. When i try to input the same cat_id 1 with diff set of values say tax_percentage 12.5, this time from_date 4/1/2013 then to_date value should be (from_date-1) of cat_id 1,tax_percent 10 and my to_date value of 10,cat_id=1 should be updated with default date 31-dec-9998.something like this
CREATE OR REPLACE procedure SCOTT.Sp_SaveNewTaxPercentage
(
iv_category_id number,
iv_tax_percentage varchar2,
iv_from_date varchar2,
iv_to_date varchar2 default '12/31/9998' ,
iv_created_date date,
ov_err_code out nocopy varchar2,
ov_err_msg out nocopy varchar2
)
is
lv_category_id varchar2(25);
LV_TO_DATE varchar2(25);
lv_cat_id varchar2(12);
begin
ov_err_code:=0;
for J in ( SELECT CATEGORY_ID,FROM_DATE FROM TAX_P WHERE CATEGORY_ID =IV_CATEGORY_ID)
loop
SELECT to_date(iv_from_date,'dd/mm/yyyy') -1 INTO LV_TO_DATE FROM DUAL;
lv_cat_id := J.CATEGORY_ID;
end loop;
update tax_p set TO_DATE=LV_TO_DATE where CATEGORY_ID =lv_cat_id;
commit;
IF lv_cat_id IS NULL THEN
LV_TO_DATE := iv_to_Date;
END IF;
insert into tax_p( TAX_ID , CATEGORY_ID ,TAX_PERCENTAGE, FROM_DATE ,TO_DATE,CREATE_DATE)
values(tax_seq.nextval,iv_category_id,iv_tax_percentage,iv_from_date,LV_TO_DATE,sysdate);
select 'Successfully Saved' into ov_err_msg from dual;
commit;
Exception
when others then
rollback;
ov_err_code:=1;
ov_err_msg:='Error while saving'||SQLERRM;
end Sp_SaveNewTaxPercentage;
/
show errors;
I think you don't need to loop records to update the to_date field, just a single UPDATE statement will work for you.
BEGIN
UPDATE tax_p
SET to_date = to_date(iv_from_date,'dd/mm/yyyy') -1
WHERE category_id = iv_category_id;
INSERT INTO tax_p(tax_id, category_id, tax_percentage, from_date, to_date, create_date)
VALUES(tax_seq.nextval, iv_category_id, iv_tax_percentage, iv_from_date, iv_to_date, SYSDATE);
COMMIT;
EXCEPTION WHEN ...
...
END;