duanaixuan7385 2017-04-20 10:53
浏览 82

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

I have some problems with running MSSQL store procedure in PDO.

I'm definitely having trouble using a non-parametric store procedure.

I use it like this;

$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);
}

However, when I want to run a store procedure that requires a parameter, there is no result. The codes that do not work are as follows;

$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);
}

When I look at the SQL Server Profiler tool, I get a query like this:

exec get_next_UniqueNumber '50'

The store procedure contents are as follows;

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 

When I run the store procedure with SQL Management Studio, the result is;

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
    • ¥15 怎么让wx群机器人发送音乐
    • ¥15 fesafe材料库问题
    • ¥35 beats蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功