tds-fdw / tds_fdw

A PostgreSQL foreign data wrapper to connect to TDS databases (Sybase and Microsoft SQL Server)
Other
381 stars 102 forks source link

Error when foreign table is queried from a nested query #255

Open isurunix opened 4 years ago

isurunix commented 4 years ago

I'm getting below error when trying to query a foreign table inside a nested query. How ever I can directly query the foreign table without any issue.

[2020-07-28 14:04:25] [HV00L] ERROR: DB-Library error: DB #: 20018, DB Msg: General SQL Server error: Check messages from the SQL Server, OS #: -1, OS Msg: , Level: 15
[2020-07-28 14:04:25] [00000] DB-Library notice: Msg #: 156, Msg state: 1, Msg: Incorrect syntax near the keyword 'as'., Server: DB1DBSSVR1, Process: , Line: 1, Level: 15

The query I'm running is something similar to below sample query.

SELECT COL1, COL2, COL3 FROM (
    SELECT a.COL1, b.COL2, c.COL3 FROM 
        PSQL_TABLE_1 a JOIN PSQL_TABLE_2 b ON a.ID = b.ID
        JOIN FOREIGN_TABLE f on b.ID = f.CODE WHERE a.COL5 = 'S'
    UNION ALL
    SELECT a.COL1, b.COL2, c.COL3 FROM 
        PSQL_TABLE_1 a JOIN PSQL_TABLE_3 b ON a.ID = b.ID
        JOIN FOREIGN_TABLE f on b.ID = f.CODE WHERE a.COL5 = 'S'
) 
DERIVED WHERE DERIVED.SEQ_NO = 'xxxx'