DevWithZachary
DevWithZachary

Reputation: 3685

SQL Server Incorrect syntax near '`'

I have the below SQL which I am trying to execute against a Microsoft SQL Server.

$rs = odbc_exec($conn, "INSERT INTO tGuest (`GuestName`, `GuestEmailAddress`, `GuestPassword`, `GuestCity`, `GuestCountry`) VALUES ('test', 'test', 'test', 'test', 'test');");

However I ened up with the below error:

[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '`'.

I can not see anything wrong with the SQL code so am not sure if I am missing something or there is something else wrong.

Upvotes: 1

Views: 664

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 175924

SQL Server uses []/" to quote identifiers:

Delimited identifiers

Are enclosed in double quotation marks (") or brackets ([ ]). Identifiers that comply with the rules for the format of identifiers might not be delimited.

$rs = odbc_exec($conn, "INSERT INTO tGuest ([GuestName], [GuestEmailAddress], [GuestPassword], [GuestCity], [GuestCountry]) VALUES ('test', 'test', 'test', 'test', 'test');");

In your case you could also skip it at all, because your identifiers are valid:

INSERT INTO tGuest(GuestName,GuestEmailAddress,GuestPassword,GuestCity,GuestCountry) 
VALUES ('test', 'test', 'test', 'test', 'test');

Your naming convention seems to be redundant. If table is named tGuest then there is no need to prefix columns with Guest.

Upvotes: 5

Related Questions