3

I have a query string in variable

suppose Select Now(); I want to execute it same like SQL do. I use

Execute 'Select Now()';

But It throws an exception

prepared statement "SELECT now()" does not exist

and not allow me to execute it.

Is there any way to do that?

Pooja-G
  • 518
  • 4
  • 20
  • Please show us your complete function –  Dec 01 '16 at 10:25
  • This is simplest one I tried. I will make function if it execute. – Pooja-G Dec 01 '16 at 10:26
  • you can't use `execute` in SQL, this is a PL/pgSQL statement. You need a function (or a `do` block) –  Dec 01 '16 at 10:30
  • Possible duplicate of [dynamic sql query in postgres](http://stackoverflow.com/questions/12780275/dynamic-sql-query-in-postgres) – JimmyB Dec 01 '16 at 10:33

2 Answers2

1

You mess plpgsql EXECUTE:

t=# do $$ begin execute 'select now()'; end; $$;
DO

and SQLEXECUTE.

t=# prepare example as select now();
PREPARE
t=# execute example;
              now
-------------------------------
 2016-12-01 10:30:15.782433+00
(1 row)
Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
-1

'Select Now()' Run like this in pgadmin

Marlon Abeykoon
  • 11,927
  • 4
  • 54
  • 75