PHP PDO的MSSQL Server存储过程参数错误

时间:2017-04-20 10:53:54

标签: php sql-server stored-procedures pdo

我在PDO中运行MSSQL存储过程时遇到一些问题。

我在使用非参数存储过程时遇到了麻烦。

我这样用它;

$uid = "sa";
$pwd = "xxx";
try{
  $conn = new PDO ("dblib:host=10.10.10.10:1433;charset=UTF-8;dbname=TESTDB",$uid,$pwd);
}
catch(PDOException $e){
  echo $e->getMessage();
}


$sth = $conn->prepare("exec testSP");
$sth->execute();
while($result = $sth->fetch(PDO::FETCH_ASSOC)) {
var_dump($result);
}

但是,当我想运行需要参数的存储过程时,没有结果。不起作用的代码如下;

$uid = "sa";
$pwd = "xxx";
try{
  $conn = new PDO ("dblib:host=10.10.10.10:1433;charset=UTF-8;dbname=TESTDB",$uid,$pwd);
}
catch(PDOException $e){
  echo $e->getMessage();
}

$param1 = 50;
$sth = $conn->prepare("exec get_next_UniqueNumber ?");
$sth->bindParam(1,$param1);
$sth->execute();
while($result = $sth->fetch(PDO::FETCH_ASSOC)) {
var_dump($result);
}

当我查看SQL Server Profiler工具时,我得到一个这样的查询:

exec get_next_UniqueNumber '50'

商店程序内容如下;

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[get_next_UniqueNumber]       
@key_lenght int,       
@key nvarchar(50) OUTPUT       
as       
begin       
declare @count int       
select @count= count(*) from unique_numbers where flag = 0       
if (@count < 5)       
begin       
exec sp_generateSemiRandomString       
end        
update unique_numbers set flag=1, @key=unique_number where unique_number = (select top 1 unique_number from unique_numbers WITH (UPDLOCK, HOLDLOCK) where flag =0 order by unique_number)       
SET @key = RIGHT(@key, @key_lenght)          
end 

当我使用SQL Management Studio运行存储过程时,结果是;

DECLARE @return_value int,
        @key nvarchar(50)

EXEC    @return_value = [dbo].[get_next_UniqueNumber]
        @key_lenght = 50,
        @key = @key OUTPUT

SELECT  @key as N'@key'

SELECT  'Return Value' = @return_value

GO

0 个答案:

没有答案