Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table name is same as SQL keyword 2

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
Hi,

I've been handed a project with tables already in place. I am using PHP to construct and run SQL statements.

One of the tables is named Values.

This creates a problem when running this SQL statement within it being constructed in PHP:

select gref.Field1, gref.Field2, othertable.Field3,
Values.Field4, Values.Field5, Values.Field6
from (Values inner join gref ON Values.Field2=gref.Field2)
left join othertable on gref.Field2=othertable.Field2
where gref.Field1='abc'


Changing the table name works, such as calling it Values2. But I can't do that. The code above in particular gets hung up on: from (Values inner join

It seems having Values at that point in the SQL statement is the problem.

Any ideas what to do?

Thanks,
KB
 
although its always better to not use reserved words, if its impossible to change it, then you can surround the table name with back ticks.

Code:
select gref.Field1, gref.Field2, othertable.Field3,
  [red]`[/red]Values[red]`[/red].Field4, [red]`[/red]Values[red]`[/red].Field5, [red]`[/red]Values[red]`[/red].Field6
 from ([red]`[/red]Values[red]`[/red] inner join gref ON [red]`[/red]Values[red]`[/red].Field2=gref.Field2)
 left join othertable on gref.Field2=othertable.Field2
 where gref.Field1='abc'

Notice these are back ticks: [COLOR=red white]`[/color], not single or double quotes.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top