SQL Server guide

Upsert a user


Creates a new user or updates an existing one. This example demonstrates using the UPSERT INTO statement, which updates the user if they exist (identified by email or external ID) or creates them otherwise.

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.

INSERT INTO Users
(
 name
,email
,organization_id
,phone
,default_group_id
,locale
,moderator
,skip_verify_email
,only_private_comments
,signature
,tags
,time_zone
,role
,external_id
,alias
,details
,notes
,remote_photo_url
,user_fields
)
VALUES(
   'Bob Walton'
  ,'bob@abc.com'
  ,5397098432795
  ,'111-222-3333'
  ,114094762733
  ,'en-US'
  ,'false'
  ,'true' --true=do not send verify account email
  ,1 --user can put only private comments
  ,'Best regards, Support Team' --Only agents and admins can have signatures
  ,'["paid","trial","solved"]' 
  ,'America/New_York'
  ,'end-user' --agent or admin 
  ,'zcrm_1558554000052161270'
  ,'some alias'
  ,'some details'
  ,'some notes'
  ,'https://zappysys.com/wp-content/uploads/2021/10/Slider-API-Hub-1.png' --this does not work in INSERT (works only in UPDATE/UPSERT for now)
  ,'{"birthdate": "1981-01-23", "gender": "M"}'
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_ZENDESK_IN_GATEWAY], 'INSERT INTO Users
(
 name
,email
,organization_id
,phone
,default_group_id
,locale
,moderator
,skip_verify_email
,only_private_comments
,signature
,tags
,time_zone
,role
,external_id
,alias
,details
,notes
,remote_photo_url
,user_fields
)
VALUES(
   ''Bob Walton''
  ,''bob@abc.com''
  ,5397098432795
  ,''111-222-3333''
  ,114094762733
  ,''en-US''
  ,''false''
  ,''true'' --true=do not send verify account email
  ,1 --user can put only private comments
  ,''Best regards, Support Team'' --Only agents and admins can have signatures
  ,''["paid","trial","solved"]'' 
  ,''America/New_York''
  ,''end-user'' --agent or admin 
  ,''zcrm_1558554000052161270''
  ,''some alias''
  ,''some details''
  ,''some notes''
  ,''https://zappysys.com/wp-content/uploads/2021/10/Slider-API-Hub-1.png'' --this does not work in INSERT (works only in UPDATE/UPSERT for now)
  ,''{"birthdate": "1981-01-23", "gender": "M"}''
)')

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_ZENDESK_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO Users
(
 name
,email
,organization_id
,phone
,default_group_id
,locale
,moderator
,skip_verify_email
,only_private_comments
,signature
,tags
,time_zone
,role
,external_id
,alias
,details
,notes
,remote_photo_url
,user_fields
)
VALUES(
   ''Bob Walton''
  ,''bob@abc.com''
  ,5397098432795
  ,''111-222-3333''
  ,114094762733
  ,''en-US''
  ,''false''
  ,''true'' --true=do not send verify account email
  ,1 --user can put only private comments
  ,''Best regards, Support Team'' --Only agents and admins can have signatures
  ,''["paid","trial","solved"]'' 
  ,''America/New_York''
  ,''end-user'' --agent or admin 
  ,''zcrm_1558554000052161270''
  ,''some alias''
  ,''some details''
  ,''some notes''
  ,''https://zappysys.com/wp-content/uploads/2021/10/Slider-API-Hub-1.png'' --this does not work in INSERT (works only in UPDATE/UPSERT for now)
  ,''{"birthdate": "1981-01-23", "gender": "M"}''
)'
EXEC (@MyQuery) AT [LS_TO_ZENDESK_IN_GATEWAY]