PLSQL TYPE modifications - sql

I have created the below type,
create or replace
TYPE MSSINT.TEST_TYPE
AS OBJECT
(service_phone_num varchar2(15),
name_last varchar2(50),
name_first varchar2(50));
Now I need to change the dataype of service_phone_num to NUMBER.
Can we achieve this using the below command or something like below?
ALTER TYPE MSSINT.TEST_TYPE MODIFY ATTRIBUTE (service_phone_num NUMBER) CASCADE;

Execute the following:
create or replace
TYPE MSSINT.TEST_TYPE
AS OBJECT
(service_phone_num number(15),
name_last varchar2(50),
name_first varchar2(50));
Should work, as you have create or replace.

Use the command
create or replace
TYPE MSSINT.TEST_TYPE
AS OBJECT
(service_phone_num number,
name_last varchar2(50),
name_first varchar2(50));
Replace modify your type.

Related

Oracle Object-Relational - PLS-00538 error and PLS-00539 error

Type definition:
CREATE TYPE INTERVENTO_TY AS OBJECT(
Testo VARCHAR2(20),
Timestmp DATE
) NOT FINAL;
CREATE TYPE COMMENTO_TY UNDER INTERVENTO_TY(
Ordine VARCHAR2(20)
);
CREATE TYPE POST_TY UNDER INTERVENTO_TY(
Titolo VARCHAR2(20),
MEMBER PROCEDURE AddCom(Ordine VARCHAR2, Testo VARCHAR2, Timestmp Date)
);
CREATE TYPE AUTORE_TY AS OBJECT(
Nome VARCHAR2(20),
Cognome VARCHAR2(20),
IdAutore INT
);
CREATE TYPE MEDIA_TY AS OBJECT(
Tipo VARCHAR2(20),
Nome VARCHAR2(20),
IdMedia INT,
Titolo VARCHAR2(20)
);
CREATE TYPE SCRIVE_INTERVENTO AS TABLE OF REF INTERVENTO_TY;
CREATE TYPE DI_AUTORE AS TABLE OF REF AUTORE_TY;
ALTER TYPE INTERVENTO_TY ADD ATTRIBUTE DI DI_AUTORE CASCADE;
ALTER TYPE AUTORE_TY ADD ATTRIBUTE SCRIVE SCRIVE_INTERVENTO CASCADE;
CREATE TYPE IN_INTERVENTO AS TABLE OF REF INTERVENTO_TY;
CREATE TYPE CONTIENE_MEDIA AS TABLE OF REF MEDIA_TY;
ALTER TYPE INTERVENTO_TY ADD ATTRIBUTE CONTIENE CONTIENE_MEDIA CASCADE;
ALTER TYPE MEDIA_TY ADD ATTRIBUTE IN_INT IN_INTERVENTO CASCADE;
CREATE TYPE COMMENTATO_POST AS TABLE OF REF COMMENTO_TY;
ALTER TYPE COMMENTO_TY ADD ATTRIBUTE A REF POST_TY CASCADE;
ALTER TYPE POST_TY ADD ATTRIBUTE COMMENTATO COMMENTATO_POST CASCADE;
CREATE TABLE AUTORE_TAB OF AUTORE_TY
NESTED TABLE SCRIVE STORE AS SCRIVE_NESTED;
CREATE TABLE INTERVENTO_TAB OF INTERVENTO_TY
NESTED TABLE DI STORE AS DI_NESTED
NESTED TABLE CONTIENE STORE AS CONTIENE_NESTED;
CREATE TABLE MEDIA_TAB OF MEDIA_TY
NESTED TABLE IN_INT STORE AS IN_INT_NESTED;
I'm trying to write an object procedure that creates a new instance of COMMENTO_TY, adds it to the COMMENTO_TAB table and the COMMENTATO nested table of POST_TY, but it gives me the following error:
CREATE OR REPLACE TYPE BODY Post_ty AS
MEMBER PROCEDURE addComm(commento VARCHAR2, ordine NUMBER, IdAut INTEGER) AS
NuovoCommento REF Commento_ty;
BEGIN
INSERT INTO Intervento_tab I
VALUES(Commento_ty(commento, SYSTIMESTAMP,
DI_AUTORE(
(SELECT REF(A) FROM AUTORE_TAB A WHERE IdAutore = IdAut)
),
CONTIENE_MEDIA(),
ordine,
(SELECT TREAT(REF(I) AS REF POST_TY)
FROM INTERVENTO_TAB I
WHERE VALUE(I) IS OF (POST_TY) AND
I.TIMESTMP = SELF.TIMESTMP)))
RETURNING REF(I) INTO NuovoCommento;
INSERT INTO TABLE(SELECT TREAT(VALUE(I) AS POST_TY).COMMENTATO
FROM INTERVENTO_TAB I
WHERE VALUE(I) IS OF (POST_TY)
AND I.TIMESTMP = SELF.TIMESTMP)
VALUES(NuovoCommento);
END addComm;
END;
/
PLS-00538: subprogram or cursor 'ADDCOM' is declared in an object type specification and must be defined in the object type body at line 3
PLS-00539: subprogram 'ADDCOMM' is declared in an object type body and must be defined in the object type specification at line 2
You have two errors:
PLS-00538: subprogram or cursor 'ADDCOM' is declared in an object type specification and must be defined in the object type body
and
PLS-00539: subprogram 'ADDCOMM' is declared in an object type body and must be defined in the object type specification
Notice how one is spelt ADDCOM and the other is ADDCOMM; you need to make sure the member procedure has the same name in the specification and the body.

