Aijaz Chauhan Aijaz Chauhan - 5 months ago 8
SQL Question

Need help to review PostgreSQL query

I have following SQL query which I want to use with Postgres

declare
_sql varchar varying(50);
_whereClause varchar varying(50);
_whereClause:='and 1=1'

execute 'SELECT count(*) FROM test t WHERE 1=1' || _whereClause


when i'm trying to execute above query it gives error at varchar in second line.
and even some time for simple command like below

declare _sql varchar varying(50);


It gives error, but I don't know what's wrong with the code.
I have even tried to figure it out with Postgres SQL offcial site and when I tried to copy and run example given over there it's not executing.

I'm new with Postgres and I'm feeling unlucky with the Postgres SQL syntax.

bma bma
Answer

The syntax is incorrect, you should use either VARCHAR(50) or CHARACTER VARYING(50), not VARCHAR VARYING(50). Eg.

DECLARE
    _sql VARCHAR(50);
    _whereClause VARCHAR(50);
Comments