Using the Chilkat ActiveX in SQL Server
call Chilkat from T-SQL stored procedures with sp_OACreate, sp_OAMethod, and
sp_OAGetProperty
SQL Server on Windows · OLE Automation procedures · Chilkat ActiveX v11
SQL Server's OLE Automation procedures let T-SQL create COM objects and call their methods and properties. With the
Chilkat ActiveX installed on the database server, a stored procedure can call a REST API, send email, transfer files with
SFTP, sign or encrypt data, parse JSON and XML, and use the rest of the Chilkat API. Chilkat runs inside the SQL Server
process (sqlservr.exe), on the server itself.
The 60-second version
- On the server that runs SQL Server, install the 64-bit per-machine Chilkat ActiveX.
- Enable the Ole Automation Procedures option (a sysadmin must do this).
- Create and run the first stored procedure.
On this page: Install Enable OLE Automation First procedure JSON as rows String & binary limits T-SQL tips Security Troubleshooting
Setup
Install the Chilkat ActiveX on the database server
Install Chilkat on the computer where the SQL Server service runs, not on the computers that connect to it. Use the per-machine installer: SQL Server runs under its own service account, which can't see a Chilkat registered for your Windows account only.
The DLL must have the same bitness as SQL Server. Current SQL Server versions are 64-bit, so install the 64-bit
Chilkat ActiveX. Only an old 32-bit SQL Server instance needs the 32-bit one. If you're unsure, SELECT
@@VERSION shows (X64) for a 64-bit instance.
SQL Server on Linux and managed cloud databases can't load a Windows COM DLL, so this approach needs SQL Server on
Windows. ZIP downloads with a register.bat script are on the ActiveX download
page; if you use them, run the script as administrator and keep the DLL on a local drive. The downloads are the full
version and are fully functional for a 30-day evaluation.
Enable Ole Automation Procedures
The sp_OA procedures are disabled by default. A member of the sysadmin role enables them once per
instance:
EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;
Read Security before enabling this on a production server.
Examples
Your first stored procedure: unlock Chilkat and fetch a URL
Every sp_OA call follows the same pattern: sp_OACreate returns an object token,
sp_OAMethod and sp_OAGetProperty use it, and sp_OADestroy releases it. Chilkat must be
unlocked with UnlockBundle; unlocking lasts for the life of the SQL Server process, and calling it again is
harmless, so each procedure can simply unlock first. During the 30-day trial any string works; after purchase, use your
license code.
CREATE OR ALTER PROCEDURE dbo.ChilkatHello
AS
BEGIN
SET NOCOUNT ON;
DECLARE @hr int, @success int, @glob int, @http int;
DECLARE @text nvarchar(4000), @err nvarchar(4000);
EXEC @hr = sp_OACreate 'Chilkat.Global', @glob OUT;
IF @hr <> 0
BEGIN
PRINT 'Could not create Chilkat.Global. HRESULT: 0x' + CONVERT(varchar(8), CONVERT(binary(4), @hr), 2);
RETURN;
END
EXEC sp_OAMethod @glob, 'UnlockBundle', @success OUT, 'Anything for 30-day trial';
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @glob, 'LastErrorText', @err OUT;
PRINT @err;
EXEC sp_OADestroy @glob;
RETURN;
END
EXEC sp_OADestroy @glob;
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT;
EXEC sp_OAMethod @http, 'QuickGetStr', @text OUT, 'https://www.chilkatsoft.com/helloworld.txt';
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @success OUT;
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @err OUT;
PRINT @err;
END
ELSE
PRINT @text;
EXEC sp_OADestroy @http;
END
GO
EXEC dbo.ChilkatHello;
The Messages tab shows Hello World!. The ProgID is Chilkat. plus the class name, for example
Chilkat.JsonObject or Chilkat.Crypt2. Methods without a return value are called with
NULL in place of the output variable: EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept',
'application/json';
Call a web service and return JSON as rows
Real responses are often longer than 4,000 characters, the most an sp_OAMethod output variable can
reliably hold. This procedure turns on Chilkat's KeepStringResult, reads the full response from
LastStringResult into an nvarchar(max), and returns the JSON members as a result set with
OPENJSON (SQL Server 2016 and later, database compatibility level 130 or higher):
CREATE OR ALTER PROCEDURE dbo.ChilkatFruit
AS
BEGIN
SET NOCOUNT ON;
DECLARE @hr int, @success int, @glob int, @http int;
DECLARE @dummy nvarchar(4000);
DECLARE @errText TABLE (msg nvarchar(max));
DECLARE @body TABLE (json nvarchar(max));
EXEC @hr = sp_OACreate 'Chilkat.Global', @glob OUT;
IF @hr <> 0 BEGIN RAISERROR('Could not create Chilkat.Global.', 16, 1); RETURN; END
EXEC sp_OAMethod @glob, 'UnlockBundle', @success OUT, 'Anything for 30-day trial';
EXEC sp_OASetProperty @glob, 'KeepStringResult', 1; -- keep full string results in LastStringResult
EXEC sp_OADestroy @glob;
IF @success <> 1 BEGIN RAISERROR('Chilkat could not be unlocked.', 16, 1); RETURN; END
-- {"apple":"red", "lime":"green", "banana":"yellow", ...}
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT;
EXEC sp_OASetProperty @http, 'ConnectTimeout', 10;
EXEC sp_OASetProperty @http, 'ReadTimeout', 30;
EXEC sp_OAMethod @http, 'QuickGetStr', @dummy OUT, 'https://www.chilkatsoft.com/exampledata/sample.json';
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @success OUT;
IF @success <> 1
BEGIN
INSERT INTO @errText EXEC sp_OAGetProperty @http, 'LastErrorText';
SELECT msg AS LastErrorText FROM @errText;
EXEC sp_OADestroy @http;
RETURN;
END
INSERT INTO @body EXEC sp_OAGetProperty @http, 'LastStringResult';
EXEC sp_OADestroy @http;
DECLARE @json nvarchar(max) = (SELECT json FROM @body);
SELECT [key] AS fruit, [value] AS color FROM OPENJSON(@json);
END
Reading LastErrorText the same way, through a table variable, avoids cutting off a long error
report.
String and binary limits of the sp_OA procedures
These limits belong to SQL Server's OLE Automation procedures, not to Chilkat, and Chilkat has workarounds for each:
| Limit | Workaround |
|---|---|
A string returned into an output variable is cut off at about 4,000 characters, even if the variable is
nvarchar(max). | Set KeepStringResult on Chilkat.Global and read
LastStringResult into a table variable, as above. See The
nvarchar(max) limitation and SQL Server methods returning large
strings. |
A method that returns bytes can't fill a varbinary output variable. | Set
KeepBinaryResult on Chilkat.Global and read LastBinaryResult, or ask Chilkat for Base64 or hex text. See
The varbinary(max) limitation. |
Things to know when calling Chilkat from T-SQL
| Topic | What to do |
|---|---|
| Two kinds of errors | The @hr value returned by an sp_OA procedure reports whether
the call could be made at all (object not registered, misspelled method name). Chilkat's own result reports whether the
operation succeeded: check the method's return value (1 or 0) or LastMethodSuccess,
and read LastErrorText. |
| Release objects | Call sp_OADestroy for every object you create, including on early
RETURN paths. SQL Server also destroys remaining objects at the end of the batch. |
| Timeouts | A Chilkat call blocks the session until it finishes. Set ConnectTimeout and
ReadTimeout (or the class's equivalent) so a slow server can't hold a session, and its locks, indefinitely.
Don't call external services from inside long transactions or triggers. |
| Files and network | File paths refer to the database server's file system, and Chilkat runs as the SQL Server service account. That account needs access to the folders you use and outbound network access to the services you call. |
| Passing objects | Pass an object token where a Chilkat method expects an object, for example the
@resp token from sp_OACreate 'Chilkat.HttpResponse' as the last argument of
HttpStr. A method that returns an object returns a new token in its output variable; destroy it too. |
| Chilkat v9.5 code | v11 ProgIDs are Chilkat.Http, Chilkat.Global, and so on. Older
code uses Chilkat_9_5_0.Http. Both versions can be installed side by side; see
Chilkat ActiveX v11 and v9.5 on the same computer. |
Security
- Enabling Ole Automation Procedures lets code with permission to run the
sp_OAprocedures create COM objects inside the SQL Server process. Some security baselines require the option to be off, so agree on it with whoever manages the server. - By default only members of sysadmin can run the
sp_OAprocedures. Rather than grantingEXECUTEon them to application users, put the Chilkat calls in stored procedures that you control. - Keep secrets such as passwords, private keys, and your Chilkat unlock code out of code that other users can read, and restrict who can alter those procedures.
Troubleshooting
| Message | Cause and fix |
|---|---|
| SQL Server blocked access to procedure 'sys.sp_OACreate' of component 'Ole Automation Procedures' because this component is turned off as part of the security configuration for this server. | The option is off. Enable Ole Automation Procedures. |
| The EXECUTE permission was denied on the object 'sp_OACreate' | The login isn't a sysadmin and hasn't been granted EXECUTE on the sp_OA procedures. See
Security. |
sp_OACreate returns 0x800401F3 (Invalid class string) or 0x80040154 (Class
not registered) |
The Chilkat ActiveX isn't registered on the database server with SQL Server's bitness, or it's registered per-user. Install the 64-bit per-machine MSI on the server, and check the ProgID spelling. |
sp_OAMethod returns 0x80020006 (Unknown name) |
The method or property name is misspelled, or the installed Chilkat version is older than the one the code was written
for. Check the name in the reference documentation.
sp_OAGetErrorInfo returns the error details for any failed sp_OA call. |
| A returned string is cut off | The 4,000-character output limit. See String and binary limits. |
| A Chilkat method returns 0 or an empty string | The operation itself failed (a network error, a bad password, an invalid file path, and so on). Read
LastErrorText; it names the cause. |
Next Steps
- 📖 Chilkat SQL Server Reference Documentation — every class, method, and property
- 💻 Chilkat SQL Server examples — thousands of ready-to-run examples
- 📦 Chilkat ActiveX downloads (MSI installers and ZIP files)
- ✉ Contact Chilkat support
SQL Server is a trademark of the Microsoft group of companies.