2

我需要uu根据通过提示传递的值来获取输出

create or replace procedure chklg( uu out logn.username%TYPE
                                 , pass in logn.password%TYPE)
is
begin
select username into uu from logn where password=pass;
end; 

我尝试以这种方式执行上述过程:

begin 

chklg(:pass);

end
4

1 回答 1

2

根据定义,过程不返回任何内容。您正在寻找一个功能

create or replace function chklg ( p_pass in logn.password%TYPE
          ) return varchar2 is -- assuming that logn.username%TYP is a varchar2

   l_uu logn.username%type;

begin
   select username into l_uu from logn where password = p_pass;
   return l_uu;
-- If there-s no username that matches the password return null.
exception when no_data_found then
   return null;
end; 

我对此有点担心,因为它看起来好像您将密码存储为纯文本。这不是最佳做法。

您应该在用户名旁边存储密码的盐渍和胡椒散列,然后对密码应用相同的盐渍、胡椒和散列,并从数据库中选择散列。

您可以通过以下两种方式之一执行该功能:

select chklg(:pass) from dual

或者

declare
   l_pass logn.password%type;
begin
   l_pass := chklg(:pass);
end;
/

为了完整起见,弗兰克施密特在评论中提出了一个非常有效的观点。除了您以非常危险的方式存储密码之外,如果两个用户具有相同的密码,会发生什么情况?

您将在SELECT INTO .... 这意味着太多的行被返回给变量。如果您也传递用户名会更好。

这可能会使您的函数看起来像下面这样

create or replace function chklg ( 
         p_password_hash in logn.password%type
       , p_username in logn.username%type
          ) return number

   /* Authenticate a user, return 1/0 depending on whether they have
      entered the correct password.
      */

   l_yes number := 0;

begin

   -- Assumes that username is unique.
   select 1 into l_yes 
     from logn
    where password_hash = p_password_hash
      and username = p_username;

   return l_yes;

-- If there-s no username that matches the password return 0.
exception when no_data_found then
   return 0;
end; 

如果您只想使用一个过程(根本没有真正的理由这样做,因为它不必要地限制了您;您没有执行任何 DML),那么您可以获得输出参数,但您必须为过程提供一个参数它可以填充。

在你的情况下,它看起来像这样。

declare
   l_uu logn.username%type;
begin 
   chklg(l_uu, :pass);
   dbms_output.put_line(l_uu);
end;
于 2012-12-13T13:36:41.973 回答