创建p_user_check
CREATE OR REPLACE FUNCTION schema_a.user_check(p_username varchar, p_password varchar)
RETURNS int AS $$ -- $$需要后最后$$配对,可以任意取名,如$BODY$
DECLARE -- 变量申明
_passwd varchar;
_out_code int := -99; -- 申明时可赋初始值
BEGIN -- 开始
SELECT password INTO _passwd
FROM schema_a.users
WHERE name = p_username;
if found then
if _passwd = p_password then
_out_code := 0;
else
_out_code := -2;
end if;
else
_out_code := -1; -- user not exists
end if;
return _out_code;
EXCEPTION WHEN OTHERS THEN --异常处理
raise notice 'exception in user_check: %, %', SQLSTATE, SQLERRM;
return _out_code;
END; --结束
$$
LANGUAGE plpgsql STABLE SECURITY DEFINER;
保存为/home/kian.gao/Desktop/user_check.sql
在psql中执行脚本\i /home/kian.gao/Desktop/user_check.sql