When I execute this query(it's a scalar function) I get the result I want. It returns a shipping number.
select ShipmentNo from openquery([SERVER_IP], 'SET FMTONLY OFF EXEC GsvStaging.dbo.Paul_GetPoShippingLabels @CartId = NULL, @CpPoNo = ''G015246''')
However, when I swap out G015246
with the parameter @PO
:
select @ShipmentLabels = ShipmentNo from openquery([SERVER_IP], 'SET FMTONLY OFF EXEC GsvStaging.dbo.Paul_GetPoShippingLabels @CartId = NULL, @CpPoNo = ''@PO''')
For some reason the return value becomes null with this change.
My return statement for both is:
RETURN ISNULL(@ShipmentLabels, @PO)
where @ShipmentLabels
is the desired return value. Since it is null it returns @PO
instead, which ends up being G015246
which seems right.
Anyone notice anything wrong with how I've added the @PO
parameter to the sql statement?
Stored Procedure that calls the Function(fnGetTheShippingLabels): I guess the best way to describe this SP is the starting point.
ALTER PROCEDURE [dbo].[StartingSP]
@JobId INT = 1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ErrorMsg VARCHAR(256)
SELECT OC.PoNo,
V.FirstName,
V.LastName,
dbo.fnGetTradeAppFacilityName(NULL,OC.PoNo) TradeAppFacilityName,
OC.ReportFileName, dbo.fnGetTheShippingLabels(NULL,OC.PoNo) ShippingLabels
FROM PoHdr P
INNER JOIN OrderConfirmation OC ON
OC.PoNo = P.PoNo
INNER JOIN CpVendor V ON
V.VendNo = P.VendNo
WHERE OC.JobId = @JobId
SELECT @ErrorMsg ErrorMsg
END
As you can see from above, the function is in the SELECT statement of the SP.
Complete Scalar Function:
ALTER FUNCTION [dbo].[fnGetTheShippingLabels]
(
@PoHdrRecId INT,
@PoNo VARCHAR(20)
)
RETURNS VARCHAR(220)
AS
BEGIN
DECLARE @PO VARCHAR(20)= @PoNo
DECLARE @ShipmentLabels VARCHAR(220)
select @ShipmentLabels = ShipmentNo from openquery([SERVER_IP], 'SET FMTONLY OFF EXEC GsvStaging.dbo.Paul_GetPoShippingLabels @CartId = NULL, @CpPoNo = ''G015246''')
--FROM dbo.PoHdr WHERE PoNo = @PoNo
-- Return the result of the functionRETURN ISNULL(@ShipmentLabels, @PO)
END
Trying a different way of writing query now, this doesn't work fyi:
Declare @TSQL VarChar(8000)
set @TSQL = N'select @ShipmentLabels = ShipmentNo from openquery([SERVER_IP],SET FMTONLY OFF EXEC GsvStaging.dbo.Paul_GetPoShippingLabels @CartId = NULL, @CpPoNo =' + @PO +''
exec sp_executesql @TSQL, N'@ShipmentLabels output', @ShipmentLabels = @ShipmentLabels out