2010-04-06 102 views
1

在Java的JDBC,我可以很容易地運行下面的SQL(注意周圍的列和表名稱雙引號)在PHP錯誤PHP mssql_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 

但相同的查詢說出來無效的語法

「警告:mssql_query()[function.mssql查詢]:消息:附近有語法錯誤CUSTOMER_ID「(嚴重性15)」

但如果去掉所有的雙引號,查詢作品罰款和沒有錯誤。

該查詢是從一個Java應用程序移植的,所以我想保留雙引號和SQL,因爲它是。任何替代方案?

謝謝 Nilesh製作

Volkerk - 解決方案(SET QUOTED_IDENTIFIER ON)

我做了以下

$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); 

而且它完美

太謝謝你了..

回答

0

QUOTED_IDENTIFIER可能設置爲OFF。

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

SET QUOTED_IDENTIFIER(的Transact-SQL)
[...]
當SET QUOTED_IDENTIFIER爲ON,標識符可以用雙引號分隔,而文字必須由單一的分隔引號。 當SET QUOTED_IDENTIFIER爲OFF時,不能引用標識符,並且必須遵循標識符的所有Transact-SQL規則。欲瞭解更多信息,請參閱 Identifiers
[...]
SQL Server的SQL Server的本機客戶端ODBC驅動程序和SQL Server本機客戶端OLE DB提供程序在連接時自動設置QUOTED_IDENTIFIER爲ON。這可以在ODBC數據源中,ODBC連接屬性或OLE DB連接屬性中進行配置。 對於來自DB-Library應用程序的連接,SET QUOTED_IDENTIFIER的默認值爲OFF。

將它設置爲On,你很好走。

+0

對於來自Java的同一實例,相同的查詢工作正常,所以我不認爲它是SQL Server的錯誤。我認爲這是更多的PHP庫問題。 – Nilesh 2010-04-06 22:34:43

+0

這不是服務器範圍內的設置。所以你的Java應用程序可能會使用其他設置 – VolkerK 2010-04-06 22:37:36

+0

嗨Volkerk,你是對的,它解決了我的問題。我對未經測試而回復表示歉意。 我做了以下 \t $ SQL = <<< EOD 選擇 CUS。 「CUSTOMER_ID」, CUS。 「organisation_or_person」, CUS。 「ORGANISATION_NAME」, CUS。 「FIRST_NAME」, CUS「。 last_name「, cus。」date_became_customer「, cus。」other_customer_details「 從 」Contact_Management「。」dbo「。」Customers「cus EOD; $ db-> Execute('SET QUOTED_IDENTIFIER ON'); $ rs = $ db-> Execute($ sql); 它工作完美 非常感謝你非常感謝 – Nilesh 2010-04-06 22:42:35

0

這不完全是,是​​的,但你可以取代雙引號與"反引號:

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 
+0

Dav,反引用也不起作用。它給出了以下錯誤 警告:mssql_query()[function.mssql-query]:message:'''附近的語法不正確。 – Nilesh 2010-04-06 22:32:18

0

這個怎麼樣?

$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); 
+0

我可以使用這個,但是如果在單詞之間有一個空白字段的列名,那麼這將會被破壞,然後 例如,選擇客戶「客戶ID」...客戶與客戶之間存在空間ID – Nilesh 2010-04-06 22:33:47