PL SQL UPDATE STATEMENT - sql

create table item_stock
( itemcode varchar2(11) not null,
name varchar(27) not null,
des varchar2(30),
pcs number(11),
unit varchar(7),
qty number(11),
stkdate date,
constraint itemcodef_pk primary key(itemcode, name)
);
BEGIN
update table item_stock
set 'item_stock.itemcode' = 'item_stock.itemcode';
END;
Error 103 at line 2, column 14
Encountered the sysmbol "ITEM_STOCK" when expecting one of the following.
Can anybody help me to resolve this error.

This one:
Update item_stock A
Set A.itemcode = item_stock.itemcode;
Note:
The data type of the item_stock.itemcode must be same as A.itemcode.

Related

SQL Stored Procedure Invalid Identifier

I'm getting ORA-00904 Invalid Identifier for several variables in this stored procedure when trying to compile it in oracle but cannot figure out why; All working storage events are declared at the DECLARE block of the code, and the variables from the schema are all properly defined too.
Has anyone had this error and/or knows how to fix it?
This is the DDL for the schema im working with
CREATE TABLE history
("history_id" number(4) primary key,
"history_dt" date not null,
"history_time" timestamp not null,
"booking_cost" varchar2(50) not null,
"booking_status" varchar2(50) not null
);
CREATE TABLE attendees
("attendee_id" number(8) primary key,
"attendee_name" varchar2(50) not null,
"attendee_class" number(4) not null,
"attendee_school" varchar2(50) not null,
"attendee_status" varchar2(50) not null
);
CREATE TABLE event
("event_id" number(10) primary key,
"event_name" varchar2(100) not null,
"event_location" varchar2(100) not null,
"event_size" number(4) not null,
"start_dt" date not null,
"end_dt" date not null,
"class_restriction" number(4) not null,
"school_restriction" varchar2(100) not null
);
CREATE TABLE reservation
("reservation_id" number(3) primary key,
"event" number(10) references event("event_id"),
"attendee" number(8) references attendees("attendee_id"),
"booking" number(4) references history("history_id"),
"reservation_status" varchar2(50) not null
);
These are the error messages I'm getting
Compilation failed,line 19 (15:38:10)
PL/SQL: ORA-00904: "END_DT": invalid identifierCompilation failed,line 18 (15:38:10)
PL/SQL: SQL Statement ignoredCompilation failed,line 26 (15:38:10)
PLS-00302: component 'EVENT_ID' must be declaredCompilation failed,line 26 (15:38:10)
PL/SQL: ORA-00904: "RESERVATION"."EVENT_ID": invalid identifierCompilation failed,line 26 (15:38:10)
PL/SQL: SQL Statement ignoredCompilation failed,line 36 (15:38:10)
PL/SQL: ORA-00904: "EVENT_ID": invalid identifierCompilation failed,line 35 (15:38:10)
PL/SQL: Statement ignoredCompilation failed,line 51 (15:38:10)
PL/SQL: ORA-00947: not enough valuesCompilation failed,line 51 (15:38:10)
create or replace procedure Event_Planning
(arg_event_id in number, arg_student_id in number)
IS
ws_event_name varchar(100);
ws_capacity number;
ws_event_school varchar2(4);
ws_event_class number;
past_event exception;
capacity exception;
school exception;
class exception;
BEGIN
--Test for active event
select max(event_name) into ws_event_name from event
where event_id = arg_event_id and end_dt > SYSDATE;
if ws_event_name is null
then raise past_event;
end if;
--Test for capacity
select max(event_capacity) into ws_capacity from event JOIN reservation ON event.event_id = reservation.event_id
where event_id = arg_event
and event_capacity > reservation_size;
if ws_capacity is null
then raise capacity;
end if;
--Test for restricted school
select max(school_restriction) into ws_event_school from event
where event_id = arg_event_id;
if ws_event_school = arg_school
then raise school;
end if;
--Test for restricted class
select max(class_restriction) into ws_event_class from event
where event.id = arg_event;
if ws_event_class = arg_class
then raise class;
end if;
--Update reservation table
insert into reservation values
(Seq.nextval, arg_event, arg_student);
update reservation
set reservation_size = reservation_size + 1;
--Exceptions
Exception
when past_event
then raise_application_error(-20001, 'Event has passed');
when capacity
then raise_application_error(-20002, 'Event at capacity');
when school
then raise_application_error(-20003, 'Invalid school');
when class
then raise_application_error(-20004, 'Invalid class');
END;
To make it more likely to have your questions answered, share everything.
That includes, the actual error messages. And if you REALLY want to be nice, include the TABLE DDL (and even some data) for your EVENT and RESERVATION tables.
I was too 'lazy' to guess what yours looked like, and just changed your queries to dummy tables to replicate the issue.
You haven't declared
ws_school
arg_school
ws_class
arg_class
When you compile, the compiler will return the line number and curpos of your issue. You're referring to things the db doesn't know about.
Recommendations
Don't hard code the data type definitions for your variables. Because, tables CAN and WILL change.
Instead, make them dynamic.
So, instead of saying
WS_SCHOOL VARCHAR2(4);
do something like
WS_SCHOOL TABLE.COLUMN%TYPE;
Then when your tables change, your code won't necessarily break.
By default, Oracle identifiers such as table and column names are case-insensitive, so for example you can
select dummy, DUMMY, Dummy
from dual;
However, double-quoting is also available to allow you to override the standard rules if you really need to:
create table demo("123/Wow!" number);
SQL> desc demo
Name Null? Type
----------------------------------------- -------- ----------------------------
123/Wow! NUMBER
Your tables history, attendees, event etc have case-sensitive, lowercase column names due to the double quoting, so you have to follow the same convention whenever you use them:
SQL> select count(event_id) from event;
select count(event_id) from event
*
ERROR at line 1:
ORA-00904: "EVENT_ID": invalid identifier
SQL> select count("event_id") from event;
COUNT("EVENT_ID")
-----------------
0
1 row selected.
Unless there is some important reason to do this, it would be simplest to recreate the tables without double-quoted column names.
(Also, reservation has an "event" column, not "event_id". There may be other typos like this - I haven't checked the whole thing.)
That doesn't need to use end_date on filters, because event_id is a primary key.
select "event_name" into ws_event_name
from event
where "event_id" = arg_event_id;
That looks so confuse to know, where the following columns come from: ---=^^^=---
-- event_capacity
-- reservation_size
select max("event_size") into ws_capacity
from event
join reservation
on event."event_id" = reservation."event"
where "event_id" = arg_event_id
and "event_size" > count("reservation_id");
Syntax error for event.id, it must event_id
select "class_restriction" into ws_event_class
from event
where "event_id" = arg_event_id;
Insert into reservation table:
select count(*) into reserve_count
from reservation
where "event" = arg_event_id
and "attendee" = arg_studen_id;
if reserve_count = 0
then
insert into reservation values
(Seq.nextval, arg_event_id, arg_student_id, null, "R");
end if;
--- note: that needs to declare reserve_count
Use count() to populate attendees, then no need to update reservation table.
-- update reservation
-- set reservation_size = reservation_size + 1;
reservation table:
CREATE TABLE reservation
("reservation_id" number(13) primary key,
"event" number(10) references event("event_id"),
"attendee" number(8) references attendees("attendee_id"),
"booking" number(10) references history("history_id"),
"reservation_status" varchar(1) not null
);
To merge as a single query:
select count(*), "event_name", "class_restriction"
into event_count, ws_event_name, ws_event_class
from event
where "event_id" = arg_event_id;
-- note: that needs to declare event_count

Column not allowed here pl sql

i am trying to create a procedure to add a new sale, but for some reason i am getting an error that says the column not allowed here
Here is the code:
CREATE OR REPLACE PROCEDURE AddSale
(
In_CID customers.CID%Type,
In_EmpID Employees.EmpID%Type,
In_car_num cars.car_num%Type,
In_Pay_M Sales.Pay_method%Type,
In_Payment_duration Sales.Pay_duration%Type,
In_Payment_type Sales.S_type%Type,
In_sh_id Sales.sh_id%Type,
)
IS
number := 0;
Id_sale number;
BEGIN
INSERT INTO Sales(Sales_ID, CID, EmpID, car_num, S_time,
S_type, Pay_method, Pay_duration, sh_id)
VALUES (Sales_ID_Seq.nextval, In_CID, In_EmpID, In_car_num, SYSDATE,
In_Payment_type, In_Payment_M, In_Payment_duration, In_sh_id);
And here is the table definition:
create table Sales
(
Pay_duration number(15),
Car_num number(15),
Sales_ID number(15),
S_type varchar2(15),
CID number(15),
Pay_method varchar2(32),
S_time timestamp,
EmpID number(15),
Sh_id number(15),
discount number(5,3),
Disc_status varchar2(20),
ApprovedBy number(15)
);
I've formatted the code in your request. You will see that proper formatting is a great help when coding.
In your declaration you have:
In_Pay_M
In your insert statement you have:
In_Payment_M
Then, there is a comma too many after your last parameter:
In_sh_id Sales.sh_id%Type,
)

