tags:

views:

40

answers:

5

I am trying to link several tables together in SQL Server. The code below shows how I have linked the tables together so far:

select *
from profile
left join learner l on l.learnerid = profileid
left join learner_levels ll on ll.learnerid = l.learnerid
left join subjects s on s.subjectid = ll.subjectid
left join learner_group lg on lg.learnerid = profileid
where ll.archived = '0' and ll.completed = '0'
order by surname asc`

What i want to do is filter the results by "groupid" which is in the table "group". if I add that as a 'left join' statement I get the error stated in the title - "Incorrect syntax near the keyword 'group'. "

This is what I tried:

select *
from profile
left join learner l on l.learnerid = profileid
left join learner_levels ll on ll.learnerid = l.learnerid
left join subjects s on s.subjectid = ll.subjectid
left join learner_group lg on lg.learnerid = profileid
left join group g on g.groupid = lg.learnerid
where ll.archived = '0' and ll.completed = '0' and g.group_name = 'class 1'
order by surname asc`

This is the result in SQL Server Management Studio:

Msg 156, Level 15, State 1, Line 7 Incorrect syntax near the keyword 'group'.

Where am I going wrong?

+5  A: 

Group is a reserved word. Try to use a different name (for the table). Or put square brackets around [group].

Tobiasopdenbrouw
You added the square brackets a second before I got my post delivered. Sneaky devil. +1
Mike M.
I would strongly encourage you to change the table name if possible. We ran into an issue like this not long ago and there are certain operations which will not work on some reserved words regardless of the square brackets. For us, we inherited a [Merge] table and there was no way to insert into that table.
Bobby B
Thanks! How do I go about creating an alias? Or how do I change the name? I can't rename the table in the database as it would cause the program running on our system to crash.
Jeff
@MikeM Because I only thought of them after I'd answered. :) But I'd still rename that table (Like @BobbyB says). Accident waiting to happen.
Tobiasopdenbrouw
+2  A: 

Wrap Group in []'s

Group is a reserved word, as Tobias said. They need to be changed or wrapped in square-brackets.

select * from profile left join learner l on l.learnerid = profileid left join learner_levels ll on ll.learnerid = l.learnerid left join subjects s on s.subjectid = ll.subjectid left join learner_group lg on lg.learnerid = profileid left join [group] g on g.groupid = lg.learnerid where ll.archived = '0' and ll.completed = '0' and g.group_name = 'class 1' order by surname asc
Mike M.
+1  A: 

If you use a reserved word, such as GROUP, as a table name, you need to quote it using square brackets:

LEFT JOIN [Group] g
devio
A: 

Use [group] to specify the table vs the reserved word group

FlyingStreudel
Thank you very much, this has solved my problem! :)
Jeff
A: 

"group" is a keyword, so you'll have to write that line as :

left join [group] g on g.groupid = lg.learnerid 
James Curran