views:

214

answers:

3

Hi all, In java-jdbc, I can easily run the following SQL (NOTE the double quotes around columns and table names)

Select 
       cus."customer_id" ,
       cus."organisation_or_person" ,
       cus."organisation_name" ,
       cus."first_name" ,
       cus."last_name" ,
       cus."date_became_customer" ,
       cus."other_customer_details"
From 
      "Contact_Management"."dbo"."Customers"    cus

But the same query in PHP errors out saying invalid syntax

"Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near 'customer_id'. (severity 15) "

But If remove all the double quotes, the query works fine and no errors.

The query is ported from a java application so I would like to keep the double quotes and the SQL as it is. Any alternative solutions?

Thank you Nilesh

Volkerk -- Solution (SET QUOTED_IDENTIFIER ON)

I did the following

    $sql = <<<EOD
Select 
       cus."customer_id" ,
       cus."organisation_or_person" ,
       cus."organisation_name" ,
       cus."first_name" ,
       cus."last_name" ,
       cus."date_became_customer" ,
       cus."other_customer_details"
From 
      "Contact_Management"."dbo"."Customers"    cus
EOD;

$db->Execute('SET QUOTED_IDENTIFIER ON');
    $rs = $db->Execute($sql); 

And it worked perfect

Thank you so much..

A: 

It's not exactly as-is, but you could replace the double-quotes " with backticks:

Select 
       cus.`customer_id` ,
       cus.`organisation_or_person` ,
       cus.`organisation_name` ,
       cus.`first_name` ,
       cus.`last_name` ,
       cus.`date_became_customer` ,
       cus.`other_customer_details`
From 
      `Contact_Management`.`dbo`.`Customers`    cus
Amber
Dav, backticks don't work either. It gives the following errorWarning: mssql_query() [function.mssql-query]: message: Incorrect syntax near '`'.
Nilesh
A: 

What about this?

$query ='Select 
   cus."customer_id" ,
   cus."organisation_or_person" ,
   cus."organisation_name" ,
   cus."first_name" ,
   cus."last_name" ,
   cus."date_became_customer" ,
   cus."other_customer_details"
From 
  "Contact_Management"."dbo"."Customers"    cus';

$query = str_replace('"', '', $query);
Shuriken
I could use this,but what if there is a column name with a white space between the words, this would be broken thenFor e.g select cus."Customer ID" ... where there is a space between customer and the ID
Nilesh
A: 

QUOTED_IDENTIFIER is probably set to OFF.

http://msdn.microsoft.com/en-us/library/ms174393.aspx says:

SET QUOTED_IDENTIFIER (Transact-SQL)
[...]
When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marks. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers. For more information, see Identifiers
[...]
The SQL Server Native Client ODBC driver and SQL Server Native Client OLE DB Provider for SQL Server automatically set QUOTED_IDENTIFIER to ON when connecting. This can be configured in ODBC data sources, in ODBC connection attributes, or OLE DB connection properties. The default for SET QUOTED_IDENTIFIER is OFF for connections from DB-Library applications.

set it to On and you're good to go.

VolkerK
The same query works fine against the same instance from Java so I don't think it is something wrong with SQL server. I think it is more of the php library issue.
Nilesh
This is not a server-wide setting. So your java application may use another setting.
VolkerK
Hi Volkerk, you are right, it solved my issue. I apologize for replying without testing it.I did the following $sql = <<<EODSelect cus."customer_id" , cus."organisation_or_person" , cus."organisation_name" , cus."first_name" , cus."last_name" , cus."date_became_customer" , cus."other_customer_details"From "Contact_Management"."dbo"."Customers" cusEOD;$db->Execute('SET QUOTED_IDENTIFIER ON'); $rs = $db->Execute($sql); And it worked perfectThank you thank you very much
Nilesh