SQL Query从各种数据库中获取数据

时间:2017-12-07 05:43:29

标签: sql-server openquery tempdb

我编写了以下查询来从不同的数据库中提取数据。我创建了两个临时表来从两个不同的数据库中提取数据,最后从原始数据库中选择一个select语句来连接所有表。我的查询正在执行但没有获取任何数据。(报告为空白)。我试着分别执行两个临时表。它提供了正确的数据。但是当我执行整个查询时,结果是空白的。以下是查询。请帮忙。

"set fmtonly off

use GODSDB

IF object_id('tempdb..#CISIS_Call_Log') IS NOT NULL DROP TABLE #CISIS_Call_Log
select * 
into #CISIS_Call_Log
from OPENQUERY (CSISDB,
'select
ccl.ContractOID,
ccl.db_insertdate,
ccl.ContractCallLogStatusIdentifier,
ccl.db_UpdateDate,
ccp.ContractCallLogPurposeOID,
ccp.ContractCallLogPurposeIdentifier,
ccp.Description
from csisdb.dbo.ContractCallLog CCL
inner join csisdb.dbo.ContractCallLogPurpose CCP on ccl.ContractCallLogPurposeIdentifier = ccp.ContractCallLogPurposeIdentifier
where JurisdictionShortIdentifier = ''ON'' 
AND  ContractCallLogStatusIdentifier IN (''DNR'', ''NR'')
')

IF object_id('tempdb..#CMS_Campaign') IS NOT NULL DROP TABLE #CMS_Campaign
select * 
into #CMS_Campaign
from OPENQUERY (BA_GBASSTOCMS, '
Select
SystemSourceIdentifier,
ContractOID,
OfferSentDate,
CampaignOfferTypeIdentifier,
CampaignContractStatusIdentifier,
CampaignContractStatusUpdateDate,
DeclineDate,
CampaignOfferOID,
CampaignOID,
CampaignStartDate,
CampaignEndDate,
Jurisdiction,
CampaignDescription
from CMS.dbo.vw_CampaignInfo
where Jurisdiction = ''ON''
and CampaignOfferTypeIdentifier = ''REN''
')

select mp.CommodityTypeIdentifier as Commodity
,c.RtlrContractIdentifier as ContractID
,cs.ContractStatusIdentifier as ContractStatus
,c.SigningDate
,cf.StartDate as FlowStartDate
,cf.EndDate as FlowEndDate
,datediff(day, getdate(), c.RenewalDate) as RemainingDays
,c.RenewalDate
,l.ContractCallLogStatusIdentifier as CallLogType
,Substring (l.Description, 1, 20) as CallPurpose
,l.db_insertDate as CallLogDate
,cms.CampaignOfferOID as OfferID
,cms.CampaignContractStatusIdentifier as OfferStatus
,cms.CampaignContractStatusUpdateDate as StatusChangeDate
,cms.DeclineDate
from Contract c
inner join contractstate cs on cs.contractoid = c.ContractOID
and cs.ContractStatusIdentifier in ('ERA', 'FLW')
and datediff(day, getdate(), c.RenewalDate) > 60
inner join SiteIdentification si on si.SiteOID = c.SiteOID
inner join MarketParticipant mp on mp.MarketParticipantOID = si.MarketParticipantOID
inner join Market m on m.MarketOID = mp.MarketOID
inner join Jurisdiction j on j.JurisdictionOID = m.JurisdictionOID
and j.CountryCode = 'CA'
and j.ProvinceOrStateCode = 'ON'
inner join ContractFlow cf on cf.ContractOID = c.ContractOID
inner join #CISIS_Call_Log l on convert(varchar(15), l.ContractOID) = c.RtlrContractIdentifier
inner join #CMS_Campaign cms on convert(varchar(15), cms.ContractOID) = c.RtlrContractIdentifier


set fmtonly on"

1 个答案:

答案 0 :(得分:0)

IF 验证每个临时表中的数据,然后:

尝试使用更小,更简单的查询来测试临时表。也可以使用LEFT连接尝试它们,例如:

select
      c.RtlrContractIdentifier as ContractID
    , c.SigningDate
    , datediff(day, getdate(), c.RenewalDate) as RemainingDays
    , c.RenewalDate
    , l.ContractCallLogStatusIdentifier as CallLogType
    , Substring (l.Description, 1, 20) as CallPurpose
    , l.db_insertDate as CallLogDate
    , cms.CampaignOfferOID as OfferID
    , cms.CampaignContractStatusIdentifier as OfferStatus
    , cms.CampaignContractStatusUpdateDate as StatusChangeDate
    , cms.DeclineDate
from Contract c
LEFT join #CISIS_Call_Log l on convert(varchar(15), l.ContractOID) = c.RtlrContractIdentifier
LEFT join #CMS_Campaign cms on convert(varchar(15), cms.ContractOID) = c.RtlrContractIdentifier

这会返回数据吗?它是否从两个连接表中返回数据?

如果临时表都没有返回数据,则需要更改这些连接条件。

如果两个临时表确实从该查询返回数据,则尝试INNER连接。如果仍然有效,则添加更多连接(一次一个),直到找到导致整体故障的连接。

如果没有每个表的数据,我们就无法确定NULL结果的确切原因。只有你可以,所以你需要一步一步地解决问题。