How can i use variables in a query of a foreign table?
I could ALTER the option (query) of the foreign tabel and add the variable into the where clause but i am concerned that this locks the foreign table so no querys can be performed on it.
I also thought about writing a procedure that dynamicly creates the whole foreign table but i dont want to create and delete the same foreign tabele with just some minor changes in its query.
Is there a better way of accomplishing my goal?
I would like to call a function or procedure with the parameters "StartDate" / "EndDate" and get the results to the following
CREATE FOREIGN TABLE "A"."B.test"(
"Col1" character varying NULL,
"Col2" numeric NULL
)
SERVER "EXT"
OPTIONS(query '
SELECT ExternalCol1, ExternalCol2
FROM [DB1].[dbo].[Table1] A
WHERE A.ProdDate >= ''2000-01-01'' -- variable, something like StartDate
AND A.ProdDate < ''2001-01-01'' -- variable something like EndDate
');
ALTER FOREIGN TABLE "A"."B.test"
OWNER TO postgres;
2
Answers
I ended up writing a function that creates the foreign table dynamicly and sets the parameter values of the query by using FORMAT. I added a random hash through the function to the foreign table so that the same foreign table could be used by differnt users. At the end of the function i delete the foreign table by its "random" id.
Since tds_fdw can push down
WHERE
conditions to the remote side, the solution would be to define the foreign table withtable_name
rather thanquery
and set thematch_column_names
option on the foreign table. Then you can simply add theWHERE
conditions to the query on the foreign table.