【问题标题】:Retrieve data by user input with a message (PL/SQL)通过带有消息的用户输入检索数据 (PL/SQL)
【发布时间】:2022-01-01 21:20:25
【问题描述】:

我想知道如何通过插入 C_Id 作为具有已定义变量和异常的用户输入来从我在此处添加的表中检索数据。如果任何客户不可用,则必须显示一条消息,显示“未找到客户”。请帮助我理解这一点。

谢谢!

【问题讨论】:

    标签: sql oracle plsql oracle-sqldeveloper plsqldeveloper


    【解决方案1】:

    如果它是一个存储过程(应该是;它接受一个参数)并且您只想显示您找到的内容,那么这可能是一种选择:

    create or replace procedure p_test (par_c_id in customer_details.c_id%type)
    is
      l_row customer_details%rowtype;
    begin
      select *
        into l_row
        from customer_details
        where c_id = par_c_id;
        
      dbms_output.put_line(l_row.customer_name ||', '||
                              l_row.address       ||', '||
                              to_char(l_row.date_of_joined, 'dd.mm.yyyy')
                             );
    exception
      when no_data_found then
        dbms_output.put_line('No customer found');
    end;
    /   
    

    然后运行它

    set serveroutput on
    begin
      p_test(121);
    end;
    /
    

    它有什么作用?

    • 接受参数
    • 声明局部变量等于表的行类型
    • 选择所有内容到变量中
    • 使用dbms_output.put_line,显示你想要的元素
    • 如果未找到任何内容,则会引发异常 (no_data_found),并以显示适当消息的方式进行处理

    请注意,如果您使用的工具(例如 SQL*Plus、SQL Developer、TOAD...)允许显示 dbms_output.put_line 的结果,则此选项有效。否则,如果您使用例如Oracle Apex,程序仍然可以工作,但您什么都看不到。

    【讨论】:

    • 感谢您的回答。但是如何添加用户输入呢?
    • 这是您传递给过程的参数。你将如何通过它?取决于工具。在 SQL*Plus 中,您将使用替换变量,例如p_test(&par_id);。或者,在 TOAD 中,您可以使用绑定变量,例如p_test(:par_id);
    • 非常感谢您的支持
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多