Updating Trigger Table Values Oracle SQL

I am not able to update a table after the following trigger added. Here is what I have so far:
CREATE TABLE ITEM(
item_id DECIMAL(10) NOT NULL,
description VARCHAR(30),
price DECIMAL(10),
PRIMARY KEY (item_id));
CREATE TABLE Item_price_history (
history_id DECIMAL(10) NOT NULL,
item_id DECIMAL(10) NOT NULL,
line_price DECIMAL(10,2),
new_line_price DECIMAL(10,2),
modified DATE,
PRIMARY KEY (HISTORY_ID),
FOREIGN KEY (ITEM_ID) REFERENCES item);
CREATE OR REPLACE TRIGGER Item_price_history
AFTER UPDATE OR INSERT ON ITEM
FOR EACH ROW
BEGIN
INSERT INTO item_price_history(item_id, line_price,
new_line_price,modified)
select item_id,:OLD.price,:New.price, SYSTIMESTAMP from item;
END;
Now if I use the following Update commands, I get this error:
UPDATE item_price_history
SET line_price = 4
WHERE ITEM_ID=ITEM.ITEM_ID and ITEM.DESCRIPTION='Spoon';
*Error report -
SQL Error: ORA-00904: "ITEM"."DESCRIPTION": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
Action:
Any help or suggestions or advice?
Could you please try this:
UPDATE item_price_history
SET line_price = 4
WHERE item_id IN (SELECT item_id
FROM ITEM
WHERE description = 'Spoon');
You can try the below Merge as well. This will not update anything as there are no records in item table with Spoon Description and item_id.
Not sure what you wanted to achieve. I see you created a trigger. The trigger fires only when there is an insert/update on item not on item_price_history.
merge INTO item_price_history tgt USING
(SELECT item_id, description FROM item
) src ON (tgt.item_id = src.item_id AND src.description = 'Spoon')
WHEN matched THEN
UPDATE SET line_price = 4;
--Updates zero records
Try this...
update ITEM_PRICE_HISTORY SET line_price = 4
WHERE item_id IN (SELECT i.item_id FROM ITEM WHERE i.description = 'Spoon');

ORA-00955 during SQL Table creation

I have been getting the error during a table creation. I know it means that the table name needs to change, but I don't see any object with the same name. A copy of the .lst is below.
Thanks
SQL> CREATE TABLE CUSTOMERtable
2 (
3 CUSTOMERID INT NOT NULL,
4 CUSTNAME VARCHAR2 (50) NOT NULL,
5 ADDRESS VARCHAR2 (100) NOT NULL,
6 PHONENUMBER VARCHAR2 (10) NOT NULL,
7 CONSTRAINT IDS_CUST_PK PRIMARY KEY (CUSTOMERID)
8 );
CREATE TABLE CUSTOMERtable
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL>
SQL> CREATE TABLE RENTALStable
2 (
3 RENTALID INT NOT NULL,
4 OUTDATE DATE NOT NULL,
5 INDATE DATE NOT NULL,
6 LATEFEE INT,
7 DAMAGEFEE INT,
8 REWINDFEE INT,
9 ID_CUSTOMER INT,
10 CONSTRAINT RentalsTable_IDS_PK PRIMARY KEY (RENTALID),
11 FOREIGN KEY (ID_CUSTOMER) REFERENCES CUSTOMERtable(CUSTOMERID)
12 );
Table created.
This should find the object that's creating the problem:
select *
from user_objects
where object_name = 'CUSTOMERTABLE'
Notice that your statement, even if you write CUSTOMERtable ( upper and lower case), will try to create a table named CUSTOMERTABLE (upper case).
If you want to keep two objects with the same names but different case (and it seems not a good idea to me) you should use double quotes:
CREATE TABLE "CUSTOMERtable" ( ...

Error while altering table in SQL Server

I'm just trying to add 2 columns to customer table
alter table CUSTOMER ADD ( CUSTOMERNAME VARCHAR(255) NULL
, CUSTOMERDESC VARCHAR(30) NULL )
but I get the following error when I try to run the script,
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('.
Can someone tell me what is the mistake that I'm doing. Thanks in anticipation
You don't need the brackets for an alter statement so remove them. ie:
alter table CUSTOMER ADD CUSTOMERNAME VARCHAR(255) NULL,
CUSTOMERDESC VARCHAR(30) NULL
You don't need any ( before the column names - just use:
ALTER TABLE dbo.Customer
ADD CustomerName VARCHAR(255) NULL, CustomerDesc VARCHAR(30) NULL
Does that work?