-1

我有一个名为 BANKCUSTOMER 的表,其中包含以下列:

 USERNAME                                  NOT NULL VARCHAR2(11)
 FAMILY_NAME                               NOT NULL VARCHAR2(25)
 NAME                                      NOT NULL VARCHAR2(25)
 PASSWD                                    NOT NULL VARCHAR2(6)

如果用户 USERNAME 和 PASSWORD 与数据库中的数据匹配,我想创建一个函数来检查数据库。如果登录成功,那么它应该打印出“登录成功!” 否则“用户名或密码错误!”

我访问了一个 pl/sql 教程站点并看到了以下代码,我对其进行了一些修改,以便它可以与我的数据库一起使用,但是有些东西我不明白,那就是做什么z number和做什么begin select 1 into z。有人可以为我解释一下。

create or replace function log_in(x in varchar2, y in varchar2)   
return varchar2  
as  
  z number;  
begin  
  select 1  
    into z   
    from bankcustomer   
    where username=x   
    and passwd=y;  
  dbms_output.put_line('Login successful!');  
exception  
when no_data_found then  
  dbms_output.put_line('Wrong username or password!'); 
end; 

我想通过写来测试这个功能,SELECT log_in() FROM dual;看看它是否有效。当我写信时,SELECT log_in() FROM dual;我收到一条错误消息:

从命令中的第 1 行开始出错:SELECT log_in() FROM dual 命令行错误:1 列:7 错误报告:SQL 错误:ORA-06553:PLS-306:调用“LOG_IN”06553 中的参数数量或类型错误. 00000 - “PLS-%s: %s” *原因:
*操作:

如何解决?

4

3 回答 3

2

You have defined a function but do not return a value from it. Given the fact that you "select" the function there is no need to use dbms_output:

create or replace function log_in(x in varchar2, y in varchar2)
return varchar2
as
  match_count number;
begin
  select count(*)
    into match_count
    from bankcustomer
    where username=x
    and passwd=y;
  if match_count = 0 then
    return 'Wrong username or password!';
  elsif match_count = 1 then
    return 'Login successful!';
  else
    return 'Too many matches, this should never happen!';
  end if;
end;
/

Additionally your call to the function does not provide the username and password parameters, that's why you get the error message. Assuming you have changed the function to actually return something, you need to use

SELECT log_in('username', 'secretpassword') FROM dual;
于 2012-02-18T08:54:28.103 回答
1

您实际上是否向log_in函数传递了任何参数?什么是logga_in()?后者是你这边的错字吗?

无论如何,select 1 into z如果找不到匹配项,唯一的强制异常。而已。

换句话说,您可以在没有它的情况下编写代码,例如使用select count(*) into authenticated ...然后您可以检查if authenticated != 0并执行适当的操作。我没有Oracle实例,所以这段代码是盲目编写的,你需要测试它:

create or replace function log_in(x in varchar2, y in varchar2)
return varchar2
as
  match_count number;
begin
  select count(*)
    into match_count
    from bankcustomer
    where username=x
    and passwd=y;
  if match_count = 0 then
    dbms_output.put_line('Wrong username or password!');
  elsif match_count = 1 then
    dbms_output.put_line('Login successful!');
  else
    dbms_output.put_line('Too many matches, this should never happen!');
end;
于 2012-02-18T02:27:13.617 回答
0

Just to add more information to what's already been provided, the BEGIN keyword indicates the beginning of the execution block; what's above that is the function header and any declaration statements.

The statement z number; is a variable declaration statement declaring a variable that is named z and is of the datatype number. The SELECT 1 INTO z WHERE... statement is checking the BANKCUSTOMER table for a row where the username matches what's passed to the function in the first parameter, and a password that matches what's passed to the function in the second parameter.

If there is a row where the username and password match what's passed to the function, then the variable z will contain the number 1. If there isn't, the Oracle NO_ROWS_FOUND exception will be raised, because SELECT...INTO statements must always select one and only one row, or else they will raise an exception (the NO_ROWS_FOUND exception for no rows, and the TOO_MANY_ROWS exception for more than one row).

Hope that's helpful! Don't hesitate to ask if you have more questions.

于 2012-02-18T04:51:51.627 回答