plsql中的函数
我创建了这个函数,它可以工作:plsql中的函数,sql,function,plsql,Sql,Function,Plsql,我创建了这个函数,它可以工作: create or replace function log_in( p_ssnr in customer.ssnr%type, p_password in customer.password%type) return number as v_number number(1); begin if (p_ssnr is not null and p_password is not null) then v_
create or replace function log_in(
p_ssnr in customer.ssnr%type,
p_password in customer.password%type)
return number
as
v_number number(1);
begin
if
(p_ssnr is not null and p_password is not null) then
v_number:= 1;
else
v_number:= 0;
end if;
return v_number;
end;
如果我然后键入:从dual中选择log_in(‘社会保险号码’,‘密码’);然后我得到一个“1”
所以它起作用了。但是你基本上可以输入你想要的任何东西,它会工作,所以它不是一个干净的代码。既然不能在if语句中包含子查询,那么如何创建函数,以便在实际键入与实际客户表匹配的社会保险号(ssnr)和密码时只返回1?否则返回0?不确定同一客户是否可以有多行。如果是,则使用以下方法:
create or replace function log_in(
p_ssnr in customer.ssnr%type,
p_password in customer.password%type)
return number
as
v_number number(1);
begin
select case when count(*) > 0 then 1 else 0 end
into v_number
from customer
where ssnr = p_ssnr and password = p_password;
return v_number;
end;
如果不是,那么这应该是好的:
create or replace function log_in(
p_ssnr in customer.ssnr%type,
p_password in customer.password%type)
return number
as
v_number number(1);
begin
count(*)
into v_number
from customer
where ssnr = p_ssnr and password = p_password;
return v_number;
end;