无法绑定多部分标识符

时间:2011-01-24 20:11:40

标签: sql sql-server-2005

我在下面创建了一个查询,但我不明白为什么我收到错误:

  

无法绑定多部分标识符“fi.FacetsImportId”。 (对于每个领域)

INSERT INTO [Elig].[dbo].[ErrorTable]
(
    [SeqId],
    [CodeId],
    [SubjectArea],
    [FieldName],
    [TableName],
    [ErrorValue],
    [ActiveFlag]
)
Select 
    [fi].[FacetsImportId] AS [SeqId],
    @InvalidLength AS [CodeId],
    @SubjectArea AS [SubjectArea], 
    FieldName,
    'Facets.FacetsImport', 
    ErrorValue, 
    '@ActiveFlag'
From (
    select [fi].[FacetsImportId]
    ,Case When len([fi].[FacetsData]) > 200 Then [FacetsData] Else 'CorrectValue' end as [FacetsData]
    ,Case When len([fi].[DataIndicator]) > 4 Then [DataIndicator] Else 'CorrectValue' end as [DataIndicator]
    from Facets.FacetsImport) [fi]
Unpivot (
    ErrorValue for FieldName in ([FacetsData], [DataIndicator] )) as x
where x.ErrorValue <> 'CorrectValue'

2 个答案:

答案 0 :(得分:4)

您的subselect不会为Facets.FacetsImport设置别名。这应该工作,请注意我取出了subselect的别名。看起来不是必要的:

INSERT INTO [Elig].[dbo].[ErrorTable] ( [SeqId], [CodeId], [SubjectArea], [FieldName], [TableName], [ErrorValue], [ActiveFlag])
Select [fi].[FacetsImportId] AS [SeqId],
    @InvalidLength AS [CodeId],
    @SubjectArea AS [SubjectArea],
    FieldName,
    'Facets.FacetsImport', 
    ErrorValue,
    '@ActiveFlag'
From ( 
    select [FacetsImportId],
        Case When len([FacetsData]) > 200 Then [FacetsData] Else 'CorrectValue' end as [FacetsData],
        Case When len([DataIndicator]) > 4 Then [DataIndicator] Else 'CorrectValue' end as [DataIndicator]
    from Facets.FacetsImport
) [fi]
Unpivot (ErrorValue for [et].FieldName in ([FacetsData], [DataIndicator] )) as x
where x.ErrorValue <> 'CorrectValue'

答案 1 :(得分:0)

在子查询之外的AND内需要[fi]别名。在查询中更改一行

    from Facets.FacetsImport [fi]) [fi]

虽然这是FacetsData is Varchar(200) and DataIndicator is Varchar(4),但再更改一次

,Case When len([fi].[DataIndicator]) > 4 Then convert(varchar(200), [DataIndicator]) Else 'CorrectValue' end as [DataIndicator]

然后那就引出了一个问题,这个问题何时才会出现

len([fi].[DataIndicator]) > 4
对于varchar(4)列,

永远返回true吗?