Oracle SQL Procedure error - sql

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.

Related

The procedure throws error "Can not insert null in"CUSTOMER"."CUST_ID" although the value is not null

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;

Procedure with sequence error - PLS-00103 occurring need guidance

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;
;

Stored procedure and IF statement

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;

Encountered the symbol "#" when expecting one of the following sql

I am tring to create a stored procedure that will add a horse to my currently table but i am getting this error. Does anyone know why this error is occuring?
CREATE OR REPLACE PROCEDURE ADDHORSE (
HorseID IN INTEGER,
Stable IN VARCHAR2,
Horse_Name IN VARCHAR2,
DateofBirth IN DATE,
Sex Char:=1);
Height FLOAT,
Dam VARCHAR2:=30),
Sire VARCHAR2:=30)
)
AS
BEGIN
INSERT INTO horse(
[HorseID],
[Stable],
[Horse_Name],
[DateofBirth],
[Sex],
[Height],
[Dam],
[Sire]
) VALUES (
HorseID,
Stable,
Horse_Name,
DateofBirth,
Sex,
Height,
Dam,
Sire
)
END ADDHORSE;
I noted you had some missing quotes (') with your varchar and char and a missing ';' at the end of the insert.
This works for me:
CREATE OR REPLACE PROCEDURE ADDHORSE (
HorseID IN INTEGER,
Stable IN VARCHAR2,
Horse_Name IN VARCHAR2,
DateofBirth IN DATE,
Sex Char := '1',
Height FLOAT,
Dam VARCHAR2 := '30',
Sire VARCHAR2 := '30'
)
AS
BEGIN
INSERT INTO horse(
HorseID,
Stable,
Horse_Name,
DateofBirth,
Sex,
Height,
Dam,
Sire
) VALUES (
HorseID,
Stable,
Horse_Name,
DateofBirth,
Sex,
Height,
Dam,
Sire
);
END ADDHORSE;
You have a semi-colon and an extra bracket in your parameter list, does this work for you...
CREATE PROCEDURE ADDHORSE (
HorseID INTEGER,
Stable VARCHAR2,
Horse_Name VARCHAR2,
DateofBirth DATE,
Sex CHAR(1),
Height FLOAT,
Dam VARCHAR2(30),
Sire VARCHAR2(30)
)
AS
BEGIN
INSERT INTO horse(
[HorseID],
[Stable],
[Horse_Name],
[DateofBirth],
[Sex],
[Height],
[Dam],
[Sire]
) VALUES (
HorseID,
Stable,
Horse_Name,
DateofBirth,
Sex,
Height,
Dam,
Sire
)
END ADDHORSE;

function that will only insert Monday-Friday?

I've created a function that will insert a customer into the database, I just wondered if it was possible to make it so it will only except inserts on Monday to Friday days at how this would be done in Oracle SQL?
Here is the code for the function that is running and works
FUNCTION CREATE_CUSTOMER(
Country IN VARCHAR2
,First_Name IN VARCHAR2
,Last_Name IN VARCHAR2
,Birth_Date IN VARCHAR2
,Customer_Type IN VARCHAR2
,Address IN VARCHAR2
) return VARCHAR2 IS
new_customer_id VARCHAR2(8);
BEGIN
SELECT custid_seq.NEXTVAL
INTO new_customer_id
FROM DUAL;
INSERT INTO customer (Customer_id, Country, First_Name, Last_name, Birth_date, Customer_Type, Address)
VALUES (new_customer_id, Country, First_Name, Last_name, Birth_date, Customer_Type, Address);
total_customers := total_customers + 1;
RETURN (new_customer_id);
end;
Anyone got any idea how to develop this or if its possible?
thanks
You want the insert statement to run only on Weekdays? If so, you can check the day of the weeks using
to_char(sysdate,'D')
It returns numbers 1-7(Sunday to Saturday). Based on this you can decide whether to insert or not.