I created a user defined function that calculates the quantity in stock for a product
CREATE OR REPLACE FUNCTION function_quantityInStock(
oldProductQuantity IN INTEGER,
orderedQuan IN INTEGER)
RETURN INTEGER
IS
v_newQuantity INTEGER;
v_oldQuantity INTEGER;
v_orderedQuan INTEGER;
BEGIN
v_newquantity := oldProductQuantity - orderedQuan;
RETURN v_newquantity;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Please check your data.');
END function_quantityInstock;
I then create a trigger that updates the table by calling the function
CREATE OR REPLACE TRIGGER TRIGGER_QUANTITY AFTER INSERT ON ordered_product
FOR EACH ROW
DECLARE
v_oldQuantity INTEGER;
BEGIN
SELECT PRODUCT_QUANTITYINSTOCK INTO v_oldQuantity
FROM product
WHERE product_id = :NEW.product_id;
UPDATE PRODUCT
SET product_quantityinstock =
function_quantityINSTOCK(v_oldQuantity, :NEW.ORDERED_PRODUCTQUANTITY)
WHERE product_id = :NEW.product_id;
END;
I want to display a message when the user enters invalid data but my exception block doesnt do that.
I use the following anonyomus block to test the function:
SET SERVEROUTPUT ON;
DECLARE
v_productID ordered_product.product_id%TYPE:= &ProductID;
v_orderID ordered_product.order_id%TYPE:=&OrderID;
v_orderedQuan ordered_product.ordered_productQuantity%TYPE := &OrderedProductQuantity;
v_totalCost ordered_product.ordered_productTotalCost%TYPE := '&TotalCost';
BEGIN
INSERT INTO ordered_product VALUES
(v_orderID, v_productID, v_orderedQuan, v_totalCost);
dbms_output.put_line('A new record has been inserted.');
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('Invalid data!');
WHEN VALUE_ERROR THEN
dbms_output.put_line('Error! Please check your values.'|| SQLERRM);
END;
You might be missing exec dbms_output.enable(10000) and therefore not be seeing the output.
You should throw the exception using
raise_application_error(-20000, 'Please check your data.');
Also it's bad practice to catch all exceptions using when others and then ignore them.
If I were you, I wouldn't rely on dbms_output.put_line to pass information around. Instead, rely on the standard exception handling, e.g. RAISE or RAISE_APPLICATION_ERROR. Also, given that you're trying to find the new quantity, I'd move the select on the product table into the function, something like:
CREATE OR REPLACE FUNCTION function_quantityinstock(p_product_id IN product.product_product_id p_orderedquan IN INTEGER)
RETURN INTEGER IS
v_newquantity INTEGER;
e_not_enough_stock EXCEPTION;
e_not_enough_stock_num INTEGER := -20001;
e_no_product_exists_num INTEGER := -20002;
PRAGMA EXCEPTION_INIT(e_not_enough_stock, -20001);
BEGIN
SELECT product_quantityinstock - orderedquan
INTO v_newquantity
FROM product
WHERE product_id = :new.product_id;
IF v_newquantity < 0
THEN
RAISE e_not_enough_stock;
END IF;
RETURN v_newquantity;
EXCEPTION
WHEN no_data_found THEN
raise_application_error(-e_no_product_exists_num,
'No product exists for product_id = ' || product_id);
-- no need to check for TOO_MANY_ROWS if product_id is the primary/unique key on the product table.
WHEN e_not_enough_stock THEN
raise_application_error(e_not_enough_stock_num,
'Not enough stock present to fulfil the order for product_id = ' ||
product_id);
WHEN OTHERS THEN
raise_application_error(SQLCODE,
'Unexpected error occurred whilst finding new quantity for product_id = ' ||
product_id || ': ' || SQLERRM);
END function_quantityinstock;
/
You'd need to change the function name though, to reflect the action that it's doing, since it's returning the new quantity in stock...
Related
I want to update a table but I want to check if the record exist. If not then throw an exception. In the C# application I pass the parameters and execute the command by the following.
procedure usp_update_example
(
p_id in mydb.member.idn_member%type,
p_idn_person in mydb.member.idn_person%type,
p_ind_rep in mydb.member.ind_rep%type
)
as
v_exist pls_integer := 0;
v_step varchar2(250);
v_exception_not_exist exception;
begin
v_step := 'Check for record ' || p_id;
select count(1)
into v_exist
from mydb.member
where idn_member = p_id;
if v_exist = 0 then
raise v_exception_not_exist;
end if;
if (v_exist > 0) then
v_step := 'Update table :' || p_id;
update mydb.member
set
idn_person = p_idn_person,
ind_rep = p_ind_rep
where idn_member = p_id;
end if;
exception
when v_exception_not_exist then
Raise_application_error(-20001, 'Not exist');
end usp_update_example;
However even my condition is right, I do have the record existing in the table. I always get Not exist exception. If I don't use if v_exist = 0 and use WHEN NO_DATA_FOUND THEN. Then everything is fine.
I am not sure where is wrong.
Your code seems to be fine. Looks like this issue is related to some uncommitted data - you see the record in the session where you inserted it and you don't see it in C# session since the record is not committed yet. Hence, C# session generates exception.
What I would suggest re the procedure code is to make it more compact.
Something like the following:
...
begin
update mydb.member
set idn_person = p_idn_person,
ind_rep = p_ind_rep
where idn_member = p_id;
if SQL%ROWCOUNT = 0 then
raise_application_error(-20001,'Not exist');
end if;
end;
I am a beginner in PL/SQL. I need to write a function with following details:
Create a function named 'find_transaction_type' that will accept the transaction_type_id as input. Based on this input, the function must return the transaction type name of type varchar.
Function name : find_transaction_type,
Input Parameter : transaction_type_id in int
Design rules:
1)If transaction type id(i.e, transaction_type_id) passed as input, matches with the id in the transaction table,then it returns the type of the given transaction_type_id.
2)If the transaction type id passed as input, does not match with the id in the transaction table,then it throws ' no_data_found' exception and displays it with the text as ' No such Type'
Note: Use variable to print the exceptions instead of 'dbms_output.put_line'
ie: umpire_name := 'No such umpire';
My Solution is:
create or replace function find_transaction_type(transaction_type_id in integer) return varchar is
transaction_type_name varchar(255);
type_id integer;
error_msg varchar(255);
begin
error_msg := 'No such Type';
select id
into type_id
from transaction_type;
if type_id = transaction_type_id
then
select type
into transaction_type_name
from transaction_type
where id = transaction_type_id;
return(transaction_type_name);
else
raise no_data_found;
end if;
exception
when no_data_found then
raise_application_error(-10403, error_msg);
end;
/
What's wrong with my code?
You do not need the first select nor do you need the if statement. Just let the query raise the no_data_found exception. Refer to types by their table's respective types.
create or replace function find_transaction_type (
transaction_type_id in transaction_type.transaction_type_id%type
)
return transaction_type.type%type is
transaction_type_name transaction_type.type%type;
begin
select type -- not a good column name
into transaction_type_name -- this should be the column name also
from transaction_type
where id = transaction_type_id;
return transaction_type_name;
exception
when no_data_found then
if transaction_type_id is null then
raise_application_error(-10403, "type argument is null");
else
raise_application_error(-10403, "type '" || transaction_type_id || "' not found");
end if;
end;
Use varchar2 instead of varchar.
Add a where clause to the first select statement.
If you do 2 no need for the if then else.
I need to create a trigger to control a link between tables. I have the following table structure (image below). an item is linked to a budget and a structure, and this structure should be linked to the same budget.
Sometimes the item receive wrong structures, receive structures different from its own budget budget. I need that when you insert a budget item or edit a budget item to be validated if the id_budget are equal, the id_budget of dg_budget_item is equal to the id_budget dg_budget_structure to which it is being linked
I started building this trigger, but do not know how to continue
CREATE TRIGGER T_BUDGETO_STRUCTURE_ITEM_ID_STRUCTURE
BEFORE INSERT OR UPDATE OF id_budget_structure ON dg_budget_structure_item
select id_budget from dg_budget_structure where id_budget_structure = new.id_budget_structure
FOR EACH ROW
WHEN (new.id_budget <> ???(result of select?))
pl/sql_block
This should get you close to what you want
CREATE OR REPLACE TRIGGER T_BUDGET_STRUCTURE_ITEM_ID_STRUCTURE
BEFORE INSERT OR UPDATE OF id_budget_structure ON dg_budget_item
FOR EACH ROW
DECLARE
v_id_budget_structure NUMBER;
Budgets_do_not_match EXCEPTION;
v_code NUMBER;
v_errm VARCHAR2(64);
BEGIN
SELECT id_budget INTO v_id_budget_structure FROM dg_budget_structure WHERE id_budget_structure = :new.id_budget_structure
IF :new.id_budget <> v_id_budget_structure THEN
RAISE Budgets_do_not_match;
END IF;
EXCEPTION
WHEN Budgets_do_not_match THEN
Raise_application_error (-20001,
'Budget '||TO_CHAR(:new.id_budget)||' for structure '
|| :new.id_budget_structure || 'does not match the budget trying to be linked';
WHEN NO_DATA_FOUND THEN
Raise_application_error(-20002,
'Invalid budget structure ' ||:new.id_budget_structure);
WHEN OTHERS THEN
v_code := SQLCODE;
v_errm := SUBSTR(SQLERRM, 1 , 64);
Raise_application_error(-20000,
'Unexpected error ' || v_code ': ' || v_errm;
END;
You cannot ROLLBACK within a trigger so your application will need to deal with that.
This is what I have so far. When I enter valid data and run it the table updates correctly. When I run it using author numbers that I know don't exist in the table it still runs, and doesn't output the exception statements. Does anyone know why my exceptions don't seem to be working. Any help would be appreciated thanks!
CREATE OR REPLACE PROCEDURE update_authorname
(selected_author_num IN NUMBER,
new_author_first IN CHAR,
new_author_last IN CHAR) AS
BEGIN
UPDATE author
SET author_first = new_author_first, author_last = new_author_last
WHERE author_num = selected_author_num;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.PUT_LINE('No author with this number exists: ' || selected_author_num);
WHEN ROWTYPE_MISMATCH
THEN
DBMS_OUTPUT.PUT_LINE('Error: There was a row type mismatch when updating');
END;
/
This is what I used to call the procedure:
BEGIN
update_authorname(6,'Emma','White');
END;
/
An update statement doesn't throw an exception when the where clause isn't matched, it just fails silently. Instead, you need to check either:
The row exists before trying to update or
The update changed something.
1. The row exists
CREATE OR REPLACE PROCEDURE update_authorname
(selected_author_num IN NUMBER,
new_author_first IN CHAR,
new_author_last IN CHAR) AS
BEGIN
IF NOT EXISTS (SELECT 1 FROM author WHERE author_num = selected_author_num;
DBMS_OUTPUT.PUT_LINE('No author with this number exists: ' || selected_author_num);
ELSE
UPDATE author
SET author_first = new_author_first, author_last = new_author_last
WHERE author_num = selected_author_num;
EXCEPTION
WHEN ROWTYPE_MISMATCH
THEN
DBMS_OUTPUT.PUT_LINE('Error: There was a row type mismatch when updating');
END;
/
2. The update made changes
CREATE OR REPLACE PROCEDURE update_authorname
(selected_author_num IN NUMBER,
new_author_first IN CHAR,
new_author_last IN CHAR) AS
BEGIN
UPDATE author
SET author_first = new_author_first, author_last = new_author_last
WHERE author_num = selected_author_num;
IF ##ROWCOUNT <= 0
DBMS_OUTPUT.PUT_LINE('No author with this number exists: ' || selected_author_num);
EXCEPTION
WHEN ROWTYPE_MISMATCH
THEN
DBMS_OUTPUT.PUT_LINE('Error: There was a row type mismatch when updating');
END;
/
There are certainly other ways to achieve the same result, but these are the two most common.
I would like to create a trigger that prevents a student from enrolling into a new module if he has any outstanding bills.
studID studNRIC paymentStatus
-------------------------------------
200 F7654672F Non Payment
it would reject the following statement:
INSERT INTO student(studID, studNRIC, paymentStatus)
VALUES (201, 'F7654672F', 'Good');
I've came out with the following trigger but I'm still able to insert a new student.
set define off;
CREATE OR REPLACE TRIGGER reject_new_account
AFTER INSERT OR UPDATE ON Student
FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
totover NUMBER(3);
BEGIN
SELECT COUNT (*)
INTO totover
FROM Student
WHERE :NEW.nric = student.nric
AND :NEW.paymentStatus = 'Non Payment';
IF totover > 0 THEN
RAISE_APPLICATION_ERROR ( -20002,
'Student' || :NEW.nric ||
' has outstanding bills' );
END IF;
END;
/
there's seems to be a problem with line 13 AND :NEW.paymentStatus = 'Non Payment';
so how do I go about doing this?
table structure
CREATE TABLE Student(
studID INTEGER NOT NULL,
firstName CHAR(25) NULL,
lastName CHAR(25) NULL,
NRIC CHAR(9) NOT NULL,
paymentStatus CHAR(25) Default 'Good',
CONSTRAINT stud_Pkey PRIMARY KEY (studID),
CONSTRAINT studPaymentStatus_type CHECK (PaymentStatus IN ('Late Payment', 'Non Payment', 'Good'))
);
There are a couple of issues with this trigger.
First if you want to prevent an INSERT you need to use a BEFORE trigger. An AFTER trigger will fire after the insert has successfully completed, by which point it is too late to stop the insert.
Secondly, I'm unsure about what you are trying to achieve with your SQL statement. Since the trigger is attached to the customer table you don't need to do a select on the customer table to access the record data. You can just do:
IF :NEW.badstatus = 'Non Payment'
THEN
RAISE_APPLICATION_ERROR ( -20002,
'Employee ' || :NEW.nric ||
' already has 5 readings' );
END IF;
You probably don't want to check if the new record has a bad status, you want to check if the existing customer records have a bad status. As James mentioned, a BEFORE trigger probably makes more sense, but that depends on what you are trying to do, whether the insert gets rollback or not by the caller, etc.
CREATE OR REPLACE TRIGGER reject_new_account
AFTER INSERT OR UPDATE ON Customer
FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
totover NUMBER(3);
BEGIN
SELECT COUNT (*)
INTO totover
FROM Customer c
WHERE c.nric = :NEW.nric
AND c.badstatus = 'Non Payment';
IF totover > 0 THEN
RAISE_APPLICATION_ERROR ( -20002,
'Employee ' || :NEW.nric ||
' already has 5 readings' );
END IF;
END;
/
I've removed the PRAGMA AUTONOMOUS_TRANSACTION; for you and replace it with an exception to handle the issue, can you try if it works.
set define off;
CREATE OR REPLACE TRIGGER reject_new_account
BEFORE INSERT OR UPDATE ON CUSTOMER
FOR EACH ROW
DECLARE
totover CHAR(100);
BEGIN
SELECT distinct badStatus
INTO totover
FROM customer
WHERE :NEW.nric = CUSTOMER.nric;
IF totover = 'Non Payment' THEN
RAISE_APPLICATION_ERROR ( -20003,
'Customer ' || :NEW.firstName||''||:NEW.lastName ||
' cannot create new account due to bad payment' );
END IF;
EXCEPTION
WHEN NO_DATA_FOUND
THEN DBMS_OUTPUT.PUT_LINE(TO_CHAR(SQLERRM(-20299)));
END;
/
As I assume you have discovered, you cannot select from the same table that a row-level trigger is defined against; it causes a table mutating exception. You have attempted to get round this by adding the autonomous transaction pragma. Unfortunately, although this works, it is just covering up your mistake in methodology.
In order to properly create this validation using a trigger a procedure should be created to obtain user-specified locks so the validation can be correctly serialized in a multi-user environment.
PROCEDURE request_lock
(p_lockname IN VARCHAR2
,p_lockmode IN INTEGER DEFAULT dbms_lock.x_mode
,p_timeout IN INTEGER DEFAULT 60
,p_release_on_commit IN BOOLEAN DEFAULT TRUE
,p_expiration_secs IN INTEGER DEFAULT 600)
IS
-- dbms_lock.allocate_unique issues implicit commit, so place in its own
-- transaction so it does not affect the caller
PRAGMA AUTONOMOUS_TRANSACTION;
l_lockhandle VARCHAR2(128);
l_return NUMBER;
BEGIN
dbms_lock.allocate_unique
(lockname => p_lockname
,lockhandle => p_lockhandle
,expiration_secs => p_expiration_secs);
l_return := dbms_lock.request
(lockhandle => l_lockhandle
,lockmode => p_lockmode
,timeout => p_timeout
,release_on_commit => p_release_on_commit);
IF (l_return = 1) THEN
raise_application_error(-20001, 'dbms_lock.request Timeout');
ELSIF (l_return = 2) THEN
raise_application_error(-20001, 'dbms_lock.request Deadlock');
ELSIF (l_return = 3) THEN
raise_application_error(-20001, 'dbms_lock.request Parameter Error');
ELSIF (l_return = 5) THEN
raise_application_error(-20001, 'dbms_lock.request Illegal Lock Handle');
ELSIF (l_return not in (0,4)) THEN
raise_application_error(-20001, 'dbms_lock.request Unknown Return Value ' || l_return);
END IF;
-- Must COMMIT an autonomous transaction
COMMIT;
END request_lock;
This procedure can then be used in a compound trigger (assuming at least Oracle 11, this will need to be split into individual triggers in earlier versions)
CREATE OR REPLACE TRIGGER reject_new_account
FOR INSERT OR UPDATE ON student
COMPOUND TRIGGER
-- Table to hold identifiers of inserted/updated students
g_studIDs sys.odcinumberlist;
BEFORE STATEMENT
IS
BEGIN
-- Reset the internal student table
g_studIDs := g_studIDs();
END BEFORE STATEMENT;
AFTER EACH ROW
IS
BEGIN
-- Store the inserted/updated students; the payment status may be updated
-- without checking the constraint
IF ( INSERTING
OR ( UPDATING
AND ( :new.studID <> :old.studID
OR :new.NRIC <> :old.NRIC)))
THEN
g_studIDs.EXTEND;
g_studIDs(g_studIDs.LAST) := :new.studID;
END IF;
END AFTER EACH ROW;
AFTER STATEMENT
IS
CURSOR csr_students
IS
SELECT sdt.studID
, sdt.NRIC
FROM TABLE(g_studIDs) sid
INNER JOIN student sdt
ON (sdt.studID = sid.column_value)
ORDER BY sdt.NRIC;
CURSOR csr_constraint_violations
(p_studID student.studID%TYPE
,p_NRIC student.studNRIC%TYPE)
IS
SELECT NULL
FROM student sdt
WHERE sdt.NRIC = p_NRIC
AND sdt.paymentStatus = 'Bad Payment'
AND sdt.studID <> p_studID;
r_constraint_violation csr_constraint_violations%ROWTYPE;
BEGIN
-- Check if for any inserted/updated student there exists another record for
-- the same NRIC with a Bad Payment status. Serialise the constraint for each
-- NRIC so concurrent transactions do not affect each other
FOR r_student IN csr_students LOOP
request_lock('REJECT_NEW_ACCOUNT_' || r_student.NRIC);
OPEN csr_constraint_violations(r_student.studID, r_student.NRIC);
FETCH csr_constraint_violations INTO r_constraint_violation;
IF csr_constraint_violations%FOUND THEN
CLOSE csr_constraint_violations;
raise_application_error(-20001, 'Student ' || r_student.NRIC || ' has Bad Payment status');
ELSE
CLOSE csr_constraint_violations;
END IF;
END LOOP;
END AFTER STATEMENT;
END;