---
product: Chilkat
product_version: 11.7.0
language: sqlserver
language_label: SQL Server
class: MessageSet
license: bundle
deprecated: false
reference_html: https://www.chilkatsoft.com/refdoc/sqlserver_MessageSetRef.html
---
# Chilkat MessageSet — SQL Server — v11.7.0

> Chilkat 11.7.0 · language=sqlserver · class=MessageSet
> Other versions: replace `11.7.0` in this URL. Other languages: `https://www.chilkatsoft.com/ai-docs/v/11.7.0/<lang>/classes/MessageSet.md`.
> Language-wide rules (install, unlock, error model, strings/bytes): `https://www.chilkatsoft.com/ai-docs/v/11.7.0/sqlserver/guide.md`.

## Purpose

**Represent and pass sets of IMAP message identifiers.**

`Chilkat.MessageSet` represents a set of IMAP message identifiers used by `Chilkat.Imap`. It is a helper object for passing groups of messages to IMAP methods and for receiving groups of message IDs returned by IMAP search, fetch, copy, delete, or flag-related operations. A `MessageSet` may contain either UIDs or sequence numbers, and the `HasUids` setting identifies which form is being used.

**IMAP message IDs**

Store the identifiers used by IMAP operations, either as message UIDs or as sequence numbers.

**Used by Chilkat.Imap**

Pass message sets to IMAP methods that operate on multiple messages, or receive them from IMAP methods that return matching messages.

**UID awareness**

Use `HasUids` to indicate whether the contained identifiers are stable IMAP UIDs or mailbox-relative sequence numbers.

**Add and remove IDs**

Insert message identifiers, remove identifiers, check whether an ID is present, and build the exact message group needed for an IMAP operation.

**Compact string formats**

Load or emit compact IMAP-style message ranges such as comma-separated lists and range expressions.

**Set operations**

Subtract one message set from another when application logic needs to exclude already-processed or unwanted messages.

**Common pattern:** Use `MessageSet` only for IMAP message identifiers, not as a general-purpose collection. Obtain a message set from an IMAP search or build one manually, confirm whether it contains UIDs or sequence numbers, then pass it to the appropriate `Chilkat.Imap` method for fetching, copying, deleting, flagging, or otherwise processing the selected messages.

