Sample code for 30+ languages & platforms
SQL Server

Define a Mail-Merge Replacement Pattern

See more Email Object Examples

Demonstrates the Chilkat Email.SetReplacePattern method, which defines a mail-merge replacement pair. The first argument is the pattern to find and the second is the replacement text. When the email is sent, the patterns are replaced in the bodies and header fields. This example defines two pairs and prints the count.

Background: This is Chilkat's built-in mail merge. You compose one message with placeholder tokens (like FIRST_NAME or CITY), register each token and its value with SetReplacePattern, and the substitutions are applied at send time — personalizing a bulk mailing without building a separate message for every recipient. Read the pairs back with GetReplacePattern and GetReplaceString.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    --  Demonstrates the SetReplacePattern method, which defines a mail-merge replacement pair.
    --  The first argument is the pattern to find and the second is the replacement text.  When the email is sent,
    --  the patterns are replaced in the bodies and header fields.

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

    EXEC sp_OASetProperty @email, 'Subject', 'Hello FIRST_NAME'
    EXEC sp_OASetProperty @email, 'Body', 'Dear FIRST_NAME, welcome to CITY.'

    --  Define two mail-merge replacement pairs.
    DECLARE @success int
    EXEC sp_OAMethod @email, 'SetReplacePattern', @success OUT, 'FIRST_NAME', 'John'
    EXEC sp_OAMethod @email, 'SetReplacePattern', @success OUT, 'CITY', 'Denver'


    EXEC sp_OAGetProperty @email, 'NumReplacePatterns', @iTmp0 OUT
    PRINT 'NumReplacePatterns = ' + @iTmp0

    EXEC @hr = sp_OADestroy @email


END
GO