在oracle中执行存储过程

时间:2012-12-13 13:33:03

标签: oracle plsql

我需要根据通过提示符

传递的值在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

1 个答案:

答案 0 :(得分:2)

根据定义,程序不会返回任何内容。您正在寻找function

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; 

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

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

您可以通过以下两种方式执行此功能:

select chklg(:pass) from dual

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

要完成,Frank Schmitt在评论中提出了非常有效点。除了以非常危险的方式存储密码,如果两个用户拥有相同的密码会发生什么?

您将在SELECT INTO ...中收到TOO_MANY_ROWS异常。这意味着将太多行返回给变量。如果你也传递了用户名会更好。

这可能会使您的功能看起来像以下

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;