SQL 语句: "" SQL 错误代码: "7335942".
错误原因:
您看到这个错误页面是因为应用程序抛出了没有捕获的异常。
异常的详细内容: exception 'FLEA_Db_Exception_SqlQuery' with message 'SQL 语句: ""
SQL 错误代码: "7335942".' in \FLEA\FLEA\Db\Driver\Mssql.php:129
LOG 文件
[2015-09-06 22:30:50 0.17770500] ======= FleaPHP Loaded =======
[2015-09-06 22:30:50] REQUEST_URI: /index.php?m=Default&a=test
[2015-09-06 22:30:50] [debug] :sql: SELECT a.COLUMN_NAME AS Field, (CASE WHEN CHARACTER_MAXIMUM_LENGTH IS NULL THEN DATA_TYPE + '(' + CONVERT(VARCHAR,NUMERIC_PRECISION) + ')' ELSE DATA_TYPE + '(' + CONVERT(VARCHAR,CHARACTER_MAXIMUM_LENGTH) + ')' END) AS DataType, IS_NULLABLE AS Nullable, CONSTRAINT_TYPE AS PrimaryKey, COLUMN_DEFAULT AS DefaultValue, (CASE WHEN COLUMNPROPERTY(OBJECT_ID('maintain'),a.COLUMN_NAME,'IsIdentity')=1 THEN 'auto_increment' ELSE '' END) AS Extra FROM INFORMATION_SCHEMA.COLUMNS AS a LEFT JOIN (INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS b INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS c ON b.CONSTRAINT_NAME=c.CONSTRAINT_NAME AND b.TABLE_NAME=c.TABLE_NAME) ON a.COLUMN_NAME=b.COLUMN_NAME AND a.TABLE_NAME=b.TABLE_NAME WHERE a.TABLE_NAME='maintain'
[2015-09-06 22:30:50] [debug] :sql:
[2015-09-06 22:30:50] [exception] :FLEA_Db_Exception_SqlQuery: SQL 语句: ""
SQL 错误代码: "7335942".
[2015-09-06 22:30:50 0.23316300] ======= FleaPHP End (elapsed: 0.055458 seconds) =======
按出错页面的DEBUG提示\TableDataGateway.php [461]行
$rowset = $this->dbo->getAll($result);
实际调用是
Db\Driver\Mssql.php [531]
526 function & getAll($sql)
527 {
528 if (is_resource($sql)) {
529 $res = $sql;
530 } else {
531 $res = $this->execute($sql);
532 }
533 $data = array();
534 while ($row = mssql_fetch_assoc($res)) {
535 $data[] = $row;
536 }
537 mssql_free_result($res);
538 return $data;
539 }
而且看起来这个传参$SQL是空的 ?
测试了下果然如此,
而且原本调用的是find , 从TableDataGateway.php [400]处可以看出,实际调用的都是findAll
398 function & find($conditions, $sort = null, $fields = '*', $queryLinks = true)
399 {
400 $rowset =& $this->findAll($conditions, $sort, 1, $fields, $queryLinks);
find, findAll的临时SQL组装都在findAll完成
出错的地方是在处理关联的部分, 如果不处理关联, 直接调用findBySql,不带limit和offset倒是可以成功运行,
只要带limit就出错
*/
function & findBySql($sql, $limit = null)
{
// 处理 $limit
if (is_array($limit)) {
list($length, $offset) = $limit;
} else {
$length = $limit;
$offset = null;
}
if (is_null($length) && is_null($offset)) {
return $this->dbo->getAll($sql);
}
$result = $this->dbo->selectLimit($sql, $length, $offset);
if ($result) {
$rowset = $this->dbo->getAll($result);
} else {
$rowset = false;
}
return $rowset;
}
可见当length,offset为null直接调用return $this->dbo->getAll($sql);
这句就能正常运行
否则
$result = $this->dbo->selectLimit($sql, $length, $offset);
$rowset = $this->dbo->getAll($result);
这样就不正常了
而调用find的时候,实际上是初始化length为1, 去调用findAll
$rowset =& $this->findAll($conditions, $sort, 1, $fields, $queryLinks);
发现 findBySql 和 findAll 两个函数里面都是对于length不为null的情况调用
$result = $this->dbo->selectLimit($sql, $length, $offset);
结果发现,mssql.php 里面对selectLimit函数是空, 怪不得这个返回的result是空
后面$rowset = $this->dbo->getAll($result); 这个就更无法运行了。
继而准备实现这个selectLimit的时候发现需要实现很复杂的sql字符串操作。。。。
SQL server 中查询第 7 条到第 9 条记录,则相应的SQL语句是
select top 3 id from tablename
where id not in (
select top 6 id from tablename
)
select top (n-m+1) id from tablename
where id not in (
select top m-1 id from tablename
)
select top @pageSize id from tablename
where id not in (
select top @offset id from tablename
)
第三条记录到第八条记录
SELECT TOP 5 *
FROM maintain
WHERE [30D130001] NOT IN
(
SELECT TOP 2 [30D130001] FROM maintain ORDER BY [30D130001]
)
正郁闷时候发现, 原来mssql.php 文件里面有一个被全部注释起来了的selectLimit函数, 额 , 结果放上去跑一下看, 语句是对的,
如下
调用
$result = $this-> _tbMaintain -> find("'30D130001' != '34D319001'",null);
生成的是
SELECT TOP 1 maintain.* FROM maintain WHERE ( '30D130001' != '34D319001') AND IDENTITYCOL NOT IN (SELECT TOP 0 IDENTITYCOL FROM maintain WHERE '30D130001' != '34D319001')
调用
$result = $this-> _tbMaintain -> findAll("'30D130001' != '34D319001'",null,5);
生成的是:
SELECT TOP 5 maintain.* FROM maintain WHERE ( '30D130001' != '34D319001') AND IDENTITYCOL NOT IN (SELECT TOP 0 IDENTITYCOL FROM maintain WHERE '30D130001' != '34D319001')
报错当前表没有IDENTITYCOL
对现存的表加了个ID字段,并设置为identity
ALTER TABLE maintain
ADD ID int IDENTITY(1,1)
ALTER TABLE [tvv].[dbo].[客服维修$] ADD ID int IDENTITY(1,1)
改好表结构后, selectLimit 这个函数跑的很好,很复杂的SQL语句都能生成
ELECT TOP 1 maintain.* FROM maintain WHERE ( '事件编码' != '34D319001') AND IDENTITYCOL NOT IN (SELECT TOP 0 IDENTITYCOL FROM maintain WHERE '事件编码' != '34D319001')
阅读(878) | 评论(0) | 转发(0) |