user1243746
user1243746

Reputation:

Syntax error at end of input in PostgreSQL

I have used the next SQL statement in both MySQL and PostgreSQL:

db.Query(`SELECT COUNT(*) as N FROM email WHERE address = ?`, email)

But it fails in PostgreSQL with this error:

pq: F:"scan.l" M:"syntax error at end of input" S:"ERROR" C:"42601" P:"50" R:"scanner_yyerror" L:"993"

What's the problem? The error messages in PostgreSQL are very cryptic.

Upvotes: 39

Views: 159182

Answers (6)

david.barkhuizen
david.barkhuizen

Reputation: 5645

One cause for this error can be that you are missing a terminating semi-colon before your final END statement, for example

return True
END;
$$
LANGUAGE plpgsql;

which should be

return True;
END;
$$
LANGUAGE plpgsql;

Upvotes: 0

Randall
Randall

Reputation: 3024

Another possibility - check for any missing END directives for IF, LOOP, etc.

I'd been working in a loop so long, that I didn't realize I never put in an END LOOP;.

The message that Postgres displays:

ERROR:  syntax error at end of input
LINE 123: $$;

is terribly unhelpful at telling you if some construct was never closed.

Upvotes: 1

calamari
calamari

Reputation: 337

In my case, it was due to using a -- line comment where the program that was responsible for interacting with the database read in the multiple lines of my query all as one giant line. This meant that the line comment corrupted the remainder of the query. The fix was to use a /* block comment */ instead.

Upvotes: 6

refrazul
refrazul

Reputation: 161

In golang for queries we have use

  • MySQL uses the ? variant
  • PostgreSQL uses an enumerated $1, $2, etc bindvar syntax
  • SQLite accepts both ? and $1 syntax
  • Oracle uses a :name syntax

Upvotes: 16

intgr
intgr

Reputation: 20456

You haven't provided any details about the language/environment, but I'll try a wild guess anyway:

MySQL's prepared statements natively use ? as the parameter placeholder, but PostgreSQL uses $1, $2 etc. Try replacing the ? with $1 and see if it works:

WHERE address = $1

The error messages in PostgreSQL are very cryptic.

In general, I've found that Postgres error messages are better than competing products (ahem, MySQL and especially Oracle), but in this instance you've managed to confuse the parser beyond sanity. :)

Upvotes: 98

simonmenke
simonmenke

Reputation: 2880

You are using Go right?

try:

db.Query(`SELECT COUNT(*) as N FROM email WHERE address = $1`, email)

Upvotes: 8

Related Questions