I'm trying to execute the procedure down below using Laravel query builder and the result always empty array,
SQL Procedure
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
Implementation code,
return DB::select("EXEC FConvTxtUnt ?,?", [$itemId ,$quantity] );
The result always seems like,
[]
Can anyone tell me what is wrong, considering that I don't have permission to modify the procedure itself, which I can't change the return to output.
WHILEin SQL runs very* slowly. I would strongly recommend rewriting your function to be an inline table value function, which uses a set based solution.EXP(SUM(LOG(factor)) OVER (PARTITION BY itmid ORDER BY Itemunitid DESC ROWS UNBOUNDED PRECEDING))