title: MS SQL Stored Procedure for JSON import - Check if input exists
body: <p>I have the following Stored Procedure.</p>
  
  <p>However if "X-Forwarded-For" is empty I would like to retrieve "remote_address" instead.  </p>
  
  <p>How can I do this?</p>
  
  <pre><code>CREATE PROCEDURE dbo.Table(@payload NVARCHAR(MAX))
  WITH ENCRYPTION
  AS    
    SET NOCOUNT ON;  
    IF (ISJSON(@payload) != 1) THROW 50001, 'JSON data expected', 1;
  
    INSERT INTO dbo.Table(IP,Time)  
    SELECT JSONData.IP, getdate()
    FROM OPENJSON (@payload)  
             WITH (  
                IP       varchar(50)  N'$.request.headers."X-Forwarded-For"' 
             )  
    AS JSONData;
  GO
  </code></pre>
  
  <p>The line for remote address looks like:</p>
  
  <pre><code>              IP       varchar(50)  N'$.transaction.remote_address' 
  </code></pre>