drrrdo0802 2010-04-06 22:12
浏览 39
已采纳

PHP mssql_query双引号不能使用

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..

  • 写回答

3条回答 默认 最新

  • douzhui8531 2010-04-06 22:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!