gpt4 book ai didi

postgresql - 无法在 postgreSQL 中触发异常 NO_DATA_FOUND

转载 作者:行者123 更新时间:2023-11-29 12:05:09 26 4
gpt4 key购买 nike

即使返回的行或结果集为零,我也无法从 PostgreSql 8.2 中的函数触发异常 NO_DATA_FOUND。

这是我的代码;

 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');

最佳答案

要触发 NO_DATA_FOUND 异常使用:

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

详情在这里:http://www.postgresql.org/docs/current/static/plpgsql-statements.html

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

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

关于postgresql - 无法在 postgreSQL 中触发异常 NO_DATA_FOUND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26888057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com