Sample code for 30+ languages & platforms
SQL Server

Datev - Upload File (Transfer a file to DATEV data center)

See more Datev Examples

Demonstrates how to transfer a file to the DATEV data center.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Implements the following CURL command:

    -- curl --request POST \
    --   --url "https://accounting-extf-files.api.datev.de/platform-sandbox/v3/clients/REPLACE_CLIENT-ID/extf-files/import" \
    --   --header "Authorization: Bearer REPLACE_BEARER_TOKEN" \
    --   --header "X-Datev-Client-ID: clientId" \
    --   --header "accept: application/json;charset=utf-8" \
    --   --header "content-type: application/octet-stream" \
    --   --header "Filename: EXTF_BS_20200101_1.csv" \
    --   --header "Reference-Id: Buchungsstapel_Verkäufe_2020_01_Nr_001" \
    --   --header "Client-Application-Version: 2.0c" \
    --   --data-binary "@REPLACE_BODY"

    -- Use the following online tool to generate HTTP code from a CURL command
    -- Convert a cURL Command to HTTP Source Code

    DECLARE @bdRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdRequestBody OUT

    EXEC sp_OAMethod @bdRequestBody, 'LoadFile', @success OUT, 'c:/temp/EXTF_BS_20200101_1.csv'
    IF @success <> 1
      BEGIN

        PRINT 'Failed to load the csv file.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdRequestBody
        RETURN
      END

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'accept', 'application/json;charset=utf-8'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Datev-Client-ID', 'clientId'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Client-Application-Version', '2.0c'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Filename', 'EXTF_BS_20200101_1.csv'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Reference-Id', 'Buchungsstapel_Verkäufe_2020_01_Nr_001'
    -- Adds the "Authorization: Bearer REPLACE_BEARER_TOKEN" header.
    EXEC sp_OASetProperty @http, 'AuthToken', 'REPLACE_BEARER_TOKEN'

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    DECLARE @url nvarchar(4000)
    SELECT @url = 'https://accounting-extf-files.api.datev.de/platform-sandbox/v3/clients/DATEV_USER_ID/extf-files/import'
    EXEC sp_OAMethod @http, 'HttpBd', @success OUT, 'POST', @url, @bdRequestBody, 'application/octet-stream', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdRequestBody
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @bdRequestBody
    EXEC @hr = sp_OADestroy @resp


END
GO