PLS-00103 encoutered the symbol - sql

I already tried to look for this type of error. I do have similar errors on others but different problems. Please help me with this, what is the good approach to this sample oracle query ?
create type Exams (
year char(4),
city varchar(20)
)
create table exams of type Exams
create OR REPLACE type Skills AS OBJECT(
type varchar(20),
ExamSet Exams multiset
);
/
create table skills of Skills;
Here is the error in script output :
LINE/COL ERROR
--------- -------------------------------------------------------------
1/12 PLS-00103: Encountered the symbol "(" when expecting one of the following: ; is authid as compress force compiled wrapped under
Errors: check compiler log

Try :
CREATE OR REPLACE TYPE ExamsType AS OBJECT (
year char(4),
city varchar(20)
);
/
create table Exams OF ExamsType;
Fixes :
create the type AS OBJECT ; this will give you an Abstract Data Type (ADT), that can actually hold attributes - see the oracle docs
you need a semi-colon and a slash at the end of the create type statement
the table cannot have the same name as the type
syntax for CREATE TABLE was wrong
To insert in this table, you would typically use the following statement :
insert into Exams values( ExamsType('2018', 'my adress') );

This will work:
CREATE TABLE new_table
AS (SELECT *
FROM old_table WHERE 1=2);
For your case it goes like this:
CREATE TABLE exams
AS (SELECT *
FROM Exams WHERE 1=2);
for 1=2 it does not select any of the rows but with as it copies the table definition.

Related

Oracle SQL3 3 nesting tables insert problem

I'm making an SQL3 script where I create types and then create tables and nested tables.
The prolem I'm getting happens when I want to insert a row where it says :
ORA-00932: inconsistent datatypes: expected UDT got CHAR
this is an online reproduction of the problem where the types and tables are already created, you can try to insert:
https://www.tutorialspoint.com/oracle_terminal_online.php?fbclid=IwAR0GgaLe2_GvGsEb80eB-D0uKDSDJDr1WNBPiK3mHQqpJQrtfacQ1cf03NA
the following is the types creation script
CREATE TYPE T_Personne ;
/
CREATE TYPE T_SET_Tag AS TABLE OF Varchar2(30);
/
CREATE TYPE T_Message AS OBJECT (
Texte Varchar2(500),
DateEcrit Date,
Tags T_SET_Tag
);
/
CREATE TYPE T_SET_Message AS TABLE OF T_Message;
/
CREATE TYPE T_Contact AS OBJECT(
Per REF T_Personne,
Depuis Date
);
/
CREATE TYPE T_SET_Contact AS TABLE OF T_Contact;
/
CREATE OR REPLACE TYPE T_Personne AS OBJECT (
Prenom Varchar2(30),
Suit T_SET_Contact,
Ecrit T_SET_Message
);
/
this one is the table creation script
CREATE TABLE TAB_Personne OF T_Personne
NESTED TABLE Suit STORE AS TAB_suit,
NESTED TABLE Ecrit STORE AS TAB_ecrit(
NESTED TABLE Tags STORE AS TAB_Tags
);
and finally the script I'm using to insert my new rows
INSERT INTO TAB_Personne VALUES(
'Baam',
T_SET_Contact(),
T_SET_Message()
);
INSERT INTO TAB_Personne VALUES(
'Rachel',
T_SET_Contact(
(SELECT REF(P)
FROM TAB_Personne P
WHERE P.Prenom='Baam'),
to_date('01/01/2018', 'dd/mm/yyyy')
),
T_SET_Message(
'Paris candidat aux jeux Olympiques 2022',
to_date('01/06/2019', 'dd/mm/yyyy'),
T_SET_Tag('JM2022')
)
);
the error message I'm getting is
1 row created.
'Paris candidat aux jeux Olympiques 2022', *
ERROR at line 10:
ORA-00932: inconsistent datatypes: expected UDT got CHAR
I'd be glad if anyone can guide me through this, thanks.
You are specifying a T_SET_Message collection but you then need T_Message objects within that; you're supplying the attributes of that object type, not an actual object of the type. The first element in the (non-)collection is a string, hence the error you get - you've supplied a string ('Paris...') when it's expecting to see a UDT (T_Message('Paris...', ...)).
You are also doing the same thing with the T_SET_Contact collection.
You need to wrap your current attributes in object constructors; so this works:
INSERT INTO TAB_Personne VALUES(
'Rachel',
T_SET_Contact(
T_Contact(
(SELECT REF(P)
FROM TAB_Personne P
WHERE P.Prenom='Baam'),
to_date('01/01/2018', 'dd/mm/yyyy')
)
),
T_SET_Message(
T_Message(
'Paris candidat aux jeux Olympiques 2022',
to_date('01/06/2019', 'dd/mm/yyyy'),
T_SET_Tag('JM2022')
)
)
);
db<>fiddle

