我运行查询时返回的函数没有错误,希望你能帮助我
CREATE OR REPLACE FUNCTION loginSesion(pPassword VARCHAR2, pUser VARCHAR2)
RETURN NUMBER
IS
vPassword VARCHAR2(100);
vUsername VARCHAR2(100);
BEGIN
SELECT Person.Username, Person.Password INTO vUsername, vPassword
FROM Person
WHERE Person.Password = pPassword and Person.Username = pUser;
EXCEPTION WHEN NO_DATA_FOUND THEN
vPassword := NULL;
vUsername := NULL;
IF vPassword IS NULL OR vUsername IS NULL THEN
RETURN FALSE;
ELSE
RETURN TRUE;
END IF;
END loginSesion;
我是sql的新手。
答案 0 :(得分:2)
正如其他人所解释的那样,如果发生异常,则只返回值。如果一切正常,你就不会退货。可以使用以下代码修复:
CREATE OR REPLACE FUNCTION loginSesion(pPassword VARCHAR2, pUser VARCHAR2)
RETURN NUMBER
IS
vPassword VARCHAR2(100);
vUsername VARCHAR2(100);
BEGIN
SELECT Person.Username, Person.Password INTO vUsername, vPassword
FROM Person
WHERE Person.Password = pPassword and Person.Username = pUser;
-- executed when a row was found
return 1;
EXCEPTION WHEN NO_DATA_FOUND THEN
-- executed when no row was found
RETURN 0;
END loginSesion;
/
使用异常作为IF
条件的替代是一种代码嗅觉(当然这有点主观)。
由于您从不使用返回行的值,因此可以简化函数以简单计算行数。
CREATE OR REPLACE FUNCTION loginSesion(pPassword VARCHAR2, pUser VARCHAR2)
RETURN NUMBER
IS
l_count integer;
BEGIN
SELECT count(*)
into l_count;
FROM Person
WHERE Person.Password = pPassword
and Person.Username = pUser;
if l_count = 0 then
return 0;
else
return 1;
end if;
END loginSesion;
/
如果主叫代码将零视为" false"如果" true"那么你可以移除if
并简单地使用return l_count;
如果count()
不唯一(我觉得很奇怪),username
也是必要的。因为如果有两个用户具有相同的用户名,则第一个解决方案中的select将抛出" select返回多于一行"异常。
答案 1 :(得分:1)
假设您想要返回1代表" true"如果找到用户名和密码的组合,则0表示" false"否则,然后:
return 1
,在之前添加EXCEPTION块return 0
您不需要在EXCEPTION块中指定任何内容,只需说出when no data found then return 0;
答案 2 :(得分:1)
专业提示。 Oracle有类似pls_warrinings的东西。你可以打开它。许多愚蠢的错误很容易找到。
ALTER SESSION SET PLSQL_WARNINGS = 'ENABLE:ALL'; - turn on
ALTER SESSION SET PLSQL_WARNINGS = 'DISABLE:ALL'; - turn off
修复示例中的编译错误。([Error] PLS-00382 (16: 15): PLS-00382: expression is of wrong type
)
接下来“编译”会显示
[Warning] PLW-05018 (1: 1): PLW-05018: unit LOGINSESION omitted optional AUTHID clause; default value DEFINER used
[Warning] PLW-06002 (15: 8): PLW-06002: Unreachable code
[Warning] PLW-05005 (1: 1): PLW-05005: subprogram LOGINSESION returns without value at line 18
首先警告不相关
PLW-06002: Unreachable code
就是这样的。
EXCEPTION WHEN NO_DATA_FOUND THEN
vPassword := NULL;
vUsername := NULL;
IF vPassword IS NULL OR vUsername IS NULL THEN
RETURN 0;
ELSE
RETURN 1; -- <-- this part is Unreachable (vPassword IS NULL OR vUsername IS null is allways ture)
END IF;
PLW-05005: subprogram LOGINSESION returns without value at line 18
因为当select有一行时不会返回值。
正确的版本是。
create or replace function loginSesion(pPassword varchar2, pUser varchar2)
return number
is
vPassword VARCHAR2(100);
vUsername VARCHAR2(100);
begin
SELECT Person.Username, Person.Password INTO vUsername, vPassword FROM Person WHERE Person.Password = pPassword and Person.Username = pUser;
return 0;
exception
when no_data_found then
return 0;
end loginSesion;
/
create or replace function loginSesion(pPassword varchar2, pUser varchar2)
return number
is
vResutl number;
begin
SELECT count(*) into vResutl FROM Person WHERE Person.Password = pPassword and Person.Username = pUser;
return vResutl;
end loginSesion;
/
答案 3 :(得分:0)
你要返回的是字符而不是数字。所以你需要改变返回类型值。
代码:
CREATE OR REPLACE FUNCTION loginSesion(pPassword VARCHAR2, pUser VARCHAR2)
RETURN NUMBER
IS
vPassword VARCHAR2(100);
vUsername VARCHAR2(100);
BEGIN
BEGIN
SELECT Person.Username, Person.Password INTO vUsername, vPassword
FROM Person
WHERE Person.Password = pPassword and Person.Username = pUser;
EXCEPTION WHEN NO_DATA_FOUND THEN
vPassword := NULL;
vUsername := NULL;
RETURN 0;
END;
IF vPassword IS NULL OR vUsername IS NULL THEN
RETURN 0;
ELSE
RETURN 1;
END IF;
exception
when others then
RETURN 0;
--dbms_output.put_line('Return' || ' - ' || sqlerrm);
END loginSesion;