Azure SQL:外部表与外部源上存在的基础独立表或分片表中列的数据类型不同

时间:2018-11-27 05:36:06

标签: sql azure elasticsearch azure-sql-database

当我运行Azure SQL数据库中的此存储过程时:

select c.ID, c.ContractIDText, c.ContractID, c.TpwrId, c.ContractType as ContractType, c.Status as StatusName, c.Grantee as GranteeName,
           c.Contact as ContactName, c.EffectiveDate, c.OptionToExtend, c.OptionExercised, t.ID as Term,
           RANK() OVER (PARTITION BY c.ContractID ORDER BY t.Sequence) as Sequence,
CASE WHEN t.TimePeriod is null then e.DueDate
     WHEN t.TimePeriodUnits = 'Years' then DATEADD(Year, -t.TimePeriod, e.DueDate)
     WHEN t.TimePeriodUnits = 'Months' then DATEADD(Month, -t.TimePeriod, e.DueDate)
     WHEN t.TimePeriodUnits = 'Days' then DATEADD(Day, -t.TimePeriod, e.DueDate)
     WHEN t.TimePeriodUnits = 'Weeks' then DATEADD(Day, -(t.TimePeriod*7), e.DueDate)
     ELSE e.DueDate end as PerceivedEffectiveDate,
          t.TermType as TermTypeName, t.TimePeriod, t.TimePeriodUnits as TermUnitTypeName, e.DueDate as ExpireDate,
CAST(t.TimePeriod AS NVARCHAR) + ' ' + t.TimePeriodUnits as TimeAndUnits,
DATEDIFF(Day, CURRENT_TIMESTAMP, e.DueDate) as DaysAway, assetname, County,[Block], Section, ISNULL(Grantor, 'Texas Pacific Land Trust') Grantor
from [dbo].[Contract] c inner join
     [dbo].[Term] t on t.ContractID = c.ID inner join
     [dbo].[Resolution] e on e.TermID = t.ID and (e.ResolutionDescription is null or e.ResolutionDescription = '' or e.ResolutionDate is null or e.ResolutionType is null) left outer join
     [dbo].[Contracts_Tracts] ct on ct.contractid = c.id left outer join
     [dbo].[Tract] tr on tr.id = ct.tractid left outer join
     [dbo].[Land_Surveys] ls on ls.id = tr.landsurveyid left outer join
     [dbo].[Asset] a on a.id = ls.assetid
     where t.TermType <> 'Perpetual' and 
     (
                  c.ContractType in ('Water: Temporary Produced Water Pipeline Permit', 'Water: Temporary Fresh Water Pipeline Permit', 'Water: Salt Water Disposal Load Station Site', 'Water: Salt Water Disposal Letter Agreement','Water: Salt Water Disposal & Facility', 'Water: Salt Water Disposal Facility', 'Water: Salt Water Disposal', 'Water: Salt Water Disposal & Facility', 'Water: Temporary Pipeline Right-Of-Way and Easement', 'Water: Produced Water Pipeline Easement','Multi-Use Pipeline Easement','Water: Temporary Water Pipeline Permit'))

我得到:

The data type of the column 'TpwrId' in the external table is different than the column's data type in the underlying standalone or sharded table present on the external source.

唯一可以改变的事情是,我可以想到的是将该数据库移动到一个弹性池中。我认为这与跨数据库查询有关。

ContractIdText(如果删除TpwrId,将会出现相同的错误)是一个利用TpwrId的计算列。 ContractIdText / TpwrId位于数据库A中,而存储过程位于数据库B中。它们都在弹性池中。错误消息对我来说有些奇怪。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

SQL Azure中的外部表功能允许/要求您定义远程表的本地架构。 (这与动态执行此操作的SQL Server中的链接服务器不同,但这也更好,因为它使优化器在外部表上具有统计信息,从而可以显着提高计划质量)。问题是本地和远程之间的绑定层看到了不同的类型。不幸的是,错误消息没有告诉您什么类型(我将要求团队改进该错误消息)。目前还不清楚哪个表是外部表(我猜是Contract)。我建议您退后一步,尝试仅运行“从合同中选择*”,看看返回的结果是否没有错误。如果那也简化了查询错误,那么您应该去看看远程端并尝试运行任何源查询,并从中创建一个本地表(从()中选择*到mylocaltable中)。然后,您可以查看每侧(本地和远程)的sys.columns表,以查看类型是否确实相同或不同。如果它们不同,请尝试调整外部表定义以使其与远程端对齐。

(弹性池不应真正引起此问题,也不太可能是原因)。

希望能帮助您取得进步。 康纳 SQL架构师