Stored Procedures [rdld].[UpdateXmlOrderLink]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@orderNonvarchar(64)128
@externalKeyvarchar(60)60
Permissions
TypeActionOwning Principal
GrantExecuteMssExec
SQL Script

/****** Object:  StoredProcedure [rdld].[UpdateXmlOrderLink]  ******/
-- =============================================
-- Author:     Jeff Spindler
-- Date: 4/2023
-- Description:   
-- =============================================
CREATE PROCEDURE [rdld].[UpdateXmlOrderLink]
  @orderNo AS     NVARCHAR(64),
  @externalKey AS VARCHAR(60) -- ShipmentID
AS
BEGIN
   SET NOCOUNT ON;

   DECLARE @integratorKeyId AS NVARCHAR(4)= 'rdld';
   DECLARE @OrdersPriKey INT=
   (
       SELECT TOP 1 PriKey
       FROM
            Orders
       WHERE        OrderNo = @orderNo
   );

   IF @OrdersPriKey IS NULL
   BEGIN
      SELECT 13001 AS ErrorCode,
             'OrderNo ' + @OrderNo + ' not found' AS ErrorMessage FOR XML RAW;
      RETURN;
   END;

   DECLARE @theVendorConnectXmlSystemID int = dbo.GetVendorConnectXmlSystemID(@integratorKeyId);
   DECLARE @theXmlOrderLinkNewRows int;

   INSERT INTO XmlOrderLink
   (
      xmlsystemfid,
      OrderFID,
      ExternalKey,
      SecondaryExternalKey
   )
   SELECT
      xmlsystemfid = @theVendorConnectXmlSystemID,
      OrderFID = @OrdersPriKey,
      ExternalKey = @externalKey,
      SecondaryExternalKey = NULL
   WHERE not exists(
      select top 1 1
      from XmlOrderLink
      where xmlsystemfid = @theVendorConnectXmlSystemID and
         OrderFID = @OrdersPriKey
   );

   SET @theXmlOrderLinkNewRows = @@ROWCOUNT;

   if( @theXmlOrderLinkNewRows = 0 )
   BEGIN
      UPDATE XmlOrderLink SET
         ExternalKey = @externalKey,
         SecondaryExternalKey = NULL
      WHERE xmlsystemfid = @theVendorConnectXmlSystemID and
         OrderFID = @OrdersPriKey;
   END;

   DECLARE @AppSysUser INT =
   (
       SELECT SysUserID
       FROM
            SysUser
       WHERE  LastName = 'Application'
              AND FirstName = 'Roadload'
   );

   UPDATE Orders
     SET  CreatedBy = @AppSysUser
   WHERE  PriKey = @OrdersPriKey;

   SELECT 0 AS ErrorCode,
             'OrderNo' + @OrderNo + ' link changed.' AS ErrorMessage FOR XML RAW;
   RETURN 0;
END;
GO
GRANT EXECUTE ON  [rdld].[UpdateXmlOrderLink] TO [MssExec]
GO
Uses