Simple JOIN query

  • Cany anyone explain to me why the query below does not work in SSMS 2005? Every time I run this I receive "Incorrect syntax near the keyword 'Group'." These are sample tables from the Adventure Works DB. If I remove st.Group it runs fine.

    SELECT sp.SalesPersonID, sp.SalesYTD, st.Group

    FROM Sales.SalesPerson sp

    INNER JOIN Sales.SalesTerritory st

    ON sp.TerritoryID = st.TerritoryID;

  • have you tried it as st.[group] ?

    group is a keyword so you probably need to enclose it in brackets.

    To help us help you read this[/url]For better help with performance problems please read this[/url]

  • Luke is correct, the parser is interpreting "GROUP" as the GROUP BY phrase. you need the square brackets.

  • If it's not too late, you might also consider renaming that column. It's just fewer headaches in the long run to avoid keywords as column names.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Adding the brackets took care of the problem. Kind of surprised that this column name was in the Adventure Works DB from Microsoft. Another lesson learned.

  • Thanks for everyones help.

  • yeah, it's lovely when MS doesn't even follow it's own rules when it comes to reserved words. I currently work with a db where some brain trust added the column [case] to about 1/3 of the tables. Luckily, I have SQL Prompt configured to always add brackets for me now, so it's less annoying, but...

    -Luke.

    To help us help you read this[/url]For better help with performance problems please read this[/url]

  • Sorry I blew over that this was out of Adventureworks.

    Merry Christmas to all. May 2009 be a better year for everyone.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply