Question

I am using BDS 2006 with PostgreSQL for my application.
I have the following code consisting of the query for finding the Primary key values inside my Table.

 Query.SQL.Clear;
 Query.SQL.Add('SELECT pg_attribute.attname,format_type(pg_attribute.atttypid, pg_attribute.atttypmod)FROM pg_index, pg_class, pg_attribute WHERE pg_class.oid = '+#39+'tablename'+#39+' ::regclass AND indrelid = pg_class.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = any(pg_index.indkey)AND indisprimary');
 Query.Open;

I get an syntax error message

 General SQL error.  
 ERROR: syntax error at or near ":";  

I tried using #58 in place of : but results the same.
The following query works fine inside my PostgreSQL.

Any views on how do i get it working

Was it helpful?

Solution

1) If you put BDE tag, then I am expecting that you used BDE + BDE ODBC SQLLink + PgSQL ODBC driver. BDE components expect ':' as param marker, and '::' as an escape sequence which is translated into ':'. You have two basic options:

  • set TQuery.ParamCheck to False and fill in Params collection by hands;
  • double every one ':', which is not a parameter marker. So, it will be '::::'.

2) You can use 3d party libraries, like AnyDAC, which are understanding what PgSQL '::' means. So, they will not recognize '::' as a parameter marker.

OTHER TIPS

Try putting table name between double quotes if it's name contains upper case chars, i.e. "MySuperDupleTable", postgres changes it's name to lowercase if it's not between double quotes.

If that doesn't fix your problem, you might also want to try ::::regclass I remember a couple of years ago, we were using some Delphi components that required doubling "::".

Hope this helps.

You could trying switching to CAST instead of the PostgreSQL-specific :::

Query.SQL.Add('SELECT pg_attribute.attname,format_type(pg_attribute.atttypid, pg_attribute.atttypmod)FROM pg_index, pg_class, pg_attribute WHERE pg_class.oid = CAST('+#39+'tablename'+#39+' AS regclass) AND indrelid = pg_class.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = any(pg_index.indkey) AND indisprimary');

Perhaps something in Delphi wants to use colons for named placeholders.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top