【问题标题】:How to write plsql exception satatement to ask user to give input in specific format如何编写pl sql异常语句以要求用户以特定格式输入
【发布时间】:2021-12-05 00:06:30
【问题描述】:
CREATE OR REPLACE PROCEDURE get_productdetails(

   p_reqid   OUT requirement.req_id%type,

   p_pid     OUT requirement.p_id%type,

   p_rstaffid OUT requirement.r_staff_id%type,
   
   p_demand  requirement.demand%type)

IS

CURSOR c_demand IS

SELECT req_id,p_id,r_staff_id from requirement where demand = upper(p_demand);

BEGIN

FOR i in c_demand Loop

DBMS_OUTPUT.PUT_LINE('Requirement ID :'||i.req_id);

DBMS_OUTPUT.PUT_LINE('Product ID'||i.p_id);

DBMS_OUTPUT.PUT_LINE('Staff ID :'||i.r_staff_id);

END LOOP;

END get_productdetails;

用户必须在 'HIGH/LOW/AVG 中输入需求,否则它应该抛出异常,要求输入该格式的数据 你能帮我写一个相应的例外吗

【问题讨论】:

  • 您的代码中没有“异常”块 - 您自己尝试过吗?你得到了什么错误?
  • 我从网上得到它我没有写这段代码但是如何在这段代码中添加异常你能帮我吗
  • 你试过什么?你检查过文档吗? Stack Overflow 不是家庭作业写作服务。如果您希望人们帮助您,请不要粘贴您在某处找到的代码块。提供一个可行的可重现案例:创建表的 ddl 语句、预期的功能以及到目前为止您尝试过的内容。
  • 我正在自学并试图理解 plsql 过程和函数 我遇到了一个 Q 在那里我被要求编写一个过程查询并要求包含异常 我试图在代码中编写异常语句但得到了如果你能告诉我如何添加异常语句,那就太好了
  • 14/1 PLS-00103:在预期以下情况之一时遇到符号“WHEN”:( begin case declare end exit for goto if loop mod null pragma raise return select update while with

标签: exception plsql procedure


【解决方案1】:

由于我无权访问您的表结构,因此下面的代码未经测试。一个很好的起点是官方oracle documentation。在您的情况下,需要一个用户定义的异常,您需要在声明部分声明它,然后在 RAISE 中声明,最后在 EXCEPTION 块中声明。

CREATE OR REPLACE PROCEDURE get_productdetails(
   p_reqid   OUT requirement.req_id%type,
   p_pid     OUT requirement.p_id%type,
   p_rstaffid OUT requirement.r_staff_id%type,
   p_demand  requirement.demand%type)
IS
  -- declare the user-defined exception
  invalid_input EXCEPTION;
  CURSOR c_demand IS
  SELECT req_id,p_id,r_staff_id from requirement where demand = upper(p_demand);
BEGIN
  IF p_demand NOT IN ('HIGH','LOW','AVG') THEN
    -- raise the exception if p_demand not one of the 3 accepted values
    RAISE invalid_input;
  END IF;
  FOR i in c_demand Loop
  DBMS_OUTPUT.PUT_LINE('Requirement ID :'||i.req_id);
  DBMS_OUTPUT.PUT_LINE('Product ID'||i.p_id);
  DBMS_OUTPUT.PUT_LINE('Staff ID :'||i.r_staff_id);
  END LOOP;
EXCEPTION WHEN  invalid_input THEN
  dbms_output.put_line('Only HIGH/LOW/AVG are valid input');  
END get_productdetails;

【讨论】:

  • 感谢 Koen Lostrie 帮助我了解如何使用异常
  • 乐于助人。请注意,触发器不是验证输入的最佳选择。通常这将在“客户端”端实现
猜你喜欢
  • 2023-02-10
  • 1970-01-01
  • 2020-01-29
  • 1970-01-01
  • 2014-03-01
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
相关资源
最近更新 更多