Хранимая процедура работает в Azure, но не в PHP, используя код, который правильно выполняет все мои другие хранимые процедуры - PullRequest
2 голосов
/ 11 февраля 2020
  • Код возвращает пустой массив
  • Хранимая процедура работает правильно в Azure с точно такими же параметрами
  • Код php правильно выполняет все остальные мои хранимые процедуры

Я не могу понять, что может быть причиной того, что этот SP ничего не возвращает, только когда он проходит через php / sqlsrv. Есть идеи ??

Мой php:

<?php
header("Access-Control-Allow-Origin: *");

$serverName = "tcp:example.database.windows.net,1433";
$connectionInfo = array( "Database"=>"Whitecap", "UID"=>"example", "PWD"=>"example", "CharacterSet"=>"UTF-8");
$conn = sqlsrv_connect( $serverName, $connectionInfo );


$arg1 = 'ES04';
$arg2 = '1';
$output = '';
$call = "{CALL uspGetSegmentInfo (?,?) }";
$params = [ 
  [$arg1, SQLSRV_PARAM_IN],
  [$arg2, SQLSRV_PARAM_IN],
  [&$output, SQLSRV_PARAM_OUT],
];

$result = sqlsrv_query($conn, $call, $params);
$array = [];

while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC)) {
  $obj = new stdClass;
  $obj->id = $row[1];
  $obj->name = $row[0];

  $array[] = $obj;
}

echo json_encode($array);
?>

