从PHP不完全执行存储过程

时间:2018-09-10 22:34:02

标签: php sql-server stored-procedures

嗨,我有一个存储过程,里面有一个while,它接收2个参数以了解while的条件。我让它像一个工作。如果我从SQL执行它,则可以正常工作,但是,如果我从PHP调用它,则while不遵守条件并突然结束。

while在每行中插入一个迭代器编号,例如

1,a,b
2,a,b
3,a,b

等如果将迭代次数设置为1到1000,则每次SP执行时循环停止都会无缘无故地进行不同的交互。

我尝试/捕获,但无法检测到错误。

我确定所有参数都正确。

有人可以帮助我检测为什么存储过程无法正确结束吗?

我正在使用PDO访问SQL Server。

存储过程:

ALTER procedure [dbo].[sp_AddSealsFromPurchase]
@initialFolio int,
@finalFolio int,
@userDelivery_ID varchar(50),
@userReceives_ID varchar(50),
@color nvarchar(10),
@supplier_ID int,
@requisition_ID int,
@office_ID int,
@reference nvarchar(50)
as
begin
declare @now datetime;
set @now = getdate();
    while @initialFolio<=@finalFolio Begin
    begin try
        INSERT into Seals (SealNo,SealType,Color,Supplier_ID,[Source],Office_ID,Requisition_ID,Reference)
        Values (@initialFolio,1,@color,@supplier_ID,1,@office_ID,@requisition_ID,@reference)
        INSERT INTO StatusSeal(Seal_ID,Status_ID,UserDelivery_ID,UserReceives_ID,StatusSeal_Date,UserDeliveryType,UserReceivesType)
        Values(@@Identity,1,@userDelivery_ID,@userReceives_ID,@now,1,1)
        set @initialFolio = @initialFolio +1;
        end try
begin catch
        set @initialFolio = @initialFolio +1;
end catch  
    end
end

PHP:

function AddSealsFromPurchase($Data){
    session_start();
    $cnx=$this->connectSqlSrv();
    $sth=$cnx->prepare("Exec sp_AddSealsFromPurchase ?,?,?,?,?,?,?,?,?");
    $sth->bindParam(1, $Data["InitialFolio"]);
    $sth->bindParam(2, $Data["FinalFolio"]);
    $sth->bindParam(3, $Data["UserDelivery"]);
    $sth->bindParam(4, $_SESSION['SESSINFOSEAL']['User_Usr']);
    $sth->bindParam(5, $Data["Color"]);
    $sth->bindParam(6, $Data["Supplier_ID"]);
    $sth->bindParam(7,  $Data["Requisition_ID"]);
    $sth->bindParam(8, $_SESSION['SESSINFOSEAL']['Ofice_ID']);
    $sth->bindParam(9, $Data["Reference"]);
    session_write_close();
    $retval=$this->ExecuteNoQuery($sth);
    return json_encode($retval);
  }

public function connectSqlSrv(){
        try{
            $dbCnx = new PDO("sqlsrv:Server=$this->server;Database=$this->db", $this->usr, $this->psw);
            $dbCnx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $dbCnx;
        }
        catch(PDOException $e){
            echo $e;
            die();
            return null;
        }
    }

    public function ExecuteNoQuery($sth)
    {
        $r=array('data'=>false,
        'error'=>false,
        'r'=>'');
        try {
            $sth->execute();
            if ($r['r'] = $sth->rowCount()) {
                $r['data'] = true;
            }
        } catch (PDOException $e) {
            $r['error']=true;
            $r['r'] = $e->getMessage();
        }
        return $r;
    }

0 个答案:

没有答案