printing a value of a variable in postgresql

SqlPostgresql

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

You can raise a notice in Postgres as follows:

raise notice 'Value: %', deletedContactId;

Read here

Solution 2 - Sql

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRustam IssabekovView Question on Stackoverflow
Solution 1 - SqlDenis de BernardyView Answer on Stackoverflow
Solution 2 - SqlEzra EpsteinView Answer on Stackoverflow