存储函数

Stored Functions

我在尝试创建一个存储函数来搜索用户的用户名和密码时遇到问题。该函数的参数应该是用户名和密码,如果用户名和密码组合存在,则它必须 return 为真,否则为假。这是我到目前为止没有成功创建的:

 delimiter $$
    create function cred(u varchar(15), p varchar(6))
        returns char(5) 
        begin
            declare a CHAR(5);
            if (select username = u and pwd = p from customers) then set a = 'true';
            else set a = 'false';
            end if;
        end$$
delimiter ;

select cred('dJete', 'abc112');

您需要在函数中使用 return 语句来 return 值。此外,对匹配客户的实际查询进行了小的修复:

create function cred(in_username varchar(15), in_password varchar(6))
returns char(5) 
begin

declare v_cnt int;

select count(*) into v_cnt
from customers
where username = in_username and pwd = in_password;

if (v_cnt>0) then
  return 'true';
else
  return 'false';
end if;

end
$$