简体   繁体   中英

Incorrect syntax near '.' while using Coalesce

I am trying to join three tables, I joined two successfully.

This Worked (Two Tables)

SELECT  ImportExportBadFile.ID, ImportExportSettingID,
    ImportExportBadFile.UserID,
    Coalesce(UserName,'') UserName  
    FROM
    ImportExportBadFile 
    LEFT OUTER JOIN Users HGSQLUsers ON ImportExportBadFile.UserID = HGSQLUsers.ID
    ORDER BY  ImportExportBadFile.DateTimeStamp DESC
  • UserName belongs to Users Table

Now, when I tried to add another Table:

SELECT  ImportExportBadFile.ID, ImportExportSettingID,
    SETT.Name,
    ImportExportBadFile.UserID,
    Coalesce(UserName,'') HGSQLUsers.UserName  
    FROM
    ImportExportBadFile 
    LEFT OUTER JOIN Users HGSQLUsers ON ImportExportBadFile.UserID = HGSQLUsers.ID
    INNER JOIN ImportExportSettings SETT ON ImportExportBadFile.ImportExportSettingID = SETT.ID  
    ORDER BY  ImportExportBadFile.DateTimeStamp DESC

It gives Error : "Incorrect syntax near '.'." at line : "Coalesce(UserName,'') HGSQLUsers.UserName"

If I write only UserName It gives

"Ambiguous column name 'UserName'."

Help.

Probably UserName column exists in both tables, you need to specify from which table do want to take UserName as below

SELECT  ImportExportBadFile.ID, ImportExportSettingID,
    SETT.Name,
    ImportExportBadFile.UserID,
    Coalesce( HGSQLUsers.UserName  ,'') as "HGSQLUsers.UserName"
    FROM
    ImportExportBadFile 
    LEFT OUTER JOIN Users HGSQLUsers ON ImportExportBadFile.UserID = HGSQLUsers.ID
    INNER JOIN ImportExportSettings SETT ON ImportExportBadFile.ImportExportSettingID = SETT.ID
ORDER BY  ImportExportBadFile.DateTimeStamp DESC

Error "Incorrect syntax near '.'." at line : "Incorrect syntax near '.'." at line : comes from alias HGSQLUsers.UserName you can use it but in quotas.

I also recommend you to use aliases for every table as below - is more readable for me

SELECT  I.ID, I.ImportExportSettingID,
        SETT.Name,
        I.UserID,
       Coalesce( HGSQLUsers .UserName  ,'') as "HGSQLUsers.UserName"
FROM ImportExportBadFile I
LEFT OUTER JOIN Users HGSQLUsers ON I.UserID = HGSQLUsers.ID
INNER JOIN ImportExportSettings SETT ON I.ImportExportSettingID = SETT.ID  
ORDER BY  I.DateTimeStamp DESC

Actually this worked for me in trial and error:

SELECT  ImportExportBadFile.ID, ImportExportSettingID,
    SETT.Name,
    ImportExportBadFile.UserID,
    Coalesce(HGSQLUsers.UserName,'')
    FROM
    ImportExportBadFile 
    INNER JOIN ImportExportSettings SETT ON ImportExportBadFile.ImportExportSettingID = SETT.ID  
    LEFT OUTER JOIN Users HGSQLUsers ON ImportExportBadFile.UserID = HGSQLUsers.ID
    ORDER BY  ImportExportBadFile.DateTimeStamp DESC

but @Parado's Answer is more specific and explaining.

Thank you.

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