Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Able to Trigger the Exception NO_DATA_FOUND in postgreSQL

I am not able to trigger the exceptions NO_DATA_FOUND from functions in PostgreSql 8.2 even if the returned rows or result sets is zero.

Here is my code;

 CREATE OR REPLACE FUNCTION func_ex() RETURNS trigger AS  

$func_ex$
    DECLARE               
        var_name  name;                 
    BEGIN
           Select empname INTO var_name from emp_table1 WHERE empid = 161232;  

    EXCEPTION
        WHEN NO_DATA_FOUND THEN 
        RAISE EXCEPTION 'No data found';
        RETURN NEW;
    END;
  return new
$func_ex$ LANGUAGE plpgsql;
-- End of Function

-- Creation of Trigger
CREATE TRIGGER insert_trigger1 AFTER update of empname
    ON emp_table1 EXECUTE PROCEDURE func_ex();


-- insertion enteries.
INSERT INTO emp_table1 (empid, empname, salary) values (124, ' Sapmle_CustormerName', '3000');
like image 600
user1581721 Avatar asked Aug 31 '26 07:08

user1581721


1 Answers

To trigger NO_DATA_FOUND exception use:

Select empname INTO STRICT var_name from emp_table1 WHERE empid = 161232;

Details here: http://www.postgresql.org/docs/current/static/plpgsql-statements.html

If STRICT is not specified in the INTO clause, then target will be set to the first row returned by the query, or to nulls if the query returned no rows.

If the STRICT option is specified, the query must return exactly one row or a run-time error will be reported, either NO_DATA_FOUND (no rows) or TOO_MANY_ROWS (more than one row).

like image 57
Ihor Romanchenko Avatar answered Sep 03 '26 03:09

Ihor Romanchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!