*注意:這與「可能的重複」不一樣。在這裏,對於在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命令數組,然後執行它。 –