[PostgreSql]PostgreSql调用函数及用IF EXISTS判断表是否存在

1.创建一个函数function1

-- FUNCTION: public.function1(character varying, integer)

-- DROP FUNCTION public.function1(character varying, integer);

CREATE OR REPLACE FUNCTION public.function1(
    useridl character varying,
    groupidl integer)
RETURNS TABLE(vehicle_id integer) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE 
    ROWS 1000
AS $BODY$

BEGIN
-- Insert statements for procedure here
RETURN QUERY
select v.vehicle_id
from mst_vehicle as v
where v.deletef=0;
END;

$BODY$;

ALTER FUNCTION public.function1(character varying, integer)
    OWNER TO postgres;

2.在另一个函数function2中调用function1

-- FUNCTION: public.function2(character varying,integer,timestamp without time zone)

-- DROP FUNCTION public.function2(character varying,integer,timestamp without time zone);

CREATE OR REPLACE FUNCTION public.function2(
    userIDl character varying,
    groupIDl integer,
    minVersionl timestamp without time zone)
RETURNS TABLE(
loading_info character varying(16)
 )
    LANGUAGE 'plpgsql'
    COST 100.0

AS $function$

BEGIN
-- Insert statements for procedure here
drop table IF EXISTS public.temp_ids;
create table public.temp_ids(vehicle_id int);
insert into public.temp_ids values(function1(userIDl,groupIDl));
RETURN QUERY
    
select 
car.loading_info
from dy_VehicleList

drop table IF EXISTS public.temp_ids;

END;

$function$;

ALTER FUNCTION public.function2(character varying,integer,timestamp without time zone)
    OWNER TO postgres;

其中用IF EXISTS判断表是否存在,存在则删除

DROP TABLE IF EXISTS tableName

猜你喜欢

转载自www.cnblogs.com/vickylinj/p/9668382.html