我是SQL Server的人,我需要在Postgres中编写一些动态SQL。这就是我需要的。动态SQL将取决于此查询生成的整数:
SELECT local_channel_id
FROM d_channels dc
INNER JOIN channel c ON c.id = dc.channel_id
AND c.name LIKE '%__Achv'
使用这个,我需要在下面的查询中构建并执行一个select和后续的union选择,用下面的{X}替换上面查询产生的值(4个地方):
SELECT
dmc.message_id,
dmm.received_date,
dmm.server_id,
dc.channel_id,
dmcm."SOURCE",
dmcm."TYPE",
dmm.status,
dmc.content
FROM
d_mc{X} dmc
INNER JOIN
d_mm{X} dmm ON dmc.message_id = dmm.message_id
INNER JOIN
d_channels dc ON dc.local_channel_id = {X}
INNER JOIN
d_mcm{X} dmcm ON dmcm.message_id = dmc.message_id
AND dmcm.metadata_id = 0
WHERE
dmm.connector_name = 'Source'
AND dmc.content_type = 1 --Raw
AND date(dmm.received_date) + interval '7' < now()
有人可以帮忙吗?对于Postgres来说,我真的很无能为力。