Question

I'm new here so if I do something that breaks convention please let me know.

I am trying to create a simple query that includes a single outer join before I move onto a larger and more complicated query. When I run my query before adding the join it works as expected. When I add the join the query returns an error message. I cannot determine the cause of the error. I am working in Microsoft SQL Server 2008 R2.

This query runs successfully and produces the expected results:

use MyDatabase
select table1.ID_NUM,
table1.LAST_NAME
From table1,
table2
where table1.id_num = table2.id_num

This query does not run successfully:

use MyDatabase
select table1.ID_NUM,
table1.LAST_NAME
From table1,
table2
where table1 left outer join table2 on table1.id_num = table2.id_num

The error message I get is:

Msg 4145, Level 15, State 1, Line 6
An expression of non-boolean type specified in a context where a condition is expected, near 'left'.

I also tried "outer join" instead of "left outer join", but this produced a similar error message (the only difference was "near 'outer'" instead of "near 'left'").

I do know what boolean means but I am not sure why it's an issue here. The boolean operator comes later in the same line. My code looks like it follows the same format as other code I have seen. Any assistance would be appreciated.

Was it helpful?

Solution

your join needs to be after the FROM not in the WHERE clause

use MyDatabase
select table1.ID_NUM,
table1.LAST_NAME
From table1 
left outer join table2 on table1.id_num = table2.id_num

OTHER TIPS

You are using deprecated syntax, listing out tables and defining their relationship in the WHERE clause has been replaced by explicit JOIN statements

FROM Table1 t1
JOIN Table2 t2
 ON t1.col1 = t2.col1
LEFT JOIN Table3 t3
 ON t1.col2 = t3.col1

Old style joins do allow for outer joins:

use MyDatabase
select table1.ID_NUM,
table1.LAST_NAME
From table1,table2
where table1.id_num *= table2.id_num

This old syntax should be avoided, but helpful to know what they are if you come across them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top