简体   繁体   English

错误:列名不明确

[英]Error: Ambiguous column name

Why am I getting this error? 为什么会出现此错误?

Msg 209, Level 16, State 1, Line 94 信息209,第16级,州1,第94行
Ambiguous column name 'New Value'. 列名不明确,“新值”。

Query: 查询:

SELECT  
    aho2.[Control Number] AS [Control Number],
    STUFF((SELECT '; ' + [Old Value] as [text()] 
           FROM #AuditHistoryOutput aho1 
           WHERE [aho1].[Control Number] = [aho2].[Control Number] 
           FOR XML PATH('')), 1, 1, '') [Unset Choice Value],
    STUFF((SELECT '; ' + [New Value] as [text()] 
           FROM #AuditHistoryOutput aho2, #AuditHistoryOutput aho1
           WHERE [aho1].[Control Number] = [aho2].[Control Number] 
           FOR XML PATH('')), 1, 1, '') [Set Choice Value]
FROM 
    #AuditHistoryOutput aho2

you use the aho2 table alias twice change one of the references to something else 您使用aho2表别名两次将其中一个引用更改为其他内容

it looks like you are doing string conactenation of rows to a semi colon delimited string. 看起来您正在将行的字符串转换为半冒号分隔的字符串。 But if you look in your second stuff statement you use the table alias aho2 and then you use it again in the last table reference. 但是,如果您在第二个填充语句中查找,则使用表别名aho2,然后在最后一个表引用中再次使用它。 So one of the 2 references need to change otherwise sql-server doesn't know which one you are referencing. 因此,这2个引用之一需要更改,否则sql-server不知道您引用的是哪个。

But now that I look deeper you also have an issue in your second select statement that you have a cross join specified due to implicit join sytax and specifying the table twice. 但是,现在看来,您在第二个select语句中还存在一个问题,即由于隐式连接语法和指定表两次而指定了交叉连接。 My guess is you don't want that either here is one way (a guess) that might get you want you want but if not you should update your question with schema, example data, and desired result so that we can more effectively assist you. 我的猜测是您不希望这是一种可能会得到您想要的方法(猜测),但是如果没有,您应该使用架构,示例数据和所需结果来更新您的问题,以便我们可以更有效地为您提供帮助。

SELECT 
    aho3.[Control Number] AS [Control Number]
    ,STUFF(
       (SELECT '; '+[Old Value] as [text()]
       FROM #AuditHistoryOutput aho1
       WHERE [aho1].[Control Number] = aho3.[Control Number]
       FOR XML PATH(''))
       , 1, 1, '') [Unset Choice Value]
    ,STUFF(
       (SELECT '; '+[New Value] as [text()]
       FROM #AuditHistoryOutput aho2
       WHERE [aho2].[Control Number] = aho3.[Control Number]
       FOR XML PATH(''))
       , 1, 1, '') [Set Choice Value]
FROM #AuditHistoryOutput aho3

"Ambiguous column name" means you have two tables with the same name and you don't specify which table you want. “模棱两可的列名”表示您有两个名称相同的表,而未指定所需的表。 So [aho1].[New Value] or [aho2].[New Value] in your select list instead of [New Value] will get rid of this error. 因此,您选择列表中的[aho1].[New Value][aho2].[New Value]代替了[New Value]将消除此错误。 Can't promise your query will work. 无法保证您的查询将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM