yii mssql查询无法正常工作

时间:2013-03-20 12:43:49

标签: php mysql pdo yii

我有以下查询:

Yii::app()->dbNav->createCommand()
    ->select('*')
    ->from('dbo Spectrum Geo Limited$Purch_ Inv_ Header')       
    ->where('"Log Number" = :id', array(':id'=>$_GET['lognumber']))
    ->queryRow();

它失败并出现以下错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 20018 Invalid object name 'dbo'. [20018] (severity 5) [(null)]. The SQL statement executed was: SELECT *
FROM [dbo] [Spectrum Geo Limited$Purch_ Inv_ Header]
WHERE "Log Number" = :id 

我的dblib连接与mssql一起使用。我想将以下内容转换为yii

$sql="SELECT * FROM [Spectrum Geo Limited\$Purch_ Inv_ Header] WHERE [Log Number] = $q ";

在Management studio中,我运行以下命令以获得所需的结果:

SELECT TOP 1000 *
  FROM [Navision].[dbo].[Spectrum Geo Limited$Purch_ Inv_ Header]
    where [Log Number] = 4593

以下是有效的,但这是理想的做法吗?

Yii::app()->dbNav->createCommand("SELECT * FROM [Spectrum Geo Limited\$Purch_ Inv_ Header] WHERE [Log Number] = ".$_GET['lognumber'] );

继续

如果我执行以下操作

Yii::app()->dbNav->createCommand()
        ->select('*')
        ->from('[dbo].[Spectrum Geo Limited$Purch_ Inv_ Header]')
        ->where('"Log Number" = :id', array(':id'=>$_GET['lognumber']))
        ->queryRow();

我收到以下错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 20018 Unclosed quotation mark after the character string 'Geo Limited$Purch_ Inv_ Header]
WHERE "Log Number" = '4593''. [20018] (severity 5) [(null)]. The SQL statement executed was: SELECT *
FROM [[dbo]].[[Spectrum] [Geo Limited$Purch_ Inv_ Header]]
WHERE "Log Number" = :id 

现在我试过了:

Yii::app()->dbNav->createCommand()
        ->select('*')
        ->from('[Spectrum Geo Limited$Purch_ Inv_ Header]')
        ->where('"Log Number" = :id', array(':id'=>$_GET['lognumber']))
        ->queryRow();

错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 20018 Unclosed quotation mark after the character string 'Geo Limited$Purch_ Inv_ Header]
WHERE "Log Number" = '4593''. [20018] (severity 5) [(null)]. The SQL statement executed was: SELECT *
FROM [[Spectrum] [Geo Limited$Purch_ Inv_ Header]]
WHERE "Log Number" = :id 

删除where子句会产生此错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 20018 Unclosed quotation mark after the character string 'Geo Limited$Purch_ Inv_ Header]'. [20018] (severity 5) [(null)]. The SQL statement executed was: SELECT *
FROM [[Spectrum] [Geo Limited$Purch_ Inv_ Header]] 

2 个答案:

答案 0 :(得分:1)

您需要表名周围的括号,因为该表的名称中包含空格。

通常认为设计不佳,但看起来这是来自第三方供应商的数据库,因此您可能无法更改它。

编辑:您可能不需要[dbo] ......

编辑2:它是您的where子句中列名中的空格,现在导致问题。

编辑3:如果您查看有效的原始查询:

Yii::app()->dbNav->createCommand("SELECT * FROM [Spectrum Geo Limited\$Purch_ Inv_ Header] WHERE [Log Number] = ".$_GET['lognumber'] );

然后将其与此进行比较:

SELECT * FROM [[Spectrum] [Geo Limited$Purch_ Inv_ Header]] 

你会注意到你的括号不同。

使用它,它应该工作:

SELECT * FROM [Spectrum Geo Limited\$Purch_ Inv_ Header]

编辑4:

好的,如果您使用以下内容会发生什么?

Yii::app()->dbNav->createCommand()
    ->select('*')
    ->from('Spectrum Geo Limited$Purch_ Inv_ Header')       
    ->where('"Log Number" = :id', array(':id'=>$_GET['lognumber']))
    ->queryRow();

我正在遗漏dbo.部分,因为你的SQL语句之前没有它。通过将所有空间留在那里,我们可能会激活Yii自动包围,这会导致您更早出现头痛......

答案 1 :(得分:0)

当我尝试这个时它适用于我:

$row=Yii::app()->db->createCommand()
->select('*')
->from('wdr_user')
->where('id = :id', array(':id'=>135))
->queryRow();
print_r($row);

我希望如果你删除where子句中的双引号,它应该可以工作。