For an extended overview, see [MessageSet Class Overview](https://www.chilkatsoft.com/messageset_class_overview.asp).

## Object creation, unlocking and error handling

**Object Creation**

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

-- ... use @messageSet ...

EXEC @hr = sp_OADestroy @messageSet
```

T-SQL uses the Chilkat ActiveX through the OLE Automation stored procedures. They must be enabled once on the server (`EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;`), and the Chilkat ActiveX registered must match the bitness of the SQL Server instance (64-bit for a 64-bit SQL Server). To bind to a specific major version of Chilkat, append the major version number to the ProgID, such as `sp_OACreate 'Chilkat.MessageSet.11'` for Chilkat v11.\*.\*.

`sp_OACreate` returns an object token (an `int`) that is passed as the first argument of every `sp_OAMethod`, `sp_OAGetProperty` and `sp_OASetProperty` call, and released with `sp_OADestroy`. Objects returned by methods (such as an `HttpResponse` or `JsonObject`) are also tokens received through an `int` OUT parameter; they must likewise be destroyed, and the OUT parameter is `NULL` when the method fails to return an object. Objects passed as arguments are passed by their token. When an OLE Automation procedure itself fails (non-zero `@hr`), `sp_OAGetErrorInfo` describes the error.

Data types: strings are `nvarchar(4000)`; integers, booleans (1 or 0) and object tokens are `int`; dates are `datetime`. In the signatures on this page, `@success`, `@iResult`, `@sResult` and the like are the OUT variables receiving a method's return value, `@iValue` / `@sValue` receive or supply a property value, and the remaining `@` variables are the method's arguments in order.

A string returned through an OUT parameter is limited to 4000 characters. For longer values, retrieve the result as a result set into a table variable instead of an OUT parameter, for example `DECLARE @tmp TABLE (outputLine ntext)` followed by `INSERT INTO @tmp EXEC sp_OAGetProperty @messageSet, 'LastErrorText'`. See [string length limitations for strings returned by sp_OAMethod calls](https://www.chilkatsoft.com/sql_server_nvarchar_max_limitation.asp).

Methods that pass or return raw byte arrays are not shown on this page, because `varbinary(max)` values cannot be exchanged through `sp_OAMethod` (see [varbinary(max) limitation](https://www.chilkatsoft.com/sql_server_varbinary_max_limitation.asp)). Use the `BinData`-based alternatives (methods ending in *Bd*) or the base64 / hex string-encoded variants instead. Binary *properties* (such as `LastBinaryResult`) can be retrieved as a result set into a table variable, as shown in their signatures. Asynchronous (*\*Async*) methods and event callbacks are not available from SQL Server.

Call `Global.UnlockBundle` once at the start of the program, before using this or any other Chilkat class. Any string unlocks a fully functional 30-day trial; a purchased unlock code is used in production. See `guide.md` for the exact code in SQL Server.

How failure is reported depends on the method's return type and is stated in the note under each method below. In every case the diagnostic text is in the `LastErrorText` property of the object whose method was called.

## Members common to every Chilkat class

Documented once per language in `common-members.md`; listed here so this page is self-contained.

| Property | Signature | Access | Meaning |
|---|---|---|---|
| `DebugLogFilePath` | `EXEC sp_OAGetProperty @messageSet, 'DebugLogFilePath', @sValue OUT EXEC sp_OASetProperty @messageSet, 'DebugLogFilePath', @sValue` | read/write | If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. |
| `LastBinaryResult` | `INSERT INTO @tmp EXEC sp_OAGetProperty @messageSet, 'LastBinaryResult'` | read-only | This property is mainly used in SQL Server stored procedures to retrieve binary data from the last method call that returned binary data. |
| `LastErrorHtml` | `EXEC sp_OAGetProperty @messageSet, 'LastErrorHtml', @sValue OUT` | read-only | Provides HTML-formatted information about the last called method or property. |
| `LastErrorText` | `EXEC sp_OAGetProperty @messageSet, 'LastErrorText', @sValue OUT` | read-only | Provides plain text information about the last called method or property. |
| `LastErrorXml` | `EXEC sp_OAGetProperty @messageSet, 'LastErrorXml', @sValue OUT` | read-only | Provides XML-formatted information about the last called method or property. |
| `LastMethodSuccess` | `EXEC sp_OAGetProperty @messageSet, 'LastMethodSuccess', @iValue OUT EXEC sp_OASetProperty @messageSet, 'LastMethodSuccess', @iValue` | read/write | Indicates the success or failure of the most recent method call: `1` means success, `0` means failure. |
| `LastStringResult` | `EXEC sp_OAGetProperty @messageSet, 'LastStringResult', @sValue OUT` | read-only | In SQL Server stored procedures, this property holds the string return value of the most recent method call that returns a string. |
| `LastStringResultLen` | `EXEC sp_OAGetProperty @messageSet, 'LastStringResultLen', @iValue OUT` | read-only | The length, in characters, of the string contained in the LastStringResult property. |
| `VerboseLogging` | `EXEC sp_OAGetProperty @messageSet, 'VerboseLogging', @iValue OUT EXEC sp_OASetProperty @messageSet, 'VerboseLogging', @iValue` | read/write | If set to `1`, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. |
| `Version` | `EXEC sp_OAGetProperty @messageSet, 'Version', @sValue OUT` | read-only | Version of the component/library, such as "10.1.0" |

## Member index

| Member | Kind | Signature |
|---|---|---|
| `Count` | property, read-only | `EXEC sp_OAGetProperty @messageSet, 'Count', @iValue OUT` |
| `HasUids` | property, read/write | `EXEC sp_OAGetProperty @messageSet, 'HasUids', @iValue OUT EXEC sp_OASetProperty @messageSet, 'HasUids', @iValue` |
| `ContainsId` | method | `EXEC sp_OAMethod @messageSet, 'ContainsId', @success OUT, @msgId` |
| `FromCompactString` | method | `EXEC sp_OAMethod @messageSet, 'FromCompactString', @success OUT, @str` |
| `GetId` | method | `EXEC sp_OAMethod @messageSet, 'GetId', @iResult OUT, @index` |
| `InsertId` | method | `EXEC sp_OAMethod @messageSet, 'InsertId', NULL, @id` |
| `RemoveId` | method | `EXEC sp_OAMethod @messageSet, 'RemoveId', NULL, @id` |
| `Subtract` | method | `EXEC sp_OAMethod @messageSet, 'Subtract', NULL, @msgSet` |
| `ToCommaSeparatedStr` | method | `EXEC sp_OAMethod @messageSet, 'ToCommaSeparatedStr', @sResult OUT` |
| `ToCompactString` | method | `EXEC sp_OAMethod @messageSet, 'ToCompactString', @sResult OUT` |

## Properties

### MessageSet.Count

`Chilkat 11.7.0 · sqlserver · MessageSet · Count · property · read-only`

```sql
EXEC sp_OAGetProperty @messageSet, 'Count', @iValue OUT
```

Returns the number of unique message identifiers currently stored in the set. A newly created `MessageSet` has a count of `0`.

The identifiers are maintained in ascending numeric order. They are interpreted as UIDs when `HasUids` is `1`, or as sequence numbers when it is `0`.

### MessageSet.HasUids

`Chilkat 11.7.0 · sqlserver · MessageSet · HasUids · property · read/write`

```sql
EXEC sp_OAGetProperty @messageSet, 'HasUids', @iValue OUT
EXEC sp_OASetProperty @messageSet, 'HasUids', @iValue
```

**Full documentation for this property (same in every language):** [IMAP Sequence Numbers Explained](https://www.chilkatsoft.com/ai-docs/v/11.7.0/articles/imap_message_sequence_numbers.md)

Indicates how the numeric identifiers in this message set are interpreted:

- `1` — the values are IMAP UIDs. This is the default.
- `0` — the values are mailbox-relative sequence numbers.

Changing this property only changes how the existing numeric values are interpreted. It does not convert, validate, reorder, or clear the identifiers.

An IMAP UID is stable only within its mailbox and while that mailbox's `UIDVALIDITY` remains unchanged. Sequence numbers are temporary and can change when messages are added or expunged.

Set this property consistently before passing a manually constructed message set to an `Imap` method. The string formats produced by `ToCommaSeparatedStr` and `ToCompactString` contain only the numeric values and do not include this setting.

## Methods

### MessageSet.ContainsId

`Chilkat 11.7.0 · sqlserver · MessageSet · ContainsId · method`

```sql
EXEC sp_OAMethod @messageSet, 'ContainsId', @success OUT, @msgId
```

Returns `1` if the raw numeric value in `msgId` is present in the message set; otherwise, returns `0`.

The comparison is numeric only. The `HasUids` setting does not participate in the comparison.

### MessageSet.FromCompactString

`Chilkat 11.7.0 · sqlserver · MessageSet · FromCompactString · method`

```sql
EXEC sp_OAMethod @messageSet, 'FromCompactString', @success OUT, @str
```

Clears the identifiers currently stored in this object and loads the compact message-set representation in `str`. The existing `HasUids` setting is preserved.

**Important:** The existing set is cleared before `str` is parsed. If parsing fails, the message set remains empty.

An empty string succeeds and leaves the set empty. Valid identifiers are unsigned 32-bit decimal values from `1` through `4294967295`. The value `0` is not accepted. Values too large for an unsigned 32-bit integer cause the method to fail.

The syntax uses commas to separate values or ranges, and a colon to specify an inclusive ascending range. A single range may contain at most 500,001 identifiers; equivalently, the ending value minus the starting value cannot exceed `500000`.

**Version note:** The error-handling behavior described here was added in Chilkat version 11.6.0.

| Input | Result |
| :--- | :--- |
| `1,2,3,4,5` | Loads `1,2,3,4,5`. |
| `1:5` | Loads the inclusive range `1` through `5`. |
| `1:5,8:10` | Loads `1,2,3,4,5,8,9,10`. |
| `1,1,2,2,3` | Loads the unique values `1,2,3`. |
| `1:5,3:7` | Loads the unique values `1` through `7`. |
| `1,,3` | Empty comma-separated elements are ignored; loads `1,3`. |
| `1,2,abc,4` | Unrecognized elements are ignored; loads `1,2,4`. |
| `+1` | A leading plus sign is accepted; loads `1`. |

Whitespace is accepted around values, commas, and colons. This includes spaces, tabs, carriage returns, and line feeds. Empty comma-separated elements and unrecognized non-numeric elements are intentionally ignored. If `str` is nonempty and no valid identifier is loaded, the method returns `0`.

Descending ranges, ranges containing `0`, wildcard ranges such as `1:*`, malformed range syntax, oversized ranges, and numeric overflow cause the method to return `0`.

On success, duplicate identifiers are removed and the resulting identifiers are stored in ascending numeric order. The serialized form contains only the numbers and does not indicate whether they are UIDs or sequence numbers.

Use `ToCompactString` to generate the canonical compact representation.

Returns 1 for success, 0 for failure.

### MessageSet.GetId

`Chilkat 11.7.0 · sqlserver · MessageSet · GetId · method`

```sql
EXEC sp_OAMethod @messageSet, 'GetId', @iResult OUT, @index
```

Returns the message identifier at the zero-based index specified by `index`. Identifiers are returned in ascending numeric order.

If `index` is negative or is greater than or equal to `Count`, this method returns `0xFFFFFFFF`.

**Important:** The numeric value `0xFFFFFFFF` can also be stored in the set. Check `index` against `Count` before calling this method when that value could be present.

### MessageSet.InsertId

`Chilkat 11.7.0 · sqlserver · MessageSet · InsertId · method`

```sql
EXEC sp_OAMethod @messageSet, 'InsertId', NULL, @id
```

Adds `id` to the message set and maintains the identifiers in ascending numeric order.

- If `id` is already present, no duplicate is added.
- If `id` is `0`, the set is unchanged.
- The value `0xFFFFFFFF` is accepted.

The `HasUids` setting determines whether the stored values are interpreted as UIDs or sequence numbers.

### MessageSet.RemoveId

`Chilkat 11.7.0 · sqlserver · MessageSet · RemoveId · method`

```sql
EXEC sp_OAMethod @messageSet, 'RemoveId', NULL, @id
```

Removes the identifier whose raw numeric value equals `id`. If the value is not present, the set is unchanged.

The comparison is numeric only and does not depend on `HasUids`. The remaining identifiers stay in ascending numeric order.

### MessageSet.Subtract

`Chilkat 11.7.0 · sqlserver · MessageSet · Subtract · method · since 11.3.0`

```sql
EXEC sp_OAMethod @messageSet, 'Subtract', NULL, @msgSet
```

Removes from this message set every identifier whose raw numeric value is present in `msgSet`. Values not present in this set are ignored.

The comparison is numeric only; the `HasUids` values of the two objects are not compared. This object's `HasUids` value is preserved, and the remaining identifiers stay in ascending numeric order.

Passing the same object as `msgSet` clears the set. Passing an empty set leaves this set unchanged.

For meaningful IMAP results, both objects should represent identifiers of the same type and from the same mailbox.

### MessageSet.ToCommaSeparatedStr

`Chilkat 11.7.0 · sqlserver · MessageSet · ToCommaSeparatedStr · method`

```sql
EXEC sp_OAMethod @messageSet, 'ToCommaSeparatedStr', @sResult OUT
```

Returns the unique identifiers as a comma-separated string in ascending numeric order. An empty set returns an empty string.

For example, a set containing the identifiers `1` through `5` returns `1,2,3,4,5`.

The returned string contains only the numeric values; it does not include the `HasUids` setting.

Returns `NULL` on failure

### MessageSet.ToCompactString

`Chilkat 11.7.0 · sqlserver · MessageSet · ToCompactString · method`

```sql
EXEC sp_OAMethod @messageSet, 'ToCompactString', @sResult OUT
```

Returns the unique identifiers in ascending numeric order using compact IMAP message-set syntax. Consecutive identifiers are represented as an inclusive range. An empty set returns an empty string.

| Comma-separated form | Compact form |
| :--- | :--- |
| `1,2,3,4,5` | `1:5` |
| `1,2,3,4,5,8,9,10` | `1:5,8:10` |
| `1,3,4,5,8,9,10` | `1,3:5,8:10` |

The returned string is canonical for the current set: identifiers are unique, sorted, and consecutive runs are compacted. It contains only the numeric values and does not include the `HasUids` setting.

Use `FromCompactString` to load this format.

Returns `NULL` on failure

