Inner Join - SQL Server

1

Folks, I need somebody's help to understand the mistake you're making. I have two tables as shown in the code, and in one of the tables the "CodePlane" column has information. I need to put the codes related to the Beneficiaries. But it is giving the error:

  

Syntax "Incorrect Syntax near the keyword 'FROM'"

    SELECT [SemPlano].[NomeBenefTitular] AS [BenfTitularS], 
            [ComPlano].[NomeBenefTitular] AS [BenfTitularC],
            [SemPlano].[NomeUsuario]      AS [NomeUsuarioS], 
            [ComPlano].[NomeUsuario]      AS [NomeUsuarioC],
            [SemPlano].[CodigoPlano] AS [CodPlanoS], 
            [ComPlano].[CodigoPlano] AS [CodPlanoC],
       FROM [TabelaUm]   AS [SemPlano]
 INNER JOIN [TabelaDois] AS [ComPlano] ON [BenfTitularS] = [BenfTitularC]
    
asked by anonymous 22.02.2018 / 15:54

2 answers

1

The correct one would be:

SELECT 
    [SemPlano].[NomeBenefTitular] AS [BenfTitularS], 
    [ComPlano].[NomeBenefTitular] AS [BenfTitularC],
    [SemPlano].[NomeUsuario]      AS [NomeUsuarioS], 
    [ComPlano].[NomeUsuario]      AS [NomeUsuarioC],
    [SemPlano].[CodigoPlano] AS [CodPlanoS], 
    [ComPlano].[CodigoPlano] AS [CodPlanoC]
FROM 
    [TabelaUm] AS [SemPlano]
INNER JOIN 
    [TabelaDois] AS [ComPlano] ON [SemPlano].[NomeBenefTitular] = [ComPlano].[NomeBenefTitular]
    
22.02.2018 / 16:09
7

You forgot to remove the comma before FROM , in this line [ComPlano].[CodigoPlano] AS [CodPlanoC]

And as per the Paulo RF Amorim comment: Using the alias created for the fields in INNER JOIN causes error

   SELECT [SemPlano].[NomeBenefTitular] AS [BenfTitularS], 
            [ComPlano].[NomeBenefTitular] AS [BenfTitularC],
            [SemPlano].[NomeUsuario]      AS [NomeUsuarioS], 
            [ComPlano].[NomeUsuario]      AS [NomeUsuarioC],
            [SemPlano].[CodigoPlano] AS [CodPlanoS], 
            [ComPlano].[CodigoPlano] AS [CodPlanoC]
       FROM [TabelaUm]   AS [SemPlano]
INNER JOIN [TabelaDois] AS [ComPlano] ON [SemPlano].[NomeBenefTitular] = [ComPlano].[NomeBenefTitular]
    
22.02.2018 / 16:05