Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.6.0+

Azure Blob: List Versions and Recover Deleted Content

See more Azure Cloud Storage Examples

For a flat-namespace account, list the versions, snapshots and soft-deleted entries for one blob. Keep version IDs and snapshot timestamps as strings. The default action only lists choices; change action deliberately to undelete, restore-version or restore-snapshot and supply the selected ID.

Undelete recovers eligible soft-deleted content within its retention period. When versioning is enabled, undelete does not automatically make an older version the current base blob: restore the selected live version by copying it to the base name. A deleted snapshot/version may first require undelete. Restoring over an existing base requires its current quoted ETag; otherwise this example only creates an absent base. Retention expiration, permanent deletion and immutability policies can prevent recovery. Hierarchical-namespace accounts use a different recovery workflow with deletion IDs.

Uses direct Chilkat.Rest requests and Azure public-cloud endpoints with verified HTTPS. REST requests select service version 2026-04-06. Create local input/output directories before running. Account-key examples read account and access_key string fields from private qa_data/azure/storage.json; do not embed secrets in distributed applications. Entra examples explain their own configuration. Keep credentials, bearer tokens, SAS URLs and diagnostic dumps private. Shared Key is supported only where allowed by account policy; Entra authentication is preferred where available.

Transport success and HTTP status are checked separately. Writes are not automatically replayed after an uncertain response. IDs, ETags, continuation markers and file sizes are strings; counters and explicitly bounded offsets use int. No webhook or web server is required.

