Laravel永远不会结束EXEC

时间:2018-03-27 12:48:51

标签: php sql-server laravel stored-procedures laravel-5.3

我正在将程序从纯PHP改写为Laravel,我在执行存储过程时遇到问题(我没有写过)。

当我尝试

$sheetLines = DB::select("exec XXXXXXX".dbo.PRICELIST '?'", [$id]); 

当PHP没有达到最大内存时,它一直在继续。 (增加内存只能让它运行更长时间)

同时,一个旧程序需要大约3秒钟并发送响应。

$tsql = "exec XXXXXXX.".dbo.PRICELIST '".$id."'";

当我从其他模块调用其他存储过程时,一切正常。

我注意到,如果我尝试例如:

,也会发生类似的事情
DB::select(count(price) from orders group by price);
// would work with: count(price) as price_count

我已经搜索了很多这个问题,但没有找到解决办法。 我会感谢任何帮助

2 个答案:

答案 0 :(得分:3)

我对PHP的经验不多,但看到了:

set nocount on

导致某些MSSQL驱动程序出现奇怪的行为。特别是存储过程返回单个记录集,但不输出“(x行受影响)”类型消息。 (在SQL Server Management Studio中手动执行查询时可以看到消息。) 我对复杂存储过程的经验法则是添加...

set nocount on

...在存储过程开始时以......结束

set nocount off

....就在最终输出之前。

示例:

create proc spTester 
as 
begin
set nocount off

-- {... do lots of crazy processing ...}

-- ok, ready to return the final output
set nocount off
select 1 as colA, 2 as colB

end

答案 1 :(得分:2)

我将访问底层的PDO驱动程序来执行存储过程。

尝试

$db = DB::getPdo();

$stmt = $db->prepare("EXEC XXXXXXX.dbo.PRICELIST :id");
$stmt->bindValue(':id', $id);

$result = $stmt->execute();

如果无效,您可以尝试查询方法;

$query = DB::getPdo()->query("EXEC XXXXXXX.dbo.PRICELIST $id");

关于你的计数问题,在Laravel's Eloquent中你可以做到以下几点;

$count = DB::table('orders')->groupBy('price')->count('price');
相关问题