简体   繁体   中英

error with a sql query because of ambiguous column name

I'm trying to create a sql query, but there is this error:

Ambiguous column name 'description'.

Its because this column occurs in both tables.

if I remove the description from the query, it works. I tried to rename the description-field "AS description_pointer", but the error still occurs.

SELECT TOP 1000 [activityid]
      ,[activitytypecodename]
      ,[subject]
      ,[regardingobjectid]
      ,[contactid] 
      ,[new_crmid]
      ,[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as I
   Left JOIN  [crmtestext_MSCRM].[dbo].[FilteredContact]
  ON I.[regardingobjectid] = [crmtestext_MSCRM].[dbo].[FilteredContact].[contactid]
  WHERE new_crmid not like '%Null%' AND activitytypecodename like '%E-mail%'

Both tables coming into play in the query have a column named description . You RDBMS cannot guess which column table you actually want.

You need to prefix the column name with the table name (or table alias) to disambiguate it.

Bottom line, it is a good practice to always prefix column names with table names or aliases as soon as several tables come into play in a query. This avoids the issue that you are seeing here and make the queries easier to understand for the poor souls that have no knowledge of the underlying schema.

Here is an updated version of your query with table aliases and column prefixes. Obviously you need to review each column to put the correct alias:

SELECT TOP 1000 
       i.[activityid]
      ,i.[activitytypecodename]
      ,i.[subject]
      ,c.[regardingobjectid]
      ,c.[contactid] 
      ,c.[new_crmid]
      ,c.[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as i
   Left JOIN  [crmtestext_MSCRM].[dbo].[FilteredContact] as c
  ON i.[regardingobjectid] = c.[contactid]
  WHERE i.new_crmid not like '%Null%' AND i.activitytypecodename like '%E-mail%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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