SQL Server guide

Update list member


Updates an existing list member. Use UPDATE ListMembers SET ... WHERE Id='...' WITH (ListId='...'). You can change EmailAddress, Status, EmailType, merge fields, Tags, and other profile fields.

Standard SQL query example

This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.

UPDATE ListMembers 
SET EmailAddress='brucewayne10@mycompany.com'
, Status='subscribed' --subscribed, unsubscribed, cleaned, pending
, EmailType='text' --html, text
, Vip='false' --true
, FirstName='Bruce'
, LastName='Wayne'
/*,MergeFields= '{
        "FNAME": "Bruce1",
        "LNAME": "Wayne1",
        "PHONE": "678-111-1234"
    }',
*/
, Language='en' --fr
, TimestampOpt='2023-04-02 11:37:49' 
, Latitude='38.8951' , Longitude='-77.0364' --Washington DC
, Tags='["tag1","tag2"]'
WHERE Id='e9f73ced3b649f0ca829103bcacb2846' 
WITH (ListId='a4d24015f8',SkipMergeValidation='false')

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_MAILCHIMP_IN_GATEWAY], 'UPDATE ListMembers 
SET EmailAddress=''brucewayne10@mycompany.com''
, Status=''subscribed'' --subscribed, unsubscribed, cleaned, pending
, EmailType=''text'' --html, text
, Vip=''false'' --true
, FirstName=''Bruce''
, LastName=''Wayne''
/*,MergeFields= ''{
        "FNAME": "Bruce1",
        "LNAME": "Wayne1",
        "PHONE": "678-111-1234"
    }'',
*/
, Language=''en'' --fr
, TimestampOpt=''2023-04-02 11:37:49'' 
, Latitude=''38.8951'' , Longitude=''-77.0364'' --Washington DC
, Tags=''["tag1","tag2"]''
WHERE Id=''e9f73ced3b649f0ca829103bcacb2846'' 
WITH (ListId=''a4d24015f8'',SkipMergeValidation=''false'')')

Using EXEC in SQL Server (handling larger SQL text)

The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements. This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).

Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_MAILCHIMP_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'UPDATE ListMembers 
SET EmailAddress=''brucewayne10@mycompany.com''
, Status=''subscribed'' --subscribed, unsubscribed, cleaned, pending
, EmailType=''text'' --html, text
, Vip=''false'' --true
, FirstName=''Bruce''
, LastName=''Wayne''
/*,MergeFields= ''{
        "FNAME": "Bruce1",
        "LNAME": "Wayne1",
        "PHONE": "678-111-1234"
    }'',
*/
, Language=''en'' --fr
, TimestampOpt=''2023-04-02 11:37:49'' 
, Latitude=''38.8951'' , Longitude=''-77.0364'' --Washington DC
, Tags=''["tag1","tag2"]''
WHERE Id=''e9f73ced3b649f0ca829103bcacb2846'' 
WITH (ListId=''a4d24015f8'',SkipMergeValidation=''false'')'
EXEC (@MyQuery) AT [LS_TO_MAILCHIMP_IN_GATEWAY]