St.Antario
St.Antario

Reputation: 27385

Why can't I execute a function whithin anonymous plpg block?

Version: postgreSQL 9.3

I've written the following procedure:

CREATE OR REPLACE FUNCTION drop_if_exists(table_name name, schema_name name) RETURNS integer AS $$
BEGIN
    IF(SELECT EXISTS(
            SELECT * 
            FROM pg_class c
            JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE relname = table_name AND
                nspname = schema_name
        ) as ex) = true
        THEN
            execute 'DROP TABLE ' || schema_name || '.' || table_name;
            RETURN 1;
        ELSE
            RETURN 0;
    END IF;
END
$$ LANGUAGE plpgsql;

DO $$
BEGIN
    drop_if_exists('ords', 'public'); --ERROR:  syntax error at or near drop_if_exists"
END
$$ LANGUAGE plpgsql;

What's a problem with that code?

Upvotes: 0

Views: 101

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125284

Missing perform is one. Didn't check for other errors.

perform drop_if_exists(...

Upvotes: 1

Related Questions