HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlModerate

How to print to console in SQL?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sqlconsoleprinthow

Problem

I'm using PostgreSQL And I want to print a message to the console.

If I use plpythonu I use plpy.notice
If I use plpgsql I use raise notice

but how do I print when the function is pure SQL?
using SELECT 'string' isn't helping me as it print the string in a column and if the message is located in the middle of the code it doesn't show it.
I want something like raise notice / plpy.notice for SQL.

CREATE OR REPLACE FUNCTION A()
  RETURNS VOID AS
$BODY$ 
                how do i print 'hello world' here?
 $BODY$
  LANGUAGE sql VOLATILE


if it was a plpgsql I would do:

CREATE OR REPLACE FUNCTION A()
  RETURNS VOID AS
$BODY$ 
        Raise Notice 'hello world'
 $BODY$
  LANGUAGE plpgsql VOLATILE


I'm looking for the equivalent in LANGUAGE SQL

Solution

With psql you could \echo text (or \qecho if using \o), for example executing with psql -f a file with contents:

select 'Straight';
\echo BETWEEN
select 'the lines.';


outputs:

?column? 
----------
 Straight
(1 row)

BETWEEN
  ?column?  
------------
 the lines.
(1 row)

Code Snippets

select 'Straight';
\echo BETWEEN
select 'the lines.';
?column? 
----------
 Straight
(1 row)

BETWEEN
  ?column?  
------------
 the lines.
(1 row)

Context

StackExchange Database Administrators Q#106144, answer score: 19

Revisions (0)

No revisions yet.