PHP PDO MSSQL存储过程

时间:2017-06-17 11:59:25

标签: php sql-server stored-procedures pdo

我对MSSQL数据库的访问权限有限,我有带有PHP 5.6和PDO库的CentOS服务器。我需要运行将结果写入临时表的存储过程,然后从该表中生成SELECT以获取所需的数据。

实际上有3个语句 - 其中2个执行存储过程和1个SELECT。如果我将它们全部放在1个语句中 - 语句不返回任何内容(但应该在列表中返回80行),但是语句不会返回错误。

但如果我分割语句 - 它会在第二次调用时返回错误。

General SQL Server error: Check messages from the SQL Server [208] (severity 16) [(null)]

所以这里是代码:`

try {
    $this->db = new PDO 
("dblib:host=".$options['host'].";dbname=".$options['db'], $options['user'], $options['pass'], array(PDO::ATTR_PERSISTENT=>true));
}catch (PDOException $e) {
  $this->addLog("Failed to get DB handle: " . $e->getMessage());
  return false;
}

$tmp = "##good123";
$hotelinctable = "##hotelinc123";

$sql = "EXEC sp_executesql @createtable = N'CREATE TABLE ".$hotelinctable." (inc int null)'

        EXEC sp_executesql N'EXEC up_prclm
        @CDateFrom = @CDFrom,
        @CDateTill = @CDTill,
        @DateFrom = @DFrom,
        @DateTill = @DTill,
        @ConfirmedDateFrom = @ConfDFrom,
        @ConfirmedDateTill = @ConfDTill,
        @PDateFrom = @PDFrom,
        @PDateTill = @PDTill,
        @Owner = @Own,
        @HPartner = @HPartn,
        @state = @StateInc,
        @TourList = @TList,
        @PartnerList = @PList,
        @PGroupList = @PGList,
        @MediatorList = @MList,
        @Curr = @CurrInc,
        @BaseCurr = @BaseCurrInc,
        @DateRate = @RateDate,
        @Rate = @RateType,
        @Round = @RoundType,
        @StatusList = @ClaimStatusList,
        @Name = @ResultTableName,
        @hotelinc = @HIncTableName,
        @TourCurrency = @TourCurrencyInc,
        @claimstr = @ClaimList',
        N'@CDFrom datetime2,@CDTill datetime2,@DFrom datetime2,@DTill datetime2,@ConfDFrom datetime2,@ConfDTill datetime2,@PDFrom datetime2,@PDTill datetime2,@Own int,@HPartn int,@StateInc int,@TList varchar(8000),@PList varchar(8000),@PGList varchar(8000),@MList varchar(8000),@CurrInc int,@BaseCurrInc int,@RateDate datetime2,@RateType int,@RoundType int,@ClaimStatusList varchar(8000),@ResultTableName varchar(8000),@HIncTableName varchar(8000),@TourCurrencyInc bit,@ClaimList varchar(8000)',
        '1900-01-01 00:00:00','2079-06-06 00:00:00','2017-05-12 00:00:00','2017-05-12 00:00:00','1900-01-01 00:00:00','2079-06-06 00:00:00','1900-01-01 00:00:00','2079-06-06 00:00:00',-2147483647,-2147483647,0,'','2870','','',2,1,'2017-05-12 00:00:00',0,0,'1, 2, 4, 5','".$tmp."','".$hotelinctable."',0,''
";



$r = $this->db->prepare($sql);
$r->execute();

$err = $r->errorInfo();
$this->addLog("ERRCHK: ".var_export($err, true));

$sql = "SELECT prclm.claim AS order_id, 
               prclm.amount AS full_cost, 
               prclm.net AS net_cost, 
               currency.alias AS currency, 
               prclm.hnet AS hotel_net, 
               prclm.fnet AS flights_net, 
               prclm.inet AS insurance_net, 
               prclm.tnet AS transfers_net, 
               prclm.enet AS excursions_net, 
               prclm.vnet AS visas_net, 
               prclm.supnet AS suppliment_net, 
               prclm.snet AS services_ner, 
               hpartner.name AS hotel_partner, 
               claim.confirmed AS is_confirmed
          FROM ".$tmp." prclm 
    INNER JOIN claim ON prclm.claim = claim.inc 
    LEFT OUTER JOIN order ord ON ord.inc = prclm.order 
    LEFT OUTER JOIN partner hpartner ON hpartner.inc = ord.partner 
    LEFT OUTER JOIN currency on currency.inc = prclm.currency
      ORDER BY prclm.inc DESC";

$r = $this->db->prepare($sql);
$r->execute();
$err = $r->errorInfo();
$this->addLog("ERRCHK: ".var_export($err, true));

$res = $r->fetchAll(PDO::FETCH_ASSOC);

$this->addLog("RES: ".var_export($res, true));  `

请告知 - 有什么问题,以及如何调用多个程序然后选择SELECT。

0 个答案:

没有答案