SQL invalid identifier - oracle

I checked some of the SQL invalid identifier solutions, but none of them helped me to fix my prob.
CREATE OR REPLACE PROCEDURE add_order(
p_cust_id cust_order.cust_id%TYPE,
p_order_id cust_order.order_id%TYPE,
p_order_date cust_order.order_date%TYPE,
p_sales_made_by cust_order.sales_made_by%TYPE,
p_qty cust_order.qty%TYPE,
p_product_id cust_order.product_id%TYPE,
p_cust_charged cust_order.cust_charged%TYPE,
p_company_price cust_order.company_price%TYPE,
p_shipper_id cust_order.shipper_id%TYPE)
IS
BEGIN
IF p_shipper_id = 'FEDEX' OR p_shipper_id = 'USPS' OR p_shipper_id = 'CUST' THEN
INSERT INTO productss (cust_id, order_id, order_date, sales_made_by, qty,
product_id, cust_charged, company_price, shipper_id)
VALUES (p_cust_id, p_order_id, p_order_date, p_sales_made_by, p_qty,
p_product_id, p_cust_charged, p_company_price, p_shipper_id);
ELSE
dbms_output.PUT_LINE('This shipper IS NOT allowed');
END IF;
END add_order;
/
The error is:
17/1 PL/SQL: SQL Statement ignored
17/116 PL/SQL: ORA-00904: "SHIPPER_ID": invalid identifier
This is the table:
CREATE TABLE CUST_ORDER
(
CUST_ID NUMBER(15),
ORDER_ID NUMBER(20),
ORDER_DATE DATE,
SALES_MADE_BY VARCHAR(40),
QTY NUMBER,
PRODUCT_ID NUMBER,
CUST_CHARGED NUMBER,
COMPANY_PRICE NUMBER,
SHIPPER_ID NUMBER,
PRIMARY KEY (ORDER_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUST_INFO(CUST_ID),
FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(PRODUCT_ID)
FOREIGN KEY (SHIPPER_ID) REFERENCES SHIPPING(SHIPPER_ID)
);
I guess that it is obvious mistake, but I was working too much.

You have an extra 'S' in the PRODUCTS table name:
INSERT INTO PRODUCTSS
^
And as #Alex Poole points out, you appear to be inserting into the wrong table anyway, given the naming of the proc.

Related

Why is Oracle giving me a missing right parentheses error when I am not missing a right parenthesis

I am trying to create a script in Oracle and keep getting an error saying I am missing the right parentheses - when I am not. please help.
create table driver
(
d_id INT(1),
d_name VARCHAR(45),
d_contact CHAR(10),
constraint driver_d_id_PK Primary Key(d_id)
);
ORA-00907: missing right parenthesis
create table limo
(
l_id INT(1),
l_callsign VARCHAR(45),
l_type VARCHAR(45),
constraint limo_l_id_PK Primary key(l_id)
);
ORA-00907: missing right parenthesis
create table clients
(
c_id INT(1),
c_name VARCHAR(45),
c_contact CHAR(10),
c_methpmt VARCHAR(25),
constraint clients_c_id_PK Primary key(c_id)
);
ORA-00907: missing right parenthesis
create table qualify
(
q_id INT(1),
q_l_id INT(1),
q_d_id INT(1),
constraint qualify_q_id_PK Primary key(q_id),
constraint qualify_q_l_id_FK Foreign key(q_l_id) references LIMO(l_id),
constraint qualify_q_d_id_FK Foreign key(q_d_id) references DRIVER(d_id)
);
ORA-00907: missing right parenthesis
create table rental
(
r_id INT(1),
r_date DATE,
r_fee INT(4),
r_c_id INT(1),
r_q_id INT(1),
constraint rental_r_id_PK Primary key(r_id),
constraint rental_r_c_id_FK Foreign key(r_c_id) references CLIENTS(c_id),
constraint rental_r_q_id_FK Foreign key(r_q_id) references QUALIFY(q_id)
);
ORA-00907: missing right parenthesis
The error is misleading. You have a parenthesis where it doesn't belong and Oracle gets confused. There must be no parentheses after INT.
If you want a numeric with a specific length, use NUMBER instead.
(And in Oracle we use VARCHAR2 instead of VARCHAR, and we don't use CHAR either.)
CREATE TABLE driver
(
d_id NUMBER(1),
d_name VARCHAR2(45),
d_contact VARCHAR2(10),
CONSTRAINT driver_d_id_PK PRIMARY KEY (d_id)
);
NUMBER(1) allows single digits only, i.e. numbers from -9 to +9.

Cannot insert NULL into, ERROR during execution of trigger

I have created a Trigger on table Customers so that every time a record is deleted from table customers this same record is inserted in table Customer_Archives with the current date as Deletion_Date.
I am have to insert a new customer into table Customers and then delete it. The record must be inserted correctly into table Customers_Archive.
Here's script I have so far:
CREATE TABLE Customer_Archives
(
customer_id NUMBER NOT NULL,
customer_first_name VARCHAR2(50),
customer_last_name VARCHAR2(50) NOT NULL,
customer_address VARCHAR2(255) NOT NULL,
customer_city VARCHAR2(50) NOT NULL,
customer_state CHAR(2) NOT NULL,
customer_zip VARCHAR2(20) NOT NULL,
customer_phone VARCHAR2(30) NOT NULL,
customer_fax VARCHAR2(30),
deletion_date DATE,
CONSTRAINT customer_archives_pk
PRIMARY KEY (customer_id)
);
CREATE OR REPLACE TRIGGER Customers_before_insert
BEFORE DELETE ON Customers
FOR EACH ROW
DECLARE
ar_row Customers%rowtype;
BEGIN
INSERT INTO Customer_Archives
VALUES(ar_row.Customer_id,
ar_row.Customer_First_Name,
ar_row.Customer_Last_Name,
ar_row.Customer_Address,
ar_row.Customer_City,
ar_row.Customer_State,
ar_row.Customer_Zip,
ar_row.Customer_Phone,
ar_row.Customer_Fax,
sysdate());
dbms_output.put_line('New row is added to Customers_Archive
Table with Customer_ID:' ||ar_row.Customer_id ||'on date:' || sysdate());
END;
/
SELECT trigger_name, status FROM user_triggers;
INSERT INTO CUSTOMERS
(customer_id, customer_first_name, customer_last_name, customer_address,
customer_city, customer_state, customer_zip, customer_phone, customer_fax)
VALUES (27,'Sofia','Chen','8888 Cowden St.','Philadelphia','PA',
'19149',7654321234',NULL);
DELETE FROM CUSTOMERS
WHERE customer_id = 27;
When I try to delete the customer that I just inserted I get an error:
Error starting at line : 47 in command -
DELETE FROM CUSTOMERS
WHERE customer_id = 27
Error report -
ORA-01400: cannot insert NULL into ("TUG81959"."CUSTOMER_ARCHIVES"."CUSTOMER_ID")
ORA-06512: at "TUG81959.CUSTOMERS_BEFORE_INSERT", line 4
ORA-04088: error during execution of trigger 'TUG81959.CUSTOMERS_BEFORE_INSERT'
In your DELETE trigger you should be using the :OLD values when creating your archive record:
CREATE OR REPLACE TRIGGER CUSTOMERS_BEFORE_INSERT
BEFORE DELETE ON CUSTOMERS
FOR EACH ROW
BEGIN
INSERT INTO CUSTOMER_ARCHIVES
(CUSTOMER_ID,
CUSTOMER_FIRST_NAME,
CUSTOMER_LAST_NAME,
CUSTOMER_ADDRESS,
CUSTOMER_CITY,
CUSTOMER_STATE,
CUSTOMER_ZIP,
CUSTOMER_PHONE,
CUSTOMER_FAX,
DELETION_DATE)
VALUES
(:OLD.CUSTOMER_ID,
:OLD.CUSTOMER_FIRST_NAME,
:OLD.CUSTOMER_LAST_NAME,
:OLD.CUSTOMER_ADDRESS,
:OLD.CUSTOMER_CITY,
:OLD.CUSTOMER_STATE,
:OLD.CUSTOMER_ZIP,
:OLD.CUSTOMER_PHONE,
:OLD.CUSTOMER_FAX,
SYSDATE());
DBMS_OUTPUT.PUT_LINE('New row is added to Customers_Archive
Table with Customer_ID:' ||:OLD.Customer_id ||'on date:' || SYSDATE());
END;
In your original trigger you'd declared a row variable named ar_row but hadn't assigned anything to any of the fields - therefore they were all NULL. When a BEFORE trigger is invoked during a DELETE, the :OLD values have the values prior to the deletion, and the :NEW values are all NULL.
Best of luck.

sql script not running

Here is a code snippet of a sql script which is giving me error,I have to generate a sequence on the primary_key of the table without using triggers in oracle:
CREATE SEQUENCE t1_seq START WITH 1 INCREMENT BY 1;
DROP TABLE CPR_SOURCE_SYSTEM_METADATA;
CREATE TABLE CPR_SOURCE_SYSTEM_METADATA
(
SYSTEM_ID NUMBER(4) NOT NULL t1_seq.nextval,
SYSTEM_NAME VARCHAR2(200),
DATE_FORMAT VARCHAR2(200),
CREATED_BY VARCHAR2(200),
MODIFIED_BY VARCHAR2(200),
CREATED_ON NUMBER(20),
MODIFIED_ON NUMBER(20),
IS_DELETED VARCHAR2(1),
CONSTRAINT "CPR_SOURCE_SYSTEM_PK" PRIMARY KEY ("SYSTEM_ID")
);
It is giving me the below error :
DROP TABLE CPR_SOURCE_SYSTEM_METADATA
* ERROR at line 1: ORA-00942: table or view does not exist
SYSTEM_ID NUMBER(4) NOT NULL t1_seq.nextval,
* ERROR at line 3: ORA-00907: missing right parenthesis
Not able to figure out the error,can anyone help??
SYSTEM_ID NUMBER(4) NOT NULL t1_seq.nextval,
The t1_seq.nextval segment is not valid - you cannot specify an auto-incrementing column like that.
The SQL parser is expecting to see:
SYSTEM_ID NUMBER(4) NOT NULL,
and throws the exception as the comma is not where it expects.
In Oracle 12c you can use an identity column but in earlier versions you will either need to:
Use the sequence in the SQL insert statement;
Use a trigger to insert the correct sequence value; or
Create a stored procedure to handle inserts and manage the sequence through that (disallowing direct inserts that could bypass this).

Trigger error: ORA-01403 no data found on insert

My database is that of a hotel reservation system. My trigger will change the availability status of a room before any new reservations are made based on whether the current date falls between the check_in and check_out values of a room.
I getting the error no data found when attempting to fire the trigger with an insert statement. I would appreciate any assistance with the problem.
Error:
SQL Error: ORA-01403: no data found
ORA-06512: at "USER.ROOM_STATUS", line 5
ORA-04088 error during execution of trigger 'USER.ROOM_STATUS' - no data found
*Cause: No data was found from the objects
Trigger:
create or replace trigger room_status
before insert on reservation
for each row
declare
status_in room.status%type;
error1 exception;
begin
select status into status_in
from room
where room.room_id = :old.room_id;
if sysdate between :old.check_in and :old.check_out then
update room
set status = 'F'
where room_id = :old.room_id;
else
update room
set status = 'T'
where room_id = :old.room_id;
end if;
exception
when error1 then
raise_application_error(-20100,'Insert Cancelled');
end;
Insert Statement:
insert into reservation
values(reservation_sequence.nextval,to_date('05-03-2017','mm/dd/yyyy'),
to_date('05-07-2017','mm/dd/yyyy'), 116, 170);
Tables:
create table roomType
(room_type varchar2(20) constraint roomType_pk primary key,
room_rate number(4));
create table room
(room_id number(3) constraint room_pk primary key,
room_type varchar2(15) constraint room_fk references roomType,
status char(1));
create table guest
(guest_id varchar2(5) constraint guest_pk primary key,
first_name varchar2(20),
last_name varchar2(20),
email varchar2(30),
phone varchar2(10));
create table reservation
(reservation_id number(6) constraint reservation_pk primary key,
check_in date,
check_out date,
room_id number(3),
guest_id varchar2(5),
foreign key (room_id) references room(room_id),
foreign key (guest_id) references guest(guest_id));
Oracle throws the NO_DATA_FOUND error when a SELECT statement finds no data. So, looking at your trigger, the obvious explanation might be there is no data in your ROOM table.
However, there is a subtle bug in your trigger code:
select status into status_in
from room
where room.room_id = :old.room_id;
The :old namespace references the value of the column before the DML is applied. But your DML statement is an insert so the values of :old.room_id is null because there is no prior record. Anything = null is always false so the select fails to find anything.
What you need to do is reference the :new value.
select status into status_in
from room
where room.room_id = :new.room_id;
:new applies to INSERT and UPDATE statements, :old applies to UPDATE and DELETE statements.
Incidentally you have defined an exception error1 but you don't raise it anywhere so that exception handler will never execute.

Integrity constraint violated - parent key not found when calling stored procedure

I'm trying to perform the stored package procedure call in Oracle 11g XE, but for some reason I get the error below:
Error report - ORA-02291: integrity constraint (ROOT.SYS_C007057)
violated - parent key not found ORA-06512: at "ROOT.BOOKS_STORE", line
69 ORA-06512: at line 2
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
when calling the following procedure:
begin
books_store.add_books_to_store(
'To Kill a Mockingbird', 21,
'test description', 5,
'https://test_img.jpg',
10, 6.99
);
end;
What the procedure does is insert the data into the books table. Below is the procedure text (which is inside the books_store package) and the description of the books table.
procedure add_books_to_store(
book_name books.name%type, book_author_id books.author_id%type,
book_description books.description%type default null,
book_publisher_id books.publisher_id%type, book_cover_img books.cover_img%type,
books_count books.available_count%type, book_price books.price%type)
is
existing_books_count integer;
add_negative_or_zero_books exception;
begin
if books_count <= 0 then
raise add_negative_or_zero_books;
end if;
select count(*) into existing_books_count from books
where
name = book_name and author_id = book_author_id and
description = book_description and publisher_id = book_publisher_id and
price = book_price;
if existing_books_count = 0 then
insert into books values (books_seq.nextval, book_name, book_description,
book_cover_img, books_count, book_author_id, book_publisher_id, book_price);
else
update books set available_count = available_count + books_count
where
name = book_name and author_id = book_author_id and
description = book_description and publisher_id = book_publisher_id and
price = book_price;
end if;
exception
when add_negative_or_zero_books then
raise_application_error(-10003, 'You cannot add 0 or less books');
end add_books_to_store;
books description:
DESC books;
Name Null? Type
------------------------------------------------------------
ID NOT NULL NUMBER(5)
NAME NOT NULL VARCHAR2(200)
DESCRIPTION VARCHAR2(2000)
COVER_IMG VARCHAR2(300)
AVAILABLE_COUNT NOT NULL NUMBER(4)
PRICE NUMBER(10,2)
AUTHOR_ID NUMBER(5)
PUBLISHER_ID NUMBER(5)
So, the error says that there's something wrong with my primary or foreign keys. Though, I cannot understand what exactly is wrong.
I thought that the problem was that I passed wrong author_id and publisher_id as arguments to the procedure, but they're correct. Here's the select * calls for authors and publishers tables:
select * from authors;
ID FIRST_NAME LAST_NAME BIRTHDAY
--------------------------------------------------
21 Harper Lee 28-APR-26
select * from publishers;
ID NAME
---------------------------
5 Penguin Fiction
Could you help me to find out what's wrong with my code and how to make it work?
PS: Here's my ER-diagram:
I think, problem could be here:
insert into books values (books_seq.nextval, book_name, book_description,
book_cover_img, books_count, book_author_id, book_publisher_id, book_price);
Because in table description columns have another order:
PRICE NUMBER(10,2)
AUTHOR_ID NUMBER(5)
PUBLISHER_ID NUMBER(5)
Try to specify column names explicitly:
insert into books (ID, NAME, DESCRIPTION, COVER_IMG, AVAILABLE_COUNT, PRICE, AUTHOR_ID, PUBLISHER_ID)
values (books_seq.nextval, book_name, book_description,
book_cover_img, books_count, book_price, book_author_id, book_publisher_id);
Now it looks like you are inserting AUTHOR_ID value into PRICE column, PUBLISHER_ID into AUTHOR_ID and PRICE into PUBLISHER_ID.

Resources