'How can I use if exists with execute?

I am unable to create this stored procedure due to syntax error. Is here any one can solve this syntax issue?

DROP PROCEDURE REMOTE_DB_CONNECTION_REMOVER;
CREATE PROCEDURE REMOTE_DB_CONNECTION_REMOVER(
    rm_server_user varchar(100),
    rm_server_name varchar(100),
    rm_table varchar(100),
    rm_server_extenstion integer=0
 ) LANGUAGE PLPGSQL AS $$
BEGIN

    IF EXISTS(EXECUTE FORMAT('DROP FOREIGN TABLE  %s', rm_table)) THEN
        EXECUTE FORMAT('DROP FOREIGN TABLE  %s', rm_table);
    END IF

    IF EXISTS(EXECUTE FORMAT('DROP USER MAPPING FOR %s SERVER %s',rm_server_user, rm_server_name)) THEN
        EXECUTE FORMAT('DROP USER MAPPING FOR %s SERVER %s',rm_server_user, rm_server_name);
    END IF

    IF EXISTS(EXECUTE FORMAT('DROP SERVER %s', rm_server_name)) THEN
        EXECUTE FORMAT('DROP SERVER %s', rm_server_name);
    END IF
            
END; $$ 

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source