Alter Object type body [PL/SQL] - plsql

i´m triying to alter a body type of an object. Here is my code:
ALTER TYPE Profesor ADD CONSTRUCTOR FUNCTION Profesor(codigo INTEGER, nombre
VARCHAR2,
primerApellido VARCHAR2, segundoApellido VARCHAR2, especialidad VARCHAR2)
RETURN SELF AS RESULT CASCADE;
/
CREATE OR REPLACE TYPE BODY Profesor AS CONSTRUCTOR FUNCTION Profesor(codigo
INTEGER, nombre VARCHAR2,
primerApellido VARCHAR2, segundoApellido VARCHAR2, especialidad VARCHAR2)
RETURN SELF AS RESULT IS
BEGIN
SELF.codigo := codigo;
SELF.nombre := nombre;
SELF.apellidos := primerApellido||' '||segundoApellido;
SELF.especialidad := especialidad;
RETURN;
END;
END;
/
I´ve created the Object and then i altered it adding a constructor and creating a type body.
The code doesn´t show errors but if i try to add a new method it lets me add the method but not de code:
ALTER TYPE Profesor ADD MEMBER FUNCTION getNombreCompleto RETURN VARCHAR2
CASCADE;
/
ALTER TYPE BODY Profesor ADD MEMBER FUNCTION getNombreCompleto RETURN
VARCHAR2
CASCADE
IS
BEGIN
RETURN SELF.nombre;
END getNombreCompleto;
END;
And that´s the question, how can i alter the body type of an object?
Thanks!

This solution should work. Alter type member function\ constructor works only for type specification. For body you have to recreate body.
ALTER TYPE Profesor ADD CONSTRUCTOR FUNCTION Profesor(codigo INTEGER, nombre
VARCHAR2,
primerApellido VARCHAR2, segundoApellido VARCHAR2, especialidad VARCHAR2)
RETURN SELF AS RESULT CASCADE;
/
ALTER TYPE Profesor ADD MEMBER FUNCTION getNombreCompleto RETURN VARCHAR2
CASCADE;
/
CREATE OR REPLACE TYPE BODY Profesor AS
CONSTRUCTOR FUNCTION Profesor(codigo
INTEGER, nombre VARCHAR2,
primerApellido VARCHAR2, segundoApellido VARCHAR2, especialidad VARCHAR2)
RETURN SELF AS RESULT IS
BEGIN
SELF.codigo := codigo;
SELF.nombre := nombre;
SELF.apellidos := primerApellido||' '||segundoApellido;
SELF.especialidad := especialidad;
RETURN;
END;
MEMBER FUNCTION getNombreCompleto RETURN
VARCHAR2
CASCADE
IS
BEGIN
RETURN SELF.nombre;
END getNombreCompleto;
END;
END;
/

Related

How to execute the procedure

How to call the procedure and function from the package?
PROCEDURE P_INSERT_PER_LG(IN_DATA DATE,
IN_CHECK VARCHAR2,
IN_PSE VARCHAR2,
IN_TP_CHECK VARCHAR2,
IN_STATUS NUMBER,
IN_MSG VARCHAR2,
OUT_ERR_MSG OUT VARCHAR2);
FUNCTION F_GT_COD_PTH(IN_CODE IN VARCHAR2)RETURN C_GT_COD_OBJ;
A small sample package:
CREATE OR REPLACE
PACKAGE TEST_PKG AS
FUNCTION Get_Squere(p_num NUMBER) RETURN NUMBER;
PROCEDURE Put_Line(p_Text VARCHAR2);
END TEST_PKG;
-- .. and package body
CREATE OR REPLACE
PACKAGE BODY TEST_PKG AS
FUNCTION Get_Squere(p_num NUMBER) RETURN NUMBER AS
BEGIN
RETURN p_num * p_num;
END Get_Squere;
PROCEDURE Put_Line(p_Text VARCHAR2) AS
BEGIN
DBMS_OUTPUT.PUT_LINE(p_Text);
END Put_Line;
END TEST_PKG;
And plsql block calling procedure/function from the package
SET SERVEROUTPUT ON
DECLARE
m_number NUMBER(9);
m_text VARCHAR2(100) := 'Test - printed out by packaged Procedure...';
BEGIN
TEST_PKG.Put_Line(m_text); -- calling pacakaged procedure
m_number := TEST_PKG.Get_Squere(5); -- calling pakaged function
TEST_PKG.Put_Line(m_number || ' squered is ' || TEST_PKG.Get_Squere(m_number)); -- calling procedure with the value returned by function
END;
/
Result:
anonymous block completed
Test - printed out by packaged Procedure...
25 squered is 625

