如何使用Oracle中的where子句获取记录?

时间:2015-03-16 13:27:08

标签: oracle stored-procedures cursor where-clause

我想在程序运行时从表中获取记录。该程序将输入一个数字,例如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'时参数的数量或类型错误

2 个答案:

答案 0 :(得分: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;

分享并享受。

答案 1 :(得分:0)

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

DECLARE l_cust_record x_remedy_oracle%ROWTYPE; BEGIN

l_cust_record:= get_CUSTOMER(' 02393','服务');

END;