I have a horrible long select statement and I need to use an alias table within it. I have not used alias tables much before and I am having problems getting my syntax correct. When I run my ASP page I get the message "Incorrect syntax near the keyword 'RIGHT'". I have bolded where my alias table is. It looks like it is the RIGHT join directly after this where the problem is happening. (If I replace my alias table in the statement with a 'real' table this error does not occur).
SELECT tblName.Surname, tblName.Firstname, tblName.ID AS NameID, tblCountry.Country, tblCountry.Region, tblCountry.ID AS CountryID, tblSection.SectionName, tblSection.ID AS SectionID, tblHeader.Heading, tblEntryValue.EntryValue
FROM (SELECT TOP 100 NameID FROM tblEntryValue AS tblALIAS WHERE HeaderID='" & SomeValue & "' ORDER BY EntryValue DESC)
RIGHT JOIN (tblSection INNER JOIN (tblHeader LEFT JOIN ((tblCountry RIGHT JOIN tblName ON tblCountry.ID = tblName.CountryID) RIGHT JOIN tblEntryValue ON tblName.ID = tblEntryValue.NameID) ON tblHeader.ID = tblEntryValue.HeaderID) ON tblSection.ID = tblHeader.SectionID) ON tblALIAS.NameID = tblEntryValue.NameID
Can anyone spot why my alias table is causing problems?
I could also do with adding another field to my alias table - one which would contain an autonumber or ranking order rather than just pulling in a field that already exists in a table. I would therefore be aiming for my alias table to be something like:
OrderID | NameID
1 | 90
2 | 23
3 | 45
SELECT tblName.Surname, tblName.Firstname, tblName.ID AS NameID, tblCountry.Country, tblCountry.Region, tblCountry.ID AS CountryID, tblSection.SectionName, tblSection.ID AS SectionID, tblHeader.Heading, tblEntryValue.EntryValue
FROM (SELECT TOP 100 NameID FROM tblEntryValue AS tblALIAS WHERE HeaderID='" & SomeValue & "' ORDER BY EntryValue DESC)
RIGHT JOIN (tblSection INNER JOIN (tblHeader LEFT JOIN ((tblCountry RIGHT JOIN tblName ON tblCountry.ID = tblName.CountryID) RIGHT JOIN tblEntryValue ON tblName.ID = tblEntryValue.NameID) ON tblHeader.ID = tblEntryValue.HeaderID) ON tblSection.ID = tblHeader.SectionID) ON tblALIAS.NameID = tblEntryValue.NameID
Can anyone spot why my alias table is causing problems?
I could also do with adding another field to my alias table - one which would contain an autonumber or ranking order rather than just pulling in a field that already exists in a table. I would therefore be aiming for my alias table to be something like:
OrderID | NameID
1 | 90
2 | 23
3 | 45