Getting "Compilation unit analysis terminated" errors in PL/SQL code

This is a subtype of "base_t". When I add the "set_name" or "set_genus" I get some errors.
Here is my code:
CREATE OR REPLACE
TYPE dwarf_t UNDER base_t
( name VARCHAR2(30)
, genus VARCHAR2(30)
, CONSTRUCTOR FUNCTION dwarf_t
( name VARCHAR2
, genus VARCHAR2) RETURN SELF AS RESULT
, OVERRIDING MEMBER FUNCTION get_name RETURN VARCHAR2
-- , OVERRIDING MEMBER FUNCTION set_name (name VARCHAR2)
, MEMBER FUNCTION get_genus RETURN VARCHAR2
, MEMBER FUNCTION set_genus (genus VARCHAR2)
, OVERRIDING MEMBER FUNCTION to_string RETURN VARCHAR2)
INSTANTIABLE NOT FINAL;
/
For reference, I have created get_name in the main type, but not set_name, or get/set_genus.
My errors are as follows:
ERROR at line 1:
ORA-06545: PL/SQL: compilation error - compilation aborted
ORA-06550: line 11, column 3:
PLS-00103: Encountered the symbol "," when expecting one of the following:
return
ORA-06550: line 0, column 0:
PLS-00565: DWARF_T must be completed as a potential REF target (object type)
The (not-so-helpful) error message is because you have a function without a return type. Since this is a SET, I assume you want a procedure instead of a function.
Change this line:
MEMBER FUNCTION set_genus (genus VARCHAR2)
To this:
MEMBER PROCEDURE set_genus (genus VARCHAR2)

Merging varchars into one CLOB

I have the following procedure for merging 10 varchar variables of length 4000 characters into one CLOB:
procedure insert_iec_by_parts(p_header_id in number,
p_contents_part1 in varchar2,
p_contents_part2 in varchar2,
p_contents_part3 in varchar2,
p_contents_part4 in varchar2,
p_contents_part5 in varchar2,
p_contents_part6 in varchar2,
p_contents_part7 in varchar2,
p_contents_part8 in varchar2,
p_contents_part9 in varchar2,
p_contents_part10 in varchar2,
o_cur_results out sys_refcursor,
result_code out number) is
l_clob clob;
begin
l_clob:=p_contents_part1||p_contents_part2||p_contents_part3||p_contents_part4||p_contents_part5||
p_contents_part6||p_contents_part7||p_contents_part8||p_contents_part9||p_contents_part10;
insert_iec(p_header_id, l_clob, o_cur_results, result_code );
end;
procedure insert_iec(p_header_id in number,
p_contents in clob,
o_cur_results out sys_refcursor,
result_code out number) is
id_temp number;
l_id number;
procedure SetError(pErrCode number) is
-- A centralised sub proc could allow for a quick change to raise_application_error at any time.
begin
result_code := pErrCode;
end;
begin
select count(*) into l_id from log_sync_calls_headers
where log_sync_calls_headers.id =p_header_id;
case
when (p_header_id is null) or (l_id <= 0) then SetError(9804);
when p_contents is null then SetError(9805);
else
-- fetch sequence number
id_temp := iec_seq.nextval;
result_code:=0;
open o_cur_results for
select id_temp as id
from dual;
insert into log_sync_calls_iecs
(id, header_id, contents)
values
(id_temp, p_header_id, p_contents );
commit;
end case;
exception when others then
result_code :=9701;
rollback;
pkg_common.insert_log_record(p_source => 'insert_iec',
p_type => 'Er',
p_message => sqlerrm);
end;
Procedure works fine when merging 10 varchar variables of 4000 characters each. However, I want to extend it to 20 varchar variables of length 4000 characters.
When I try that, it gives me the following error:
ORA 06502: character string buffer too small.
Could someone show me how to be able to extend this procedure to 20 varchar variables of length 4000 characters?
Use DBMS_LOB.APPEND function like in this question (or just search for DBMS_LOB.APPEND in SO): how to insert long string oracle clob or blob
you should use the DBMS_LOB package.
first you should create a clob object otherwise you variable is not initialized
dbms_lob.createtemporary(l_clob ,true);
now you can use append procedure
dbms_lob.append(l_clob, p_contents_part1 );
after you are finished , don't forget to release a clob Memory and destroy you clob object
dbms_lob.freetemporary(l_clob);

Z-ORA-01745: invalid host/bind variable name (PLSQL)

I kind of new on dynamic variables in plsql.
I am getting an error "invalid host/bind variable name" on EXECUTE IMMEDIATE
statement. Thanks in advance.
CREATE OR REPLACE PROCEDURE MY_CLASS_CONFIG_DML (pBuCode VARCHAR2,
pMyPKId VARCHAR2,
pMyName VARCHAR2,
pMyId VARCHAR2,
pRemarks VARCHAR2,
pUserId VARCHAR2)
AS
v_table VARCHAR2 (50);
v_lastdate DATE;
--bindable var.
v_pkey VARCHAR2 (12) := pMyPKId;
v_myname VARCHAR2 (30) := pMyName;
v_bucode VARCHAR2 (4) := pBuCode;
v_myid VARCHAR2 (12) := pMyId;
v_remarks VARCHAR2 (50) := pRemarks;
v_lastid VARCHAR2 (8) := pUserId;
BEGIN
v_lastdate := SYSDATE;
v_table := v_bucode || '_WORKSHEET_CONFIG';
--Error happens on this part
EXECUTE IMMEDIATE
'insert into '
|| v_table
|| '(pk_id,my_name,my_id,remarks,last_user,last_date)
values(:pkey,:myname,:myid,:remarks,:user,:lastdate)'
USING v_pkey,
v_myname,
v_myid,
v_remarks,
v_lastid,
v_lastdate;
--end comment
COMMIT;
RETURN;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
END MY_CLASS_CONFIG_DML;
Replace :user by another keyword: :myuser or :lastid. The colon should make it possible to use the reserved keyword "user", but this is the only problem i see.

What is the difference between Procedures and Functions in PLSQL?

I am trying to Insert a line into a table using PLSQL. The table name is DOCUMENT_ISSUE_HISTORY. I am using an API Procedure name PROCEDURE
Insert_New_Line_ (
doc_class_ IN VARCHAR2,
doc_no_ IN VARCHAR2,
doc_sheet_ IN VARCHAR2,
doc_rev_ IN VARCHAR2,
info_category_db_ IN VARCHAR2,
note_ IN VARCHAR2 );
I am confused if PRECEDURE can return value like function does.
I am doing this:
DECLARE
doc_class_ varchar2(4000) := 'CVS FILE';
doc_no_ varchar2(4000) := '01004901.DWG-DWF';
doc_sheet_ varchar2(20) := 1;
doc_rev_ varchar2(20) := -1;
info_category_db_ VARCHAR2(20) := NULL;
note_ VARCHAR2(4000) := 'TEXTING TO UPDATE or FIeld to update';
BEGIN
Document_Issue_History_API.Insert_New_Line__ (doc_class_ ,doc_no_,doc_sheet_,doc_rev_ ,info_category_db_,note_);
end;
How can I can insert the line in the table?
Functions have return values and hence may appear anywhere an expression may be used (such as to the right of the assignment operator, or as a column expression in a SQL statement) while Procedures do not have return values and may not be used as expressions.
This is not to say that procedures can't return values, because they can as long as they are OUT or IN/OUT parameters. You just can't use the procedure directly in an expression.
Similarly since Functions do have a return value (other than those parameters specified as OUT or IN/OUT parameters) they must be used in an expression and can't be called directly.

Resources