Мой SQL:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[uspGetSegmentInfo] @TripNumber varchar(10), @SegmentNumber int
AS
BEGIN
    -- Segment data
    declare @Segments Table (Sequence int, [Segment Type] varchar(20))
    declare @SegmentType varchar(20)

    insert @Segments exec uspGetTripSegments @TripNumber
    select @SegmentType = [Segment Type] from @Segments where Sequence = @SegmentNumber

    -- Departure Port
    IF @SegmentType = 'Departure Port'
        BEGIN        
            select (select [port name] from [PORT DETAIL] pd where pd.[PORT ID] = ptd.[PORT ID]) as 'PORT NAME', 
            (select dd.[dock name] from [DOCK DETAIL] dd where dd.[DOCK ID] = ptd.[DOCK ID]) as 'DOCK NAME' 
            from [PORT TRIP DETAIL] ptd where [TRIP NUMBER] = @TripNumber and [PORT SEQUENCE] = @SegmentNumber
        END
    -- Load/Discharge Port (INCOMPLETE)
    ELSE IF @SegmentType = 'Load Port' OR @SegmentType = 'Discharge Port'
        BEGIN
            declare @CargoPortInfo table ([PORT NAME] varchar(50),[DOCK NAME] varchar(50),CUSTOMER varchar(50),[CARGO TYPE] varchar(50),[CARGO SUB TYPE] varchar(50),TONNAGE varchar(20),LAYTIME VARCHAR(10),[EXPECTED DURATION] varchar(10))
            declare @NumberCargos int
            declare @PortName varchar(50)
            declare @DockName varchar(50)
            declare @Customer varchar(50)
            declare @CargoType varchar(50)
            declare @CargoSubType varchar(50)
            declare @Tonnage varchar(20)
            declare @Laytime varchar (10)
            declare @Duration varchar(10)
            declare @CurrentCargoID int
            declare @i int

            -- Get number of cargos for this port
            select @NumberCargos = count(distinct [cargo detail id]) from [cargo event detail] where [TRIP NUMBER] = @TripNumber and [CARGO SEQUENCE] = @SegmentNumber

            -- Loop through cargos
            set @i = 1
            while @i < (@NumberCargos + 1)
                begin
                    select @CurrentCargoID = [cargo detail id] from [cargo event detail] where [TRIP NUMBER] = @TripNumber and [CARGO SEQUENCE] = @SegmentNumber and [SOURCE CARGO ID] = @i

                    -- Port Name
                    select @PortName = (select [port name] from [PORT DETAIL] pd where pd.[PORT ID] = ptd.[PORT ID]) from [PORT TRIP DETAIL] ptd where [TRIP NUMBER] = @TripNumber and [PORT SEQUENCE] = @SegmentNumber

                    -- Dock Name
                    select @DockName = (select dd.[dock name] from [DOCK DETAIL] dd where dd.[DOCK ID] = ptd.[DOCK ID]) from [PORT TRIP DETAIL] ptd where [TRIP NUMBER] = @TripNumber and [PORT SEQUENCE] = @SegmentNumber

                    -- Customer
                    select @Customer = (select cd.[CUSTOMER NAME] from [CUSTOMER DETAIL] cd where cd.[CUSTOMER ID] = crd.[CUSTOMER ID]) from [CARGO DETAIL] crd where crd.[cargo detail id] = @CurrentCargoID

                    -- Cargo type
                    select @CargoType = (select ct.[CARGO TYPE NAME] from [CARGO TYPE] ct where ct.[CARGO TYPE ID] = crd.[CARGO TYPE ID]) from [CARGO DETAIL] crd where crd.[cargo detail id] = @CurrentCargoID

                    -- Cargo sub type
                    select @CargoSubType = (select cst.[CARGO SUB TYPE NAME] from [CARGO SUB TYPE] cst where cst.[CARGO SUB TYPE ID] = crd.[CARGO SUB TYPE ID]) from [CARGO DETAIL] crd where crd.[cargo detail id] = @CurrentCargoID

                    -- Tonnage
                    select @Tonnage = concat(format(convert(decimal(10,2),[EXPECTED TONNAGE]),'#,#'),' ',[EXPECTED TONNAGE UNITS]) from [CARGO EVENT DETAIL] ced where ced.[TRIP NUMBER] = @TripNumber and ced.[CARGO SEQUENCE] = @SegmentNumber and ced.[CARGO DETAIL ID] = @CurrentCargoID

                    -- Laytime
                    select @Laytime = concat([LAYTIME RATE],' ',[LAYTIME RATE UNITS]) from [CARGO EVENT DETAIL] ced where ced.[TRIP NUMBER] = @TripNumber and ced.[CARGO SEQUENCE] = @SegmentNumber and ced.[CARGO DETAIL ID] = @CurrentCargoID

                    -- Get cargo laytime allowance in hours
                    select @Duration = convert(decimal(10,5),[cargo load time]) from [CARGO EVENT DETAIL] ced where ced.[TRIP NUMBER] = @TripNumber and ced.[CARGO SEQUENCE] = @SegmentNumber and ced.[CARGO DETAIL ID] = @CurrentCargoID

                    -- Format duration
                    set @Duration = dbo.getFormattedDuration(@Duration,'H')

                    insert into @CargoPortInfo values (@PortName,@DockName,@Customer,@CargoType,@CargoSubType,@Tonnage,@Laytime,@Duration)

                    set @i = @i + 1
                end

                -- Return merged list
                select * from @CargoPortInfo
        END
    -- Sailing Segment
    ELSE
        BEGIN        
            -- Grab total sailing time in minutes
            select @Duration = sum([standard time minute]) from [SAILING SEGMENT DETAIL] ssd where ssd.[SAILING TRIP ID] = (select [sailing trip id] from [sailing trip detail] where [TRIP NUMBER] = @TripNumber and [SAILING SEQUENCE] = @SegmentNumber)

            -- Convert to display format
            set @Duration = dbo.getFormattedDuration(@Duration,'M')

            select (select [port name] from [PORT DETAIL] pd where pd.[PORT ID] = std.[starting PORT ID]) as 'START PORT', 
            (select pd.[port name] from [port DETAIL] pd where pd.[PORT ID] = std.[ending port ID]) as 'END PORT',
            (select @Duration) as 'EXPECTED DURATION'
            from [SAILING TRIP DETAIL] std where [TRIP NUMBER] = @TripNumber and [SAILING SEQUENCE] = @SegmentNumber
        END
END

GO

Я не уверен, что еще я могу добавить сюда. Stackoverflow не позволяет мне публиковать, потому что ему не нравится соотношение текста к коду: | Так вот почему это здесь:)

...