Circular Dependency -Table Insertion Error

I want to insert data into table but got below error..Can any one help me..
CREATE OR REPLACE TYPE TEST_TYP FORCE IS OBJECT
("id" VARCHAR(5000 NULL)
NOT FINAL;
CREATE OR REPLACE TYPE TEST_TAB is table of REF TEST_TYP;
CREATE OR REPLACE TYPE TEST1_TYP FORCE IS OBJECT
("id" VARCHAR2(500) NULL,
"extension" "TEST_TAB" NULL )
NOT FINAL;
CREATE TABLE "TEST_OBJ_TABLE" OF "TEST1_TYP"
NESTED TABLE "extension" STORE AS "Allin"
When I try to insert using this statementL
insert into "TEST_OBJ_TABLE" ("id","extension")
VALUES(
'0FE71A85',
"TEST_TAB"("TEST_TYP"( '0FE71A8'))
);
It throws this error
Error at Command Line : 59 Column : 12
Error report -
SQL Error: ORA-00932: inconsistent datatypes: expected REF SUB_HWOW.TEST_TYP got SUB_HWOW.TEST_TYP
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*Cause:
*Action:
You have to modify object TEST_TYP -> varchar2 has to be max 4000.
You have to store this object in db.
Create object table for TEST_TYP
create table t_for_test_type of TEST_TYP;
To obtain object reference, the object have to be save in db table. In your case:
declare
v_ref_to_test_type1 ref TEST_TYP;
begin
insert into t_for_test_type t values('abcd1') return ref(t) into v_ref_to_test_type1;
end:
Join all parts together.
declare
v_ref_to_test_type1 ref TEST_TYP;
v_ref_to_test_type2 ref TEST_TYP;
begin
insert into t_for_test_type t values('abcd1') return ref(t) into v_ref_to_test_type1;
insert into t_for_test_type t values('abcd2') return ref(t) into v_ref_to_test_type2;
insert into TEST_OBJ_TABLE values (TEST1_TYP('abcd',new TEST_TAB(v_ref_to_test_type1,v_ref_to_test_type2)));
end;
Query table.
select t."id",x.column_value from TEST_OBJ_TABLE t, table(t."extension") x it returns id + ref to other object
Viewing the referenced object.
select t."id",deref(x.column_value) from TEST_OBJ_TABLE t, table(t."extension") x
Note1. You should avoid declaring attribute using double quote. Db becomes case sensitive and it's not normal situation :)
Note2. I don't know why code formatting is not working today
Question. Why are you trying to use such complicated construction?
Changed the original answer. The quotes are doing you no favors - nor was calling your column names id. Removed the REF reference.
CREATE OR REPLACE TYPE TEST_TYP FORCE IS OBJECT
(id1 VARCHAR(4000) NULL)
NOT FINAL;
CREATE OR REPLACE TYPE TEST_TAB is table of TEST_TYP;
CREATE OR REPLACE TYPE TEST1_TYP FORCE IS OBJECT
(id2 VARCHAR2(500) NULL,
extension TEST_TAB NULL )
NOT FINAL;
CREATE TABLE TEST_OBJ_TABLE OF TEST1_TYP
NESTED TABLE extension STORE AS Allin ;
insert into TEST_OBJ_TABLE ( ID2, EXTENSION)
VALUES(
'0FE71A85',
TEST_TAB(TEST_TYP( '0FE71A8'))
);
SELECT * FROM TEST_OBJ_TABLE ;

How to use in statement with nested table

Hey there I have a function, and part of the function is to make sure that the selected value is within the passed in table of varchar2s. To start I declare a varchar2 table type like so.
create or replace type Varchar2Table is table of varchar2(200)
Then I have the function which accepts the nested table parameter and has a select statement on them.
function SelectPeople(inputNames Varchar2Table) return People
begin
--stuff
select * from person_table where name in inputNames; --line of interest
--more stuff
end;
This doesn't seem to work though, I get the following error:
ORA-00932: inconsistent datatypes: expected NUMBER got
ENGSPL5.VARCHAR2TABLE
Any suggestions?
The TABLE operator allows nested tables to be used in SQL statements. The function was also missing an IS and an INTO.
create or replace type Varchar2Table is table of varchar2(200);
create table person_table(id number, name varchar2(100));
create or replace function SelectPeople(inputNames Varchar2Table) return number
is --Missing "IS".
type numberTable is table of number; --Need a collection to store results.
numbers numberTable;
begin
select id
bulk collect into numbers --Missing "INTO".
from person_table
where name in (select column_value from table(inputNames)); --Missing "TABLE".
--Alternatively a multiset condition can be used.
--where name member of inputNames;
--Dummy return value to make the function compile.
return 1;
end;
/

Reserved keyword in Oracle database trigger?

When I try to create a trigger in Oracle 11g, I get the following errors:
ORA-06552: PL/SQL: COMPILATION UNIT ANALYSIS TERMINATED
ORA-06553: PLS-320: THE DECLARATION OF THIS TYPE OF THIS EXPRESSION IS INCOMPLETE OR MALFORMED.
I've tried changing the table name from EVENT to another table and the trigger compiles but my search for reserved keywords only indicates EVENTS though. Is something else wrong with the trigger?
CREATE TRIGGER GEN_EVENT_ID
BEFORE INSERT ON EVENT
FOR EACH ROW
BEGIN
:NEW.ID := EVENT_ID_SEQ.NEXTVAL;
END;
Table
CREATE TABLE EVENT (
ID NUMBER(19,0) NOT NULL,
TIMESTAMP TIMESTAMP NOT NULL,
NAME VARCHAR2(255)
);
Id appears in the v$RESERVED_WORDS view, which is the view of SQL keywords. Check this link for more information on this.
Select *
From V$RESERVED_WORDS
Where KEYWORD = 'ID'
While Id is not marked as Reserved in this view, there seems to be some problem with using column names that appear in that list for triggers as stated in this other SO question.
If you read it you will see the OP had the same problem but with timestamp. In the other SO question a workaround was suggested that worked for the OP. If you change the column name the problem should disappear.
Update
Seeing your table declaration, all of your column names appear in the V$RESERVED_WORDS. Particularly the timestamp column is the same one mentioned in the other SO question (see link above) as being problematic for triggers.
My suggestion is to rename both the table and its columns. For example:
Create Table my_EVENT (
event_Id Number(19,0) Not Null,
event_Timestamp Timestamp Not Null,
event_Name Varchar2(255)
);

Creating a Package with a Procedure

I'm trying to create a procedure where the input is lname from table Employee and output is sal and average from table Information. Both outputs are numbers and average is a decimal number. This is what I have so far.
CREATE OR REPLACE PACKAGE getSalAvgPack IS PROCEDURE getSalAvg
(name IN Employee.lname%TYPE,
pSal OUT NUMBER,
pAvg OUT NUMBER);
END;
.
/
The package compiles fine.
CREATE OR REPLACE PACKAGE BODY getSalAvgPack IS PROCEDURE getSalAvg
(name IN Employee.lname%TYPE,
pSal OUT NUMBER,
pAvg OUT NUMBER)
IS
BEGIN
SELECT Information.sal, Information.average
INTO pSal, pAvg
FROM Information
WHERE Information.eid=Employee.eid AND name=lname;
END;
END;
.
/
When I try to compile the package body I get these errors
PL/SQL: SQL Statement ignored (for line 7, column 2)
PL/SQL: ORA-00904: "LNAME": invalid identifier
You need to join in the Employee table:
SELECT Information.sal, Information.average
INTO pSal, pAvg
FROM Information join
Employee
on Information.eid=Employee.eid
WHERE name=lname;
It appears that you are missing a join
SELECT Information.sal, Information.average
INTO pSal, pAvg
FROM Information
JOIN Employee ON (Information.eid = Employee.eid)
WHERE name = Employee.lname;
As a general principle, I would strongly suggest using a naming convention for your parameters that differentiates them from columns in your tables. If name is a column in either table that you're joining, your predicate will compare the Employee.lname against that column rather than using the name parameter from your procedure. A common convention is to prefix all parameters with p_ so that your query becomes
SELECT Information.sal, Information.average
INTO p_Sal, p_Avg
FROM Information
JOIN Employee ON (Information.eid = Employee.eid)
WHERE p_name = Employee.lname;