Unterminated dollar-quoted string at or near "$$

16.8k Views Asked by At

I'm trying to declare some variables using DBeaver and keep hitting this error.

Unterminated dollar-quoted string at or near "$$

 DO $$
 DECLARE A integer; B integer;

BEGIN   
END$$;

Any ideas?

4

There are 4 best solutions below

0
user1158745 On BEST ANSWER

DBeaver was the issue. Switched to PGAdmin and no more problems.

0
Craig Ringer On

The syntax posted is fine. Your problem is that the client application or driver is mangling the query, probably because it doesn't understand dollar-quoting.

It might be trying to split it into separate statements on semicolons, running:

  • DO $$ DECLARE A integer;
  • B integer;
  • BEGIN END$$;

as three separate statements. This would result in the reported error, e.g.

$ psql -c 'DO $$ DECLARE A integer;'
ERROR:  unterminated dollar-quoted string at or near "$$ DECLARE A integer;"
LINE 1: DO $$ DECLARE A integer;
           ^

This is why you must specify your client driver/application when asking questions.


Another possibility with some clients is that it might treat $ as an escaped query-parameter placeholder and replace it with a single $ or try to substitute it for a server-side placeholder like $1. That's not what's happening here, though.

1
Hobotron On

As of DBeaver 6, you can execute the script with ALT-X (on Windows), which does not attempt to do variable capture/interpolation involving dollar signs.

0
Joel MC On

DBeaver also gives this error when there is a SQL syntax error in the script. In my case, it was a pair of mismatched parenthesis in a select calculated column.