1、存储过程返回一个表
CREATE OR REPLACE FUNCTION 存储过程名(
IN 参数1text,
IN 参数2text,
IN 参数3text)
RETURNS TABLE(v_id integer, v_n text, v_me text, v_sid integer, v_sno integer, v_sname text) AS
$BODY$
DECLARE sql text;
BEGIN
sql:='SELECT a.sid,a.no,a.name,b.sid,b.no,b.name
FROM 表名 a
INNER JOIN 表名 b ON a.station_id = b.sid';
IF 参数1= '' AND 参数2= '' AND 参数3= '' THEN
RETURN QUERY EXECUTE sql;
ELSEIF 参数1= '' AND 参数2 = '' THEN
sql:= sql || ' WHERE b.no = '''||in_stationNo||'''';
RETURN QUERY EXECUTE sql;
ELSEIF 参数2 = '' AND 参数3= '' THEN
sql:= sql || ' WHERE a.name LIKE ''%'||参数1||'%''';
RETURN QUERY EXECUTE sql;
ELSEIF 参数1= '' AND 参数3= '' THEN
sql:= sql || ' WHERE a.no LiKE ''%'||参数2||'%''';
RETURN QUERY EXECUTE sql;
ELSEIF 参数1= '' THEN
sql:= sql || ' WHERE a.name LIKE ''%'||参数2||'%''AND b.no = '''||参数3||'''';
RETURN QUERY EXECUTE sql;
ELSEIF 参数2= '' THEN
sql:= sql || ' WHERE a.no LIKE ''%'||参数1||'%''AND b.no = '''||参数3||'''';
RETURN QUERY EXECUTE sql;
ELSEIF 参数3= '' THEN
sql:= sql || ' WHERE a.name LIKE ''%'||参数2||'%''AND a.no LIKE ''%'||参数1||'%''';
RETURN QUERY EXECUTE sql;
ELSE
sql:= sql || ' WHERE a.name LIKE ''%'||参数2||'%''AND a.no LIKE ''%'||参数1||'%''AND b.no = '''||参数3||'''';
RETURN QUERY EXECUTE sql;
END IF;
END
$BODY$
LANGUAGE plpgsql VOLATILE;