SQL Server
SQL Server
Datev - Get a List of Clients
See more Datev Examples
Demonstrates how to get a list of clients in the accounting:clients Datev API.Chilkat SQL Server Downloads
-- 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
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 GET \
-- --url "https://accounting-clients.api.datev.de/platform/v2/clients?filter=REPLACE_THIS_VALUE&skip=REPLACE_THIS_VALUE&top=REPLACE_THIS_VALUE" \
-- --header "Authorization: Bearer REPLACE_BEARER_TOKEN" \
-- --header "X-Datev-Client-ID: clientId" \
-- --header "accept: application/json;charset=utf-8"
-- Use the following online tool to generate HTTP code from a CURL command
-- Convert a cURL Command to HTTP Source Code
DECLARE @queryParams int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @queryParams OUT
-- ignore = queryParams.UpdateString("filter","REPLACE_THIS_VALUE");
-- ignore = queryParams.UpdateString("skip","REPLACE_THIS_VALUE");
-- ignore = queryParams.UpdateString("top","REPLACE_THIS_VALUE");
-- Adds the "Authorization: Bearer REPLACE_BEARER_TOKEN" header.
EXEC sp_OASetProperty @http, 'AuthToken', 'REPLACE_BEARER_TOKEN'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'accept', 'application/json;charset=utf-8'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Datev-Client-ID', 'DATEV_CLIENT_ID'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpParams', @success OUT, 'GET', 'https://accounting-clients.api.datev.de/platform-sandbox/v2/clients', @queryParams, @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @queryParams
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
DECLARE @jarr int
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @jarr OUT
-- Insert code here to load the above JSON array into the jarr object.
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @jarr, 'Load', @success OUT, @sTmp0
DECLARE @json int
DECLARE @client_number int
DECLARE @consultant_number int
DECLARE @id nvarchar(4000)
DECLARE @name nvarchar(4000)
DECLARE @j int
DECLARE @count_j int
DECLARE @k int
DECLARE @count_k int
DECLARE @strVal nvarchar(4000)
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAGetProperty @jarr, 'Size', @count_i OUT
WHILE @i < @count_i
BEGIN
EXEC sp_OAMethod @jarr, 'ObjectAt', @json OUT, @i
EXEC sp_OAMethod @json, 'IntOf', @client_number OUT, 'client_number'
EXEC sp_OAMethod @json, 'IntOf', @consultant_number OUT, 'consultant_number'
EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'id'
EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'name'
SELECT @j = 0
EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'services'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @json, 'J', @j
EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'services[j].name'
SELECT @k = 0
EXEC sp_OAMethod @json, 'SizeOfArray', @count_k OUT, 'services[j].scopes'
WHILE @k < @count_k
BEGIN
EXEC sp_OASetProperty @json, 'K', @k
EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'services[j].scopes[k]'
SELECT @k = @k + 1
END
SELECT @j = @j + 1
END
EXEC @hr = sp_OADestroy @json
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @queryParams
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @jarr
END
GO