在Microsoft SQL Server 2000上执行以下(完整)SQL查询时:
SELECT B.ARTIFACTTNS, B.ARTIFACTNAME, B.ARTIFACTTYPE, B.INITIALBYTES, B.TIMESTAMP1, B.FILENAME, B.BACKINGCLASS, B.CHARENCODING, B.APPNAME, B.COMPONENTTNS, B.COMPONENTNAME, B.SCAMODULENAME, B.SCACOMPONENTNAME FROM (SELECT DISTINCT A.ARTIFACTTYPE, A.ARTIFACTTNS, A.ARTIFACTNAME FROM (SELECT DISTINCT ARTIFACTTYPE, ARTIFACTTNS, ARTIFACTNAME FROM CUSTPROPERTIES WHERE PNAME = 'AcmeSystemName' AND PVALUE = 'MyRuleGroup' UNION SELECT DISTINCT ARTIFACTTYPE, ARTIFACTTNS, ARTIFACTNAME FROM CUSTPROPERTIES WHERE PNAME = 'AcmeSystemDisplayName' AND PVALUE = 'MyRuleGroup') A, (SELECT DISTINCT ARTIFACTTYPE, ARTIFACTTNS, ARTIFACTNAME FROM CUSTPROPERTIES WHERE PNAME = 'AcmeSystemTargetNameSpace' AND PVALUE = 'http://MyModule') B WHERE A.ARTIFACTTYPE = B.ARTIFACTTYPE AND A.ARTIFACTTNS = B.ARTIFACTTNS AND A.ARTIFACTNAME = B.ARTIFACTNAME) A, BYTESTORE B WHERE (A.ARTIFACTTYPE = 'BRG') AND A.ARTIFACTTYPE = B.ARTIFACTTYPE AND A.ARTIFACTTNS = B.ARTIFACTTNS AND A.ARTIFACTNAME = B.ARTIFACTNAME ORDER BY ARTIFACTTYPE, ARTIFACTTNS, ARTIFACTNAME
我得到以下异常:
java.sql.SQLException: [Acme][SQLServer JDBC Driver][SQLServer] Ambiguous column name 'ARTIFACTTYPE'.
我在这里做错了什么,如何纠正呢?
由于ARTIFACTTYPE
可指A.ARTIFACTTYPE
或B.ARTIFACTTYPE
与服务器需要知道你想要哪一个,只是将其更改为A.ARTIFACTTYPE
,你应该在这种情况下是好的.
为了澄清,您需要在列名称不明确时指定别名前缀.始终使用别名前缀是不错的做法,因为它可以在您阅读查询时清楚显示哪些列来自哪些表,并消除了类似这样的问题.
有人可能想知道为什么当它们都引用同一个表中的同一列时,你需要区分两列中的哪一列.答案是,当您将表连接到自身时,A.column和B.column中的值可能会根据连接条件而有所不同(例如,可能是外连接的情况,其中一列中的值可能是空值).