Home » SQL & PL/SQL » SQL & PL/SQL » Unable to compile procedure (ORACLE)
Unable to compile procedure [message #689232] Thu, 26 October 2023 12:49 Go to next message
DorababuMeka
Messages: 11
Registered: October 2023
Junior Member
I have created an OBJECT TYPE as follows and referring it in TABLE TYPE

reate or replace TYPE EMP_CSV_OBJ AS OBJECT
(
   FirstName VARCHAR2(50),
   LastName VARCHAR2(50),
   MiddleName VARCHAR2(50)
);

create or replace TYPE EMP_CSV_TABLE
AS TABLE OF emp_csv_obj;

I am referring the table type in one of my PROC

CREATE OR REPLACE PROCEDURE INSERT_EMPLOYEE(empCsv IN EMP_CSV_TABLE) AS
BEGIN
  MERGE INTO Employee pt
  USING empCsv src
  ON (pt.FIRSTNAME = src.FIRSTNAME)
 WHEN NOT MATCHED THEN INSERT 
  (pt.FIRSTNAME, pt.LASTNAME, pt.MIDDLENAME) 
  VALUES (ps.FirstName , ps.LastName , ps.MIDDLENAME);
  
  COMMIT;
END;
Unable to compile any can some one help me
Re: Unable to compile procedure [message #689234 is a reply to message #689232] Thu, 26 October 2023 13:27 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Always post your Oracle version, with 4 decimals (query v$version), as often solution depends on it.

There are several errors in your procedure (USING clause, "ps." instead of "src.". Here's a correct version:
SQL> create or replace TYPE EMP_CSV_OBJ AS OBJECT
  2  (
  3     FirstName VARCHAR2(50),
  4     LastName VARCHAR2(50),
  5     MiddleName VARCHAR2(50)
  6  );
  7  /

Type created.

SQL> create or replace TYPE EMP_CSV_TABLE
  2  AS TABLE OF emp_csv_obj;
  3  /

Type created.

SQL> CREATE OR REPLACE PROCEDURE INSERT_EMPLOYEE(empCsv IN EMP_CSV_TABLE) AS
  2  BEGIN
  3    MERGE INTO Employees pt
  4    USING (select * from table(empCsv)) src
  5    ON (pt.FIRST_NAME = src.FIRSTNAME)
  6   WHEN NOT MATCHED THEN INSERT
  7    (pt.FIRST_NAME, pt.LAST_NAME, pt.MIDDLE_NAME)
  8    VALUES (src.FirstName , src.LastName , src.MIDDLENAME);
  9
 10  -- COMMIT;
 11  END;
 12  /

Procedure created.
Note: do NOT commit inside a procedure; the caller knows if he wants to commit or to rollback what the procedure does, the procedure doesn't without speaking that your procedure would also commit work that has been done before its call which may something the caller does not want.

Re: Unable to compile procedure [message #689236 is a reply to message #689234] Thu, 26 October 2023 16:20 Go to previous messageGo to next message
mathguy
Messages: 107
Registered: January 2023
Senior Member
Or, depending on your Oracle version, you may be able to simplify to this:

CREATE OR REPLACE PROCEDURE INSERT_EMPLOYEE(empCsv IN EMP_CSV_TABLE) AS
BEGIN
  MERGE INTO Employee pt
  USING TABLE(empCsv) src
  ON (pt.FIRSTNAME = src.FIRSTNAME)
 WHEN NOT MATCHED THEN INSERT 
  (pt.FIRSTNAME, pt.LASTNAME, pt.MIDDLENAME) 
  VALUES (src.FirstName , src.LastName , src.MIDDLENAME);
END;
/
This works at least in Oracle 12.2 (the version I tested in).
Re: Unable to compile procedure [message #689238 is a reply to message #689236] Fri, 27 October 2023 00:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You're right this works at least from 10gR2. Smile

Re: Unable to compile procedure [message #689239 is a reply to message #689238] Fri, 27 October 2023 04:46 Go to previous messageGo to next message
DorababuMeka
Messages: 11
Registered: October 2023
Junior Member
Here is the version


Oracle SQL Developer

Oracle IDE 20.4.1.407.0006
Version 20.4.1.407
Build 407.0006
  • Attachment: version.png
    (Size: 22.38KB, Downloaded 550 times)

[Updated on: Fri, 27 October 2023 04:53]

Report message to a moderator

Re: Unable to compile procedure [message #689240 is a reply to message #689234] Fri, 27 October 2023 05:14 Go to previous messageGo to next message
DorababuMeka
Messages: 11
Registered: October 2023
Junior Member
Michel Cadot wrote on Thu, 26 October 2023 13:27

Always post your Oracle version, with 4 decimals (query v$version), as often solution depends on it.

There are several errors in your procedure (USING clause, "ps." instead of "src.". Here's a correct version:
SQL> create or replace TYPE EMP_CSV_OBJ AS OBJECT
  2  (
  3     FirstName VARCHAR2(50),
  4     LastName VARCHAR2(50),
  5     MiddleName VARCHAR2(50)
  6  );
  7  /

Type created.

SQL> create or replace TYPE EMP_CSV_TABLE
  2  AS TABLE OF emp_csv_obj;
  3  /

Type created.

SQL> CREATE OR REPLACE PROCEDURE INSERT_EMPLOYEE(empCsv IN EMP_CSV_TABLE) AS
  2  BEGIN
  3    MERGE INTO Employees pt
  4    USING (select * from table(empCsv)) src
  5    ON (pt.FIRST_NAME = src.FIRSTNAME)
  6   WHEN NOT MATCHED THEN INSERT
  7    (pt.FIRST_NAME, pt.LAST_NAME, pt.MIDDLE_NAME)
  8    VALUES (src.FirstName , src.LastName , src.MIDDLENAME);
  9
 10  -- COMMIT;
 11  END;
 12  /

Procedure created.
Note: do NOT commit inside a procedure; the caller knows if he wants to commit or to rollback what the procedure does, the procedure doesn't without speaking that your procedure would also commit work that has been done before its call which may something the caller does not want.

Thanks this works for me
Re: Unable to compile procedure [message #689241 is a reply to message #689240] Fri, 27 October 2023 05:15 Go to previous messageGo to next message
DorababuMeka
Messages: 11
Registered: October 2023
Junior Member
I have an issue while integrating this with C# code where can I post that query
Re: Unable to compile procedure [message #689242 is a reply to message #689241] Fri, 27 October 2023 05:44 Go to previous message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You can try in our Windows forum but we have few regulars working with C#.
You can also try C# forum, I think some of them have an Oracle section.

Previous Topic: Need a pl/sql block to be entered in existing package
Next Topic: Create a oracle function that parse a string to date time when not null
Goto Forum:
  


Current Time: Sat Apr 27 16:57:24 CDT 2024