Inner Join - SQL Server

Asked

Viewed 85 times

1

Guys, I need someone’s help to understand the mistake you’re making. I have two tables as the code shows, and in one of the tables the column "Code" is with information. I need to enter the codes related to the Beneficiaries. But you’re making a mistake:

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]
  • Using the alias created for the fields in INNER JOIN causes error, in my reply I show how the search should be done correctly.

2 answers

7

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

And as commented by Paulo R. F. Amorim : Use the alias created for the 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]
  • Using the alias created for the fields in INNER JOIN causes error, in my reply I show how the search should be done correctly

  • I had not attempted it myself.. I altered my answer to make it correct

1


The right thing 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]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.