Rustam Issabekov
Rustam Issabekov

Reputation: 3497

printing a value of a variable in postgresql

I have a postgresql function

CREATE OR REPLACE FUNCTION fixMissingFiles() RETURNS VOID AS $$
DECLARE
    deletedContactId integer;
    BEGIN
            SELECT INTO deletedContactId contact_id FROM myContacts WHERE id=206351;

            -- print the value of deletedContactId variable to the console

    END;
$$ LANGUAGE plpgsql;

How can I print the value of the deletedContactId to the console?

Upvotes: 198

Views: 360929

Answers (2)

Denis de Bernardy
Denis de Bernardy

Reputation: 78443

You can raise a notice in Postgres as follows:

RAISE NOTICE 'Value: %', deletedContactId;

Read here for more details.

Upvotes: 421

Ezra Epstein
Ezra Epstein

Reputation: 31

Also, the SELECT INTO you're using looks like PostgreSQL for copying rows into a table. See, for example, the SELECT INTO doc page which state: "SELECT INTO -- define a new table from the results of a query"

In pgplSQL I'm used to seeing it in this order: SELECT INTO ... so your first line should probably be:

        SELECT contact_id INTO deletedContactId FROM myContacts WHERE id=206351;

As described in Executing a Query with a Single-Row Result.

Upvotes: 3

Related Questions