【问题标题】:How to get a record using a where clause in Oracle?如何使用 Oracle 中的 where 子句获取记录?
【发布时间】:2015-05-18 15:18:25
【问题描述】:

我想在程序运行时从表中获取记录。该程序将输入一个数字,例如employee_number,它会返回一条完整的记录——其中将包含例如employee_name、公司、加入日期等。我通常不使用程序。我喜欢分析 SQL。

create or replace procedure getdetails (search_strin table_name.column_1%type,
                      p_recordset out sys_refcursor) as 
begin 
  open p_recordset for
    select column_2, column_3
    from   table_name
    where  column_1= search_str;
end getdetails;

这应该可行,对吧?但是,我收到以下错误!

PLS-00306:调用“GET_EMP_RS”时参数的数量或类型错误

【问题讨论】:

  • 你的过程被命名为getdetails,那么GET_EMP_RS是什么?

标签: oracle stored-procedures cursor where-clause


【解决方案1】:

假设您的表名为 EMPLOYEE。要执行您所要求的操作,您需要执行以下操作:

CREATE OR REPLACE FUNCTION GET_EMPLOYEE_RECORD(nEmployee_number IN NUMBER)
  RETURN EMPLOYEE%ROWTYPE
IS
  rowEmployee  EMPLOYEE%ROWTYPE;
BEGIN
  SELECT e.*
    INTO rowEmployee
    FROM EMPLOYEE e
    WHERE e.EMPLOYEE_NUMBER = nEmployee_number;

  RETURN rowEmployee;
END GET_EMPLOYEE_RECORD;

编辑

如果您需要使用过程而不是函数,那么您需要使用输出参数来返回数据;因此,您可以执行以下操作:

CREATE OR REPLACE PROCEDURE GET_EMPLOYEE_RECORD
   (pin_Employee_number IN  NUMBER
    pout_Employee_row   OUT EMPLOYEE%ROWTYPE)
IS
BEGIN
  SELECT e.*
    INTO pout_Employee_row   
    FROM EMPLOYEE e
    WHERE e.EMPLOYEE_NUMBER = pin_Employee_number ;
END GET_EMPLOYEE_RECORD;

然后您可以从类似于以下的代码调用此过程:

DECLARE
  nEmployee_number  NUMBER;
  rowEmployee       EMPLOYEE%ROWTYPE;
BEGIN
  nEmployee_number := 123;  -- or whatever value you like

  GET_EMPLOYEE_RECORD(pin_Employee_number => nEmployee_number,
                      pout_Employee_row   => rowEmployee);

  -- Now do something with the fields in rowEmployee...
END;

【讨论】:

  • 谢谢!当我使用代码时,我得到了这个。 ORA-06553: PLS-801: 内部错误 [55018] 06553. 00000 - "PLS-%s: %s"
  • 另外,我们正在使用的应用程序存在限制。我们不能使用函数——我们只能使用过程来获取记录。
  • 是的,你需要一个 rowEmployee 声明,rowEmployee EMP%ROWTYPE; 才能编译。
  • @MontyPython - 我添加了一个示例,说明如何使用过程完成此操作。祝你好运。
【解决方案2】:

您应该尝试在匿名块中执行该函数,我猜您已尝试在 select 语句中添加该函数。

声明 l_cust_record x_remedy_oracle%ROWTYPE; 开始

l_cust_record := get_CUSTOMER('02393','Service');

结束;

【讨论】:

    猜你喜欢
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2014-03-09
    • 1970-01-01
    • 2013-05-05
    • 2020-03-16
    • 2021-08-16
    相关资源
    最近更新 更多