如何获取SQL服务器程序的返回值?
How to fetch SQL Server procedure returned value?
我正在尝试使用 Laravel 查询生成器执行下面的过程,结果总是空数组,
SQL 程序
CREATE FUNCTION [dbo].[FConvTxtUnt]
(
-- Add the parameters for the function here
@itmid int ,
@number numeric(12,2)
)
RETURNS nvarchar(150)
AS
BEGIN
--
declare @txtunitqty as nvarchar(150)
set @txtunitqty=''
declare @factors as numeric(12,2)
declare @result as int
declare @result2 as numeric(12,2)
set @result=1
declare tcur cursor for
select itemunitid,unitname
from stock_ItemsUnits
inner join stock_Units on stock_Units.unitid=stock_ItemsUnits.unitid
where itmid = @itmid
order by itemunitid
open tcur ;
declare @Itemunitid int;
declare @unitname nvarchar(50);
fetch next from tcur into @Itemunitid,@unitname
while (@@fetch_status = 0)
BEGIN
set @factors = (
select EXP(SUM(LOG(factor)))
from stock_ItemsUnits
where itmid = @itmid and Itemunitid>=@Itemunitid
)
if ( @factors=1)
begin
set @txtunitqty=@txtunitqty +'/'+cast (@number as nvarchar(50))+@unitname
end
else
begin
set @result= @number/@factors
set @result2= @number%@factors
set @txtunitqty=@txtunitqty +'/'+cast (@result as nvarchar(50))+@unitname
end
set @number=@result2
fetch next from tcur into @Itemunitid,@unitname;
end
close tcur;
deallocate tcur;
RETURN @txtunitqty
END
实现代码,
return DB::select("EXEC FConvTxtUnt ?,?", [$itemId ,$quantity] );
结果总是这样,
[]
任何人都可以告诉我哪里出了问题,考虑到我没有修改程序本身的权限,我无法将 return 更改为输出。
我将重点关注问题的 PHP 部分。您正在尝试执行一个函数,而不是一个过程,因此您只需更改语句:
DB::select("SELECT dbo.FConvTxtUnt(?, ?)", [$itemId, $quantity]);
我明白你不能改变功能,因为你...没有权限修改程序...,但你需要考虑非常仔细看关于使用循环和游标的评论。
我正在尝试使用 Laravel 查询生成器执行下面的过程,结果总是空数组,
SQL 程序
CREATE FUNCTION [dbo].[FConvTxtUnt]
(
-- Add the parameters for the function here
@itmid int ,
@number numeric(12,2)
)
RETURNS nvarchar(150)
AS
BEGIN
--
declare @txtunitqty as nvarchar(150)
set @txtunitqty=''
declare @factors as numeric(12,2)
declare @result as int
declare @result2 as numeric(12,2)
set @result=1
declare tcur cursor for
select itemunitid,unitname
from stock_ItemsUnits
inner join stock_Units on stock_Units.unitid=stock_ItemsUnits.unitid
where itmid = @itmid
order by itemunitid
open tcur ;
declare @Itemunitid int;
declare @unitname nvarchar(50);
fetch next from tcur into @Itemunitid,@unitname
while (@@fetch_status = 0)
BEGIN
set @factors = (
select EXP(SUM(LOG(factor)))
from stock_ItemsUnits
where itmid = @itmid and Itemunitid>=@Itemunitid
)
if ( @factors=1)
begin
set @txtunitqty=@txtunitqty +'/'+cast (@number as nvarchar(50))+@unitname
end
else
begin
set @result= @number/@factors
set @result2= @number%@factors
set @txtunitqty=@txtunitqty +'/'+cast (@result as nvarchar(50))+@unitname
end
set @number=@result2
fetch next from tcur into @Itemunitid,@unitname;
end
close tcur;
deallocate tcur;
RETURN @txtunitqty
END
实现代码,
return DB::select("EXEC FConvTxtUnt ?,?", [$itemId ,$quantity] );
结果总是这样,
[]
任何人都可以告诉我哪里出了问题,考虑到我没有修改程序本身的权限,我无法将 return 更改为输出。
我将重点关注问题的 PHP 部分。您正在尝试执行一个函数,而不是一个过程,因此您只需更改语句:
DB::select("SELECT dbo.FConvTxtUnt(?, ?)", [$itemId, $quantity]);
我明白你不能改变功能,因为你...没有权限修改程序...,但你需要考虑非常仔细看关于使用循环和游标的评论。