SQL Server
SQL Server
Get JSON Details from the Last Operation
See more MIME Examples
Demonstrates GetLastJsonData, which copies structured details from the most recent operation into a JsonObject when such details are available. Many operations produce no JSON data, in which case the object is empty or minimal.
The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Tip: Code to parse the returned JSON can be generated with Chilkat's online tool at https://tools.chilkat.io/jsonParse.
Background. Some Chilkat operations expose extra diagnostic or result information as JSON. GetLastJsonData provides access to that information after the operation completes.
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 @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
DECLARE @mime int
EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @mime, 'LoadMimeFile', @success OUT, 'qa_data/encrypted.eml'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mime
RETURN
END
-- Some operations produce structured JSON details. After an operation, copy those details into a
-- JsonObject with GetLastJsonData. Many methods produce no JSON, in which case the object is
-- little or empty.
DECLARE @pfxPassword nvarchar(4000)
SELECT @pfxPassword = 'myPfxPassword'
EXEC sp_OAMethod @mime, 'AddPfxSourceFile', @success OUT, 'qa_data/recipient.pfx', @pfxPassword
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mime
RETURN
END
EXEC sp_OAMethod @mime, 'Decrypt', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mime
RETURN
END
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @mime, 'GetLastJsonData', NULL, @json
EXEC sp_OASetProperty @json, 'EmitCompact', 0
DECLARE @jsonStr nvarchar(4000)
EXEC sp_OAMethod @json, 'Emit', @jsonStr OUT
PRINT @jsonStr
EXEC @hr = sp_OADestroy @mime
EXEC @hr = sp_OADestroy @json
END
GO