*注意:这与“可能的重复”不一样。在这里,对于在SELECT语句中返回的每个记录,表名将是不同的。所以我不能“设置”像set @tableName = 'whatever'
这样的变量。如何在连接中使用select语句中返回的表名?
这是我的SQL - 看看我最后的内部连接。表中的e.Name EmailSendDefintion是我需要加入的表的名称。所以,这是一种动态的,我知道,但我如何加入存储在另一个表中的字段中的表?
select top 5000
x.HL_ACCT_ID as 'HL_ACCT_ID',
x.SALE_CODE as 'SALE_CODE',
s.SubscriberKey as 'EmailAddress',
o.EventDate as 'Opened',
c.EventDate as 'Clicked',
b.EventDate as 'Bounced'
from c100._sent s with (nolock)
inner join c100._job j with (nolock) on s.jobid = j.jobid
inner join emailsenddefinition e with (nolock) on e.customerkey = j.emailsenddefinition
left join c100._open o with (nolock) on o.jobid = s.jobid and o.subscriberkey = s.subscriberkey
left join c100._click c with (nolock) on c.jobid = s.jobid and c.subscriberkey = s.subscriberkey
left join c100._bounce b with (nolock) on b.jobid = s.jobid and b.subscriberkey = s.subscriberkey
inner join c100.[e.name] x with (nolock) on x.EmailAddress = s.SubscriberKey
where e.clientid = 100
的可能的复制[如何创建内部CURSOR动态SQL查询(http://stackoverflow.com/questions/16394720/how -to-create-dynamic-sql-queries-cursor-cursor) –
我是seconding @Juan。你想要的东西好像是一个动态的sql来填充一个sql命令数组,然后执行它。 –