'SET' must be a type

I created the following types :
CREATE TYPE Adress AS Object (Street varchar2(50), PostalC number, Ville varchar2(50));
CREATE TYPE PhoneNumber;
A person can have a set of PhoneNumbers, when I try to create the Type Person :
CREATE TYPE Person AS Object (FirstName varchar2(50), LastName varchar2(50), Adr Adress, Mobile SET(PhoneNumber));
I get the following error :
Errors: TYPE PERSON Line/Col: 0/0 PL/SQL: Compilation unit analysis
terminated Line/Col: 1/90 PLS-00488: 'SET' must be a type
If you want an array of phone numbers for each person, define the phone number type as a nested table:
create or replace type address as object (
street varchar2(50), postalc number, ville varchar2(50)
);
/
create or replace type phonenumber as table of varchar2(20);
/
create or replace type person as object (
firstname varchar2(50), lastname varchar2(50),
adr address, mobile phonenumber
);

How to overcome a persistent oracle 'invalid identifier' error on a basic insert?

I am trying to create a basic table using subtypes and insert some data into this in Oracle Express 11g.
My table is successfully created but i am having issues with inserting data.
The result of my insert statement always throws back an error 'SQL Error: ORA-00904: "BRANCH_PHONE": invalid identifier'.
The column which shows up in the error message is always the column which is at the end of the insert statement, despite the column existing in the table. I have tried the following code:
create type addressType as object(
street varchar2(20),
city varchar2(20),
postCode varchar2(8))
not final
/
create type branchType as object(
branchID int,
branch_address addressType,
branch_phone int(11))
not final
/
create table Branch of branchType(
constraint branch_pk primary key(branchID));
/
insert into Branch values (
branchID('2364'),
addressType('12 Rooster','Atlantis','A13 4UG'),
branch_phone('01316521311'));
I would really appreciate any ideas.
I made some changes, including changing the branch_phone to varchar2. A Phone number, while is "numbers" is not a data type of number. it is a string of characters. Also you were passing branchID as a string, but you are declaring it as a number, so changed that also. BranchID and branch_phone are primitive data types, so no constructor needed.
create type addressType as object(
street varchar2(20),
city varchar2(20),
postCode varchar2(8))
not final
/
create type branchType as object(
branchID int,
branch_address addressType,
branch_phone varchar2(11))
not final
/
create table Branch of branchType(
constraint branch_pk primary key(branchID));
/
insert into Branch values (
branchtype(2364,
addressType('12 Rooster','Atlantis','A13 4UG'),
'01316521311') )

Alter Object So That Spec Is Organized?

I'm using PL/SQL and I'd like to alter a type by adding an attribute. I need to alter as there are table dependencies that prevent replacing. When I use the following code.
create or replace type example_type as object
(
num_var number
);
alter type example_type add attribute (varchar2_var varchar2(10));
It creates a spec with that exact code.
Is there a way to make it so that the spec looks like
create or replace type example_type as object
(
num_var number,
varchar2_var varchar2(10)
);
Instead?

oracle11g sql: Warning: Type created with compilation errors

I'm trying to create a supertype customer service and subtype agent and supervisor, so they can inherent values however when I try to run this in oracle sql: a message comes up
Warning: Type created with compilation errors.
What is wrong with the code below?
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint supervisor_pk primary key(csID) );
You can use show errors in SQL*Plus or SQL Developer, or select * from user_errors, to see the error details.
Since you've shown six commands and the warning is about one of the first three (since it refers to type), and they appear OK independently apart from the constraint pointed put in comments, it looks like the whole script is being imterpreted as one command. It depends on your client settings, but you probably just need to seperate the commands with a / to cause them to execute. Becuase types can include PL/SQL the ; isn't treated as a statement seperator. So:
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
/
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
/
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
/
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint customer_service_pk primary key(csID) );