Microsoft reference.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  StringBuilder compares text by value in every generated language.
    DECLARE @textCompare int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @textCompare OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    DECLARE @config int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @config OUT

    EXEC sp_OASetProperty @config, 'Strict', 1
    EXEC sp_OAMethod @config, 'LoadFile', @success OUT, 'qa_data/azure/storage.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load private Azure configuration.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'account'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText1 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText1 OUT, '', 1
    EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'account'
    IF (@iTmp0 <> 1) or (@sameText1 = 1)
      BEGIN

        PRINT 'Supply the storage account name.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    --  access_key is the base64 account key, not a connection string or SAS.
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText2 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText2 OUT, '', 1
    EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
    IF (@iTmp0 <> 1) or (@sameText2 = 1)
      BEGIN

        PRINT 'Supply the base64 account access key.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    DECLARE @endpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT

    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'account'
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        RETURN
      END
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.blob.core.windows.net'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        RETURN
      END
    --  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
    --  Keep slash separators; names and paths are case-sensitive.
    DECLARE @resourcePath nvarchar(4000)
    SELECT @resourcePath = '/examplecontainer'
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT

    EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
    EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
    DECLARE @azAuth int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAzureStorage', @azAuth OUT

    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'account'
    EXEC sp_OASetProperty @azAuth, 'Account', @sTmp0
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OASetProperty @azAuth, 'AccessKey', @sTmp0
    EXEC sp_OASetProperty @azAuth, 'Service', 'Blob'
    EXEC sp_OASetProperty @azAuth, 'Scheme', 'SharedKey'
    EXEC sp_OASetProperty @azAuth, 'XMsVersion', '2026-04-06'
    EXEC sp_OAMethod @rest, 'SetAuthAzureStorage', @success OUT, @azAuth
    IF @success <> 1
      BEGIN

        PRINT 'Could not configure Shared Key signing.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        RETURN
      END
    DECLARE @connection int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT

    EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @connection, 'MaxReadIdleMs', 60000
    EXEC sp_OASetProperty @connection, 'MaxSendIdleMs', 60000
    EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
    IF @success <> 1
      BEGIN

        PRINT 'TLS connection failed; inspect diagnostics privately.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    --  Do not replay a write automatically after losing its response.
    EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    DECLARE @responseText nvarchar(4000)

    DECLARE @result int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @result OUT

    EXEC sp_OASetProperty @result, 'Strict', 1
    --  Flat-namespace Blob account. Default is inspection only.
    --  Choose list, undelete, restore-version, or restore-snapshot.
    DECLARE @action nvarchar(4000)
    SELECT @action = 'list'
    DECLARE @selectedId nvarchar(4000)
    SELECT @selectedId = ''
    --  For restoring over an existing base blob, supply its current quoted ETag.
    --  Empty means restore only when the base blob is absent.
    DECLARE @destinationEtag nvarchar(4000)
    SELECT @destinationEtag = ''
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'restype', 'container'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'comp', 'list'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'prefix', 'example.bin'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'include', 'versions,snapshots,deleted,deletedwithversions'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'maxresults', '1000'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @azAuth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    DECLARE @seenMarkers int
    EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @seenMarkers OUT

    DECLARE @marker nvarchar(4000)
    SELECT @marker = ''
    DECLARE @more int
    SELECT @more = 1
    DECLARE @page int
    SELECT @page = 0
    WHILE (@more = 1)
      BEGIN
        IF @page >= 1000
          BEGIN

            PRINT 'Page limit reached; results are incomplete. Save the continuation marker.'
            EXEC @hr = sp_OADestroy @textCompare
            EXEC @hr = sp_OADestroy @config
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @seenMarkers
            RETURN
          END
        EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'GET', @resourcePath
        EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'Request did not complete; outcome may be uncertain.'
            EXEC @hr = sp_OADestroy @textCompare
            EXEC @hr = sp_OADestroy @config
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @seenMarkers
            RETURN
          END
        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
            PRINT 'HTTP status: ' + @iTmp0

            EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-error-code'
            PRINT 'Azure error code: ' + @sTmp0

            EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-request-id'
            PRINT 'Request ID: ' + @sTmp0
            --  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
            EXEC @hr = sp_OADestroy @textCompare
            EXEC @hr = sp_OADestroy @config
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @seenMarkers
            RETURN
          END
        EXEC sp_OAMethod @result, 'LoadXml', @success OUT, @responseText
        IF @success <> 1
          BEGIN

            PRINT 'Expected well-formed XML.'
            EXEC @hr = sp_OADestroy @textCompare
            EXEC @hr = sp_OADestroy @config
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @seenMarkers
            RETURN
          END
        EXEC sp_OAMethod @result, 'TagEquals', @iTmp0 OUT, 'EnumerationResults'
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'Unexpected XML response root.'
            EXEC @hr = sp_OADestroy @textCompare
            EXEC @hr = sp_OADestroy @config
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @seenMarkers
            RETURN
          END
        DECLARE @count int
        EXEC sp_OAMethod @result, 'NumChildrenAt', @count OUT, 'Blobs'
        DECLARE @i int
        SELECT @i = 0
        WHILE (@i < @count)
          BEGIN
            EXEC sp_OASetProperty @result, 'I', @i
            EXEC sp_OAMethod @textCompare, 'Clear', NULL
            EXEC sp_OAMethod @result, 'GetChildContent', @sTmp0 OUT, 'Blobs|Blob[i]|Name'
            EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
            DECLARE @sameText3 int
            EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText3 OUT, 'example.bin', 1
            IF @sameText3 = 1
              BEGIN

                EXEC sp_OAMethod @result, 'GetChildContent', @sTmp0 OUT, 'Blobs|Blob[i]|VersionId'
                PRINT 'Version ID: ' + @sTmp0

                EXEC sp_OAMethod @result, 'GetChildContent', @sTmp0 OUT, 'Blobs|Blob[i]|Snapshot'
                PRINT 'Snapshot: ' + @sTmp0

                EXEC sp_OAMethod @result, 'GetChildContent', @sTmp0 OUT, 'Blobs|Blob[i]|Deleted'
                PRINT 'Deleted: ' + @sTmp0

                EXEC sp_OAMethod @result, 'GetChildContent', @sTmp0 OUT, 'Blobs|Blob[i]|IsCurrentVersion'
                PRINT 'Current version: ' + @sTmp0

                EXEC sp_OAMethod @result, 'GetChildContent', @sTmp0 OUT, 'Blobs|Blob[i]|HasVersionsOnly'
                PRINT 'Has versions only: ' + @sTmp0
              END
            SELECT @i = @i + 1
          END
        EXEC sp_OAMethod @result, 'GetChildContent', @marker OUT, 'NextMarker'
        SELECT @page = @page + 1
        EXEC sp_OAMethod @textCompare, 'Clear', NULL
        EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @marker
        DECLARE @sameText4 int
        EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText4 OUT, '', 1
        IF @sameText4 = 1
          BEGIN
            SELECT @more = 0
          END
        ELSE
          BEGIN
            EXEC sp_OAMethod @seenMarkers, 'Contains', @iTmp0 OUT, @marker
            IF @iTmp0 = 1
              BEGIN

                PRINT 'Repeated marker; results are incomplete.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
            EXEC sp_OAMethod @seenMarkers, 'AddInt', @success OUT, @marker, 1
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
            IF @page > 1
              BEGIN
                EXEC sp_OAMethod @rest, 'RemoveQueryParam', @success OUT, 'marker'
                IF @success <> 1
                  BEGIN

                    PRINT 'Could not prepare the request.'
                    EXEC @hr = sp_OADestroy @textCompare
                    EXEC @hr = sp_OADestroy @config
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @azAuth
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @seenMarkers
                    RETURN
                  END
              END
            EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'marker', @marker
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
            EXEC sp_OAMethod @rest, 'Disconnect', @success OUT, 10
            EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
            EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
            IF @success <> 1
              BEGIN

                PRINT 'TLS reconnection failed.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
            EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 0
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
          END
      END
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @action
    DECLARE @sameText5 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText5 OUT, 'list', 1
    IF @sameText5 <> 1
      BEGIN
        EXEC sp_OAMethod @rest, 'ClearAllQueryParams', @success OUT
        IF @success <> 1
          BEGIN

            PRINT 'Could not prepare the request.'
            EXEC @hr = sp_OADestroy @textCompare
            EXEC @hr = sp_OADestroy @config
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @azAuth
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @seenMarkers
            RETURN
          END
        SELECT @resourcePath = '/examplecontainer/example.bin'
        EXEC sp_OAMethod @textCompare, 'Clear', NULL
        EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @action
        DECLARE @sameText6 int
        EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText6 OUT, 'undelete', 1
        IF @sameText6 = 1
          BEGIN
            EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'comp', 'undelete'
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
            EXEC sp_OAMethod @rest, 'Disconnect', @success OUT, 10
            EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
            EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
            IF @success <> 1
              BEGIN

                PRINT 'TLS reconnection failed.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
            EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 0
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
            EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'PUT', @resourcePath
            EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
            IF @iTmp0 <> 1
              BEGIN

                PRINT 'Request did not complete; outcome may be uncertain.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
            EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
            IF @iTmp0 <> 200
              BEGIN

                EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
                PRINT 'HTTP status: ' + @iTmp0

                EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-error-code'
                PRINT 'Azure error code: ' + @sTmp0

                EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-request-id'
                PRINT 'Request ID: ' + @sTmp0
                --  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END

            PRINT 'Undelete accepted. Rerun listing before selecting a version or snapshot.'
          END
        ELSE
          BEGIN
            EXEC sp_OAMethod @textCompare, 'Clear', NULL
            EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @action
            DECLARE @sameText7 int
            EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText7 OUT, 'restore-version', 1
            EXEC sp_OAMethod @textCompare, 'Clear', NULL
            EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @action
            DECLARE @sameText8 int
            EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText8 OUT, 'restore-snapshot', 1
            IF (@sameText7 <> 1) and (@sameText8 <> 1)
              BEGIN

                PRINT 'Unknown recovery action.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
            EXEC sp_OAMethod @textCompare, 'Clear', NULL
            EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @selectedId
            DECLARE @sameText9 int
            EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText9 OUT, '', 1
            IF @sameText9 = 1
              BEGIN

                PRINT 'Select a version or snapshot ID from the listing first.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                RETURN
              END
            DECLARE @encodedId int
            EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @encodedId OUT

            EXEC sp_OAMethod @encodedId, 'Append', @success OUT, @selectedId
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                RETURN
              END
            EXEC sp_OAMethod @encodedId, 'Encode', @success OUT, 'url', 'utf-8'
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                RETURN
              END
            DECLARE @restoreUrl int
            EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @restoreUrl OUT

            EXEC sp_OAMethod @restoreUrl, 'Append', @success OUT, 'https://'
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
            EXEC sp_OAMethod @restoreUrl, 'Append', @success OUT, @sTmp0
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            EXEC sp_OAMethod @restoreUrl, 'Append', @success OUT, @resourcePath
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            EXEC sp_OAMethod @textCompare, 'Clear', NULL
            EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @action
            DECLARE @sameText10 int
            EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText10 OUT, 'restore-version', 1
            IF @sameText10 = 1
              BEGIN
                EXEC sp_OAMethod @restoreUrl, 'Append', @success OUT, '?versionid='
                IF @success <> 1
                  BEGIN

                    PRINT 'Could not prepare the request.'
                    EXEC @hr = sp_OADestroy @textCompare
                    EXEC @hr = sp_OADestroy @config
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @azAuth
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @seenMarkers
                    EXEC @hr = sp_OADestroy @encodedId
                    EXEC @hr = sp_OADestroy @restoreUrl
                    RETURN
                  END
              END
            ELSE
              BEGIN
                EXEC sp_OAMethod @restoreUrl, 'Append', @success OUT, '?snapshot='
                IF @success <> 1
                  BEGIN

                    PRINT 'Could not prepare the request.'
                    EXEC @hr = sp_OADestroy @textCompare
                    EXEC @hr = sp_OADestroy @config
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @azAuth
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @seenMarkers
                    EXEC @hr = sp_OADestroy @encodedId
                    EXEC @hr = sp_OADestroy @restoreUrl
                    RETURN
                  END
              END
            EXEC sp_OAMethod @encodedId, 'GetAsString', @sTmp0 OUT
            EXEC sp_OAMethod @restoreUrl, 'Append', @success OUT, @sTmp0
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            EXEC sp_OAMethod @restoreUrl, 'GetAsString', @sTmp0 OUT
            EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'x-ms-copy-source', @sTmp0
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            EXEC sp_OAMethod @textCompare, 'Clear', NULL
            EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @destinationEtag
            DECLARE @sameText11 int
            EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText11 OUT, '', 1
            IF @sameText11 = 1
              BEGIN
                EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'If-None-Match', '*'
                IF @success <> 1
                  BEGIN

                    PRINT 'Could not prepare the request.'
                    EXEC @hr = sp_OADestroy @textCompare
                    EXEC @hr = sp_OADestroy @config
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @azAuth
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @seenMarkers
                    EXEC @hr = sp_OADestroy @encodedId
                    EXEC @hr = sp_OADestroy @restoreUrl
                    RETURN
                  END
              END
            ELSE
              BEGIN
                EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'If-Match', @destinationEtag
                IF @success <> 1
                  BEGIN

                    PRINT 'Could not prepare the request.'
                    EXEC @hr = sp_OADestroy @textCompare
                    EXEC @hr = sp_OADestroy @config
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @azAuth
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @seenMarkers
                    EXEC @hr = sp_OADestroy @encodedId
                    EXEC @hr = sp_OADestroy @restoreUrl
                    RETURN
                  END
              END
            EXEC sp_OAMethod @rest, 'Disconnect', @success OUT, 10
            EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
            EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
            IF @success <> 1
              BEGIN

                PRINT 'TLS reconnection failed.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 0
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'PUT', @resourcePath
            EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
            IF @iTmp0 <> 1
              BEGIN

                PRINT 'Request did not complete; outcome may be uncertain.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
            IF @iTmp0 <> 202
              BEGIN

                EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
                PRINT 'HTTP status: ' + @iTmp0

                EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-error-code'
                PRINT 'Azure error code: ' + @sTmp0

                EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-request-id'
                PRINT 'Request ID: ' + @sTmp0
                --  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            DECLARE @copyId nvarchar(4000)
            EXEC sp_OAMethod @rest, 'ResponseHdrByName', @copyId OUT, 'x-ms-copy-id'
            EXEC sp_OAMethod @textCompare, 'Clear', NULL
            EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @copyId
            DECLARE @sameText12 int
            EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText12 OUT, '', 1
            IF @sameText12 = 1
              BEGIN

                PRINT 'Copy accepted without a copy ID; inspect before retrying.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END

            PRINT 'Copy ID: ' + @copyId
            --  Save copyId and destination in application state if polling must survive restart.
            EXEC sp_OAMethod @rest, 'ClearAllHeaders', @success OUT
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            EXEC sp_OAMethod @rest, 'ClearAllQueryParams', @success OUT
            IF @success <> 1
              BEGIN

                PRINT 'Could not prepare the request.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END
            DECLARE @polls int
            SELECT @polls = 0
            DECLARE @completed int
            SELECT @completed = 0
            WHILE ((@polls < 20) and (@completed <> 1))
              BEGIN
                EXEC sp_OAMethod @rest, 'Disconnect', @success OUT, 10
                EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
                EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
                IF @success <> 1
                  BEGIN

                    PRINT 'TLS reconnection failed.'
                    EXEC @hr = sp_OADestroy @textCompare
                    EXEC @hr = sp_OADestroy @config
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @azAuth
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @seenMarkers
                    EXEC @hr = sp_OADestroy @encodedId
                    EXEC @hr = sp_OADestroy @restoreUrl
                    RETURN
                  END
                EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 0
                IF @success <> 1
                  BEGIN

                    PRINT 'Could not prepare the request.'
                    EXEC @hr = sp_OADestroy @textCompare
                    EXEC @hr = sp_OADestroy @config
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @azAuth
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @seenMarkers
                    EXEC @hr = sp_OADestroy @encodedId
                    EXEC @hr = sp_OADestroy @restoreUrl
                    RETURN
                  END
                EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'HEAD', @resourcePath
                EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
                IF @iTmp0 <> 1
                  BEGIN

                    PRINT 'Request did not complete; outcome may be uncertain.'
                    EXEC @hr = sp_OADestroy @textCompare
                    EXEC @hr = sp_OADestroy @config
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @azAuth
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @seenMarkers
                    EXEC @hr = sp_OADestroy @encodedId
                    EXEC @hr = sp_OADestroy @restoreUrl
                    RETURN
                  END
                EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
                IF @iTmp0 <> 200
                  BEGIN

                    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
                    PRINT 'HTTP status: ' + @iTmp0

                    EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-error-code'
                    PRINT 'Azure error code: ' + @sTmp0

                    EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-request-id'
                    PRINT 'Request ID: ' + @sTmp0
                    --  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
                    EXEC @hr = sp_OADestroy @textCompare
                    EXEC @hr = sp_OADestroy @config
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @azAuth
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @seenMarkers
                    EXEC @hr = sp_OADestroy @encodedId
                    EXEC @hr = sp_OADestroy @restoreUrl
                    RETURN
                  END
                EXEC sp_OAMethod @textCompare, 'Clear', NULL
                EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-copy-id'
                EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
                DECLARE @sameText13 int
                EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText13 OUT, @copyId, 1
                IF @sameText13 <> 1
                  BEGIN

                    PRINT 'Destination no longer reports this copy ID.'
                    EXEC @hr = sp_OADestroy @textCompare
                    EXEC @hr = sp_OADestroy @config
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @azAuth
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @seenMarkers
                    EXEC @hr = sp_OADestroy @encodedId
                    EXEC @hr = sp_OADestroy @restoreUrl
                    RETURN
                  END
                DECLARE @copyStatus nvarchar(4000)
                EXEC sp_OAMethod @rest, 'ResponseHdrByName', @copyStatus OUT, 'x-ms-copy-status'
                EXEC sp_OAMethod @textCompare, 'Clear', NULL
                EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @copyStatus
                DECLARE @sameText14 int
                EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText14 OUT, 'success', 1
                IF @sameText14 = 1
                  BEGIN
                    SELECT @completed = 1
                  END
                ELSE
                  BEGIN
                    EXEC sp_OAMethod @textCompare, 'Clear', NULL
                    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @copyStatus
                    DECLARE @sameText15 int
                    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText15 OUT, 'pending', 1
                    IF @sameText15 <> 1
                      BEGIN

                        PRINT 'Copy failed, was aborted, or returned an unexpected status; inspect copy diagnostics privately.'
                        EXEC @hr = sp_OADestroy @textCompare
                        EXEC @hr = sp_OADestroy @config
                        EXEC @hr = sp_OADestroy @endpoint
                        EXEC @hr = sp_OADestroy @rest
                        EXEC @hr = sp_OADestroy @azAuth
                        EXEC @hr = sp_OADestroy @connection
                        EXEC @hr = sp_OADestroy @result
                        EXEC @hr = sp_OADestroy @seenMarkers
                        EXEC @hr = sp_OADestroy @encodedId
                        EXEC @hr = sp_OADestroy @restoreUrl
                        RETURN
                      END
                    --  Pause between polls; the copy can outlast this example's bounded wait.
                    EXEC sp_OAMethod @connection, 'SleepMs', NULL, 1000
                  END
                SELECT @polls = @polls + 1
              END
            IF @completed <> 1
              BEGIN

                PRINT 'Copy is still pending. Continue polling this ID; do not start another copy.'
                EXEC @hr = sp_OADestroy @textCompare
                EXEC @hr = sp_OADestroy @config
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @azAuth
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @seenMarkers
                EXEC @hr = sp_OADestroy @encodedId
                EXEC @hr = sp_OADestroy @restoreUrl
                RETURN
              END

            PRINT 'Copy completed.'
          END
      END

    PRINT 'Recovery workflow finished.'

    EXEC @hr = sp_OADestroy @textCompare
    EXEC @hr = sp_OADestroy @config
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @azAuth
    EXEC @hr = sp_OADestroy @connection
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @seenMarkers
    EXEC @hr = sp_OADestroy @encodedId
    EXEC @hr = sp_OADestroy @restoreUrl


END
GO