Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
As of right now I can get 3 tables to merge using an inner join, but I can not seem to find a way to get 4+ tables inner joined.

Here is my code:

Dim sqlcon As New SqlConnection("sqlstring")
     Dim sqladapt As New SqlDataAdapter("Select * From profile inner join (cemeteries inner join cemetery_reference on cemeteries.Cemetery_ID = cemetery_reference.Cemetery_ID)  on Profile.Individual_ID = Cemeteries.Individual_ID", sqlcon)

     sqlcon.Open()
     sqladapt.SelectCommand.ExecuteNonQuery()

     Dim DT As New DataTable
     sqladapt.Fill(DT)
     DataGridView1.DataSource = DT
     sqlcon.Close()



I keep getting "incorrect syntax on"

What I have tried:

I have tried double parenthesis after the word from, however, I am still getting the same syntax error.
Posted
Updated 13-Sep-18 22:24pm
Comments
Maciej Los 14-Sep-18 3:45am    
What's DBMS?

You didn't provide information about DBMS. Nevertheless...
The number of tables available to join within single query depends on DBMS...

A "typical" query looks like:
SQL
SELECT A.<List_Of_Fields>, B.<List_Of_Fields>, C.<List_Of_Fields>, D.<List_Of_Fields>
FROM Table1 AS A
    INNER JOIN Table2 AS B ON A.PrimaryKey = B.ForeignKey
    INNER JOIN Table3 AS C ON B.PrimaryKey = C.ForeignKey
    INNER JOIN Table4 AS D ON C.PrimaryKey = D.ForeignKey


There's few types of joins. See: Visual Representation of SQL Joins[^]

For further details, please see:
MS SQL Server: Querying with Joins[^]
MS Access: Join tables and queries - Access[^]
 
Share this answer
 
Comments
CHill60 14-Sep-18 8:04am    
5'd
Maciej Los 14-Sep-18 8:22am    
Thank you, Caroline.
you should try your SQL in SqlManagement, for faster SQL debugging! ;)
At any rate I immediatelyu spoted one bu (there might be more ...)

Select * From profile inner join (cemeteries inner join ...
should be, instead
Select * From profile inner join (select * from cemeteries inner join ...
 
Share this answer
 
Comments
Member 11856456 13-Sep-18 23:40pm    
this is for a 3 table, but what about for a 4 table join?
CHill60 14-Sep-18 8:01am    
A sub-query? Why would you do that? Don't get confused by the brackets (which are required by Access)
Member 11856456 14-Sep-18 14:10pm    
actually watching a video on a 3 table inner join brought me to an access video. I followed the example and it worked, but could not get this to run with a 4 table. However, the example above helped and I see where my mistake was.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900