1) Create a procedure (PrintProc) that prints out "This is the Final Test".
2) Create a procedure (UpdateProc) that takes a 'student Id' and then updates his State to ‘New York’.
Pic student database
Pic of faculty database
For #1 is it
CREATE OR REPLACE
PROCEDURE PrintProc IS
BEGIN
DBMS_OUTPUT.PUT_LINE(’This is the Final Test’);
END;
To execute I did
begin
PrintProc;
end;
but I got an error
For #2 is it
CREATE OR REPLACE PROCEDURE UpdateProc
AS
BEGIN
Update Student
set s_state = 'New York'
where s_state = 'WI'
END;
and got this error Error at line 6: PL/SQL: ORA-00933: SQL command not properly ended
to execute it is it
Begin
UpdateProc;
end;
6) Create a procedure (PrintStudentsProc) that prints out a list of students who have been taught by Kim Cox.
CREATE OR REPLACE PROCEDURE PrintStudentsProc
AS
BEGIN
Select S.S_ID, F.F_ID
FROM Faculty F INNER JOIN STUDENT S ON F.F_ID = S.F_ID
WHERE F.F_ID = 1
END;
and I get this error Error at line 4: PLS-00428: an INTO clause is expected in this SELECT statement
Was about to make another topic about triggers but stackoverflow bugged out again and I can't post for -9 days cause I posted 6 recent(not even recent) questions.
5) Create a trigger (UpdateTrigger) that outputs a message saying "Student record is going to be updated” Before the Update Takes place on Student Table,
I did
CREATE OR REPLACE TRIGGER UpdateTriggers
BEFORE UPDATE ON StudentsInfo
BEGIN
DBMS_OUTPUT.PUT_LINE('Student record is going to be updated');
Update StudentsInfo Set StudentsUpdated = StudentsUpdated + 1;
End;
and how would I execute it?
begin
UpdateTriggers;
end;
1. Error in PrintProc
The error in following code:
begin
PrintProc;
end;
is caused by the invalid character ’ in DBMS_OUTPUT.PUT_LINE in PrintProc. Replace ’ with ', this should resolve that error.
DBMS_OUTPUT.PUT_LINE('This is the Final Test');
2. Error in UpdateProc
The error in UpdateProc is caused by missing semi-colon in update statement. Add semicolon like the following:
Update Student
set s_state = 'New York'
where s_state = 'WI';
3. Error in PrintStudentsProc
Regarding the error in PrintStudentsProc, you can't do plain select statement inside plsql block. You need to use cursor. Also, you are missing semi-colon in the following query:
Select S.S_ID, F.F_ID
FROM Faculty F INNER JOIN STUDENT S ON F.F_ID = S.F_ID
WHERE F.F_ID = 1;
Cursor for loop example:
CREATE OR REPLACE PROCEDURE PrintStudentsProc AS
BEGIN
FOR stud_rec IN (
Select S.S_ID SID, F.F_ID FID
FROM Faculty F INNER JOIN STUDENT S ON F.F_ID = S.F_ID
WHERE F.F_ID = 1)
LOOP
DBMS_OUTPUT.PUT_LINE(stud_rec.SID||', '||stud_rec.FID);
END LOOP;
END;
/
Have a look at other types of cursors here
4. Error in Trigger
For trigger, after creating the trigger you need to execute the statement which invokes the trigger. In your case it is before update, so you need to perform a update query on the table.
CREATE OR REPLACE TRIGGER update_trigger BEFORE
update ON StudentsInfo FOR EACH ROW
DECLARE
stud_updated int;
BEGIN
DBMS_OUTPUT.PUT_LINE('Student record is going to be updated');
select StudentsUpdated into stud_updated from StudentsInfo where s_id=:new.s_sid;
--increase the value
stud_updated := stud_updated+1;
Update StudentsInfo Set StudentsUpdated = :stud_updated;
DBMS_OUTPUT.PUT_LINE('Student updated count:'||stud_updated);
END;
/
The above trigger should be able to execute, whenever you perform an update on the StudentsInfo table.
Also, check this sqlfiddle
Related
can anyone help me solve this error
CREATE OR REPLACE TRIGGER INVALID_COURSE
BEFORE INSERT OR UPDATE ON MARKS
FOR EACH ROW BEGIN
IF :NEW.C_ID NOT IN (SELECT C_ID FROM COURSE WHERE B_ID =(SELECT B_ID FROM EMPLOYEE WHERE EMP_ID= :NEW.EMP_ID)) THEN RAISE_APPLICATION_ERROR(-20001, 'INVALID COURSE FOR THE BATCH');
END IF;
END;
Check it first, raise if necessary. Something like this:
CREATE OR REPLACE TRIGGER invalid_course
BEFORE INSERT OR UPDATE
ON marks
FOR EACH ROW
DECLARE
l_cnt NUMBER;
BEGIN
SELECT COUNT (*)
INTO l_cnt
FROM course c JOIN employee e ON e.b_id = c.b_id
WHERE e.emp_id = :new.emp_id
AND c.c_id = :new.c_id;
IF l_cnt = 0
THEN
raise_application_error (-20001, 'INVALID COURSE FOR THE BATCH');
END IF;
END;
You are mixing SQL and PL/SQL. They are different engines with different syntax and vocabulary, though there are similarities and overlaps.
IF is a PL/SQL command, and it does not support an embedded SQL statement as you are trying to do here. You need to separate your SQL from your PL/SQL. Fetch the SQL result into a variable and then use PL/SQL constructs to make decisions based on the contents of that variable, as Littlefoot's answer demonstrates.
This question already has answers here:
syntax Error in PostgreSQL when I try to create Trigger
(2 answers)
Closed last year.
I am getting a syntax error for my code which I can't understand why
am I missing something?
also, I read this I did not get my answer
syntax Error in PostgreSQL when I try to create Trigger
CREATE TRIGGER MyExampleName AFTER INSERT ON baskets
FOR EACH ROW BEGIN
UPDATE customers
SET customers.credit=customers.credit - NEW.amount
WHERE customers.id = NEW.customer_id;
END;
and tried it like this as well:
CREATE TRIGGER MyExampleName AFTER INSERT ON baskets
FOR EACH ROW AS $$ BEGIN
UPDATE customers
SET customers.credit=customers.credit - NEW.amount
WHERE customers.id = NEW.customer_id;
END;
$$ LANGUAGE plpgsql;
Error:
ERROR: syntax error at or near "BEGIN"
LINE 2: FOR EACH ROW BEGIN
^
SQL state: 42601
Character: 67
I'd say the first comment on your question pretty much covers it all. You cannot put the trigger code in the trigger body, you must first create a separate function and include the function call inside the trigger body.
This example comes directly from the Postgres docs:
-- 1. Create the function that does what you need
CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
BEGIN
-- Check that empname and salary are given
IF NEW.empname IS NULL THEN
RAISE EXCEPTION 'empname cannot be null';
END IF;
IF NEW.salary IS NULL THEN
RAISE EXCEPTION '% cannot have null salary', NEW.empname;
END IF;
-- Who works for us when they must pay for it?
IF NEW.salary < 0 THEN
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
END IF;
-- Remember who changed the payroll when
NEW.last_date := current_timestamp;
NEW.last_user := current_user;
RETURN NEW;
END;
$emp_stamp$ LANGUAGE plpgsql;
-- 2. Create the trigger with the 'EXECUTE FUNCTION function_name' part
-- replacing the actual function name from step 1.
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
FOR EACH ROW EXECUTE FUNCTION emp_stamp();
I’m trying to create a stored procedure in Oracle apex that execute 4 other procedures that work by themselves alone, but it looks like I'm doing something wrong. And I don't really know what to change, could someone please help me?
create or replace procedure "UPDATE_COST"
is
begin
UPDATE orders
SET order_price = (
SELECT sum(total)
FROM detail_order
where detail_order.nord=orders.nord
group by nord
);
end;
create or replace procedure "Detail_quantity_proc"
is
begin
update detail_order d set
d.total = (select d.quantity * p.price_exclvat
from product p
where p.npro = d.npro
)
where exists (select null from product a
where a.npro = d.npro
);
end;
create or replace procedure "Proc_excl_vat"
is
begin
UPDATE bill
SET PRICE_EXCL_VAT = (
SELECT ORDER_PRICE
FROM orders
where orders.nord=bill.nord
);
end;
create or replace procedure "Proc_INCL"
is
begin
UPDATE BILL SET PRICE_INCL_VAT = PRICE_EXCL_VAT*1.06;
end;
create or replace procedure "Fill_all_cost"
is
begin
execute UPDATE_COST;
execute Detail_quantity_proc;
execute Proc_excl_vat;
execute Proc_INCL;
end;
You don't need the execute keyword. Also, you have those defined with double quotes, so they are case sensitive. Redefine them without the quotes:
create or replace procedure fill_all_costs
is
begin
update_cost;
detail_quantity_proc;
proc_excl_vat;
proc_incl;
end;
I have the following procedure,
CREATE OR REPLACE PROCEDURE Add_Shipment_Method(
shipment_method_id_p IN NUMBER,
shipment_description_p IN VARCHAR2)
AS
BEGIN
INSERT INTO shipment_method
SELECT shipment_method_id_p, shipment_description_p
FROM dual
WHERE NOT EXISTS (SELECT * FROM SHIPMENT_METHOD WHERE SHIPMENT_METHOD_ID = shipment_method_id_p AND SHIPMENT_DESCRIPTION = shipment_description_p);
COMMIT;
EXCEPTION WHEN
OTHERS THEN
ROLLBACK;
END;
/
The procedure generally allows adding a new shipment method into the table shipment_method when the row doesn't exist.
I want to modify it and show a message box when the primary key 'shipment_method_id' does exist, and allows to write the row when it doesn't exist.
Any suggestions?
You can not show MessageBoxes from a Stored Proc.
Now for checking if the INSERT has inserted any row and show message in output window.
Use
BEGIN
INSERT INTO shipment_method
SELECT shipment_method_id_p, shipment_description_p
FROM dual
WHERE NOT EXISTS (SELECT * FROM SHIPMENT_METHOD
WHERE SHIPMENT_METHOD_ID = shipment_method_id_p
AND SHIPMENT_DESCRIPTION = shipment_description_p);
IF SQL%ROWCOUNT = 0 THEN --if this is true, then no records were added.
DBMS_OUTPUT.PUT_LINE( 'Not records added'); --writes this message
COMMIT;
Note: There should not be any other statement between the INSERT statement and "IF SQL%ROWCOUNT = 0" validation. SQL%ROWCOUNT reflects most recently executed statement.
I have a few things of code I need help debugging but I feel that if I can get one of them running i'll be able to get the rest(oh how i hope).
create or replace
trigger minimumwage
before insert or update on Employee
for each row
begin
if :new.Wage < 7.25
then raise_application_error('-20000,Pay is below Texas minimum wage!');
end if;
end;
/
I'm trying to do this on a table ran on my school's server through sqlplus if that helps.
When you're getting an error, it's always helpful to specify what error. There is a syntax error in the raise_application_error call in your trigger. That procedure takes two arguments, a number and a string. You are passing in a single argument that is one long string.
create or replace trigger minimumwage
before insert or update on Employee
for each row
begin
if :new.Wage < 7.25
then
raise_application_error(-20000,'Pay is below Texas minimum wage!');
end if;
end;
should be valid assuming there is a WAGE column in your EMPLOYEE table.
create or replace trigger deny_dec_pu before update of PU on ARTICLE
for each row
declare
erreur_pu exception;
begin
*insert into erreur values ('Operation de MAJ',sysdate);*
-- this intruction is never executec why ?
if (:new.pu < :old.pu) then
raise erreur_pu ;
end if;
exception
when erreur_pu then
Raise_application_error(-20100, 'rrrrrrrr', FALSE);
end;
/