How to make table name dynamic Postgresql

Viewed 20

This is my query which join the result of query performed in postgresql from one database with another query from another database the thing is the table's name in the seconde database depend on the result of the query that target the first database. Here is the query:

select 
    gdr.id , 
    attachment_id ,
    state ,
    gdr.user_id ,
    gdr.document_id ,
    internal_code, 
    ia."name" , 
    gdd.id,tb2.col_3095
from 
    gediso_document_record as gdr
inner join ir_attachment ia on ia.id = gdr.attachment_id 
inner join gediso_document_descriptor gdd on gdd.document_id  = gdr.document_id 
inner join (
    select * from dblink('dbname=gediso_document','select id,record_id,col_3095 from doc_id_'||gdr.id)
     AS     tb2(id int,record_id int,col_3095 varchar)
    )AS tb2 ON tb2.record_id = gdr.id
where 
    gdr.user_id =2031
limit 100

The problem resides in this line { select * from dblink('dbname=gediso_document','select id,record_id,col_3095 from doc_id_'||gdr.id) }

It won't allow me to use gdr.id

Can anyone help me please ? Thank you in advance.

0 Answers
Related