Bulk upsert users/organizations using SQL Server data
Upserts users or organizations in bulk using data from a SQL Server database. This example demonstrates using the UPSERT INTO ... SOURCE syntax to read data from an external source and import it into Zendesk.
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.
UPSERT INTO Organizations
SOURCE('MSSQL' --ODBC
, 'Data Source=localhost;Initial Catalog=test;Integrated Security=true'
--For bulk input, map columns in External Query (Must use alias column name to match INSERT command Input Column names - see help file)
--If parameter value not same for all input rows then you can prefix some column with $$ to map as parameter (i.e. $$MyParam1)
--'ODBC', 'Driver={ZappySys CSV Driver};DataPath=C:\AccountsToInsert.csv'
--'ODBC', 'DSN=MyDSN'
--'OLEDB', 'Provider=SQLNCLI11;Server=localhost,1433;Database=tempdb;Trusted_Connection=yes;'
, 'select
21863188631451 as id
, ''Abc Inc'' as name
,''zcrm_1558554000052161270'' as external_id
,114094762733 as group_id
,''["paid","trial","solved"]'' as tags
,''some details'' as details
,''some notes'' as notes
,''{"startdate": "1981-01-23", "revenue": 12000000.50, "somenumber": 1235678}'' as organization_fields
,''["aaa.com", "bbb.com"]'' domain_names
,''false'' as shared_tickets
,''false'' as shared_comments
')
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_ZENDESK_IN_GATEWAY], 'UPSERT INTO Organizations
SOURCE(''MSSQL'' --ODBC
, ''Data Source=localhost;Initial Catalog=test;Integrated Security=true''
--For bulk input, map columns in External Query (Must use alias column name to match INSERT command Input Column names - see help file)
--If parameter value not same for all input rows then you can prefix some column with $$ to map as parameter (i.e. $$MyParam1)
--''ODBC'', ''Driver={ZappySys CSV Driver};DataPath=C:\AccountsToInsert.csv''
--''ODBC'', ''DSN=MyDSN''
--''OLEDB'', ''Provider=SQLNCLI11;Server=localhost,1433;Database=tempdb;Trusted_Connection=yes;''
, ''select
21863188631451 as id
, ''''Abc Inc'''' as name
,''''zcrm_1558554000052161270'''' as external_id
,114094762733 as group_id
,''''["paid","trial","solved"]'''' as tags
,''''some details'''' as details
,''''some notes'''' as notes
,''''{"startdate": "1981-01-23", "revenue": 12000000.50, "somenumber": 1235678}'''' as organization_fields
,''''["aaa.com", "bbb.com"]'''' domain_names
,''''false'''' as shared_tickets
,''''false'''' as shared_comments
'')')
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) = 'UPSERT INTO Organizations
SOURCE(''MSSQL'' --ODBC
, ''Data Source=localhost;Initial Catalog=test;Integrated Security=true''
--For bulk input, map columns in External Query (Must use alias column name to match INSERT command Input Column names - see help file)
--If parameter value not same for all input rows then you can prefix some column with $$ to map as parameter (i.e. $$MyParam1)
--''ODBC'', ''Driver={ZappySys CSV Driver};DataPath=C:\AccountsToInsert.csv''
--''ODBC'', ''DSN=MyDSN''
--''OLEDB'', ''Provider=SQLNCLI11;Server=localhost,1433;Database=tempdb;Trusted_Connection=yes;''
, ''select
21863188631451 as id
, ''''Abc Inc'''' as name
,''''zcrm_1558554000052161270'''' as external_id
,114094762733 as group_id
,''''["paid","trial","solved"]'''' as tags
,''''some details'''' as details
,''''some notes'''' as notes
,''''{"startdate": "1981-01-23", "revenue": 12000000.50, "somenumber": 1235678}'''' as organization_fields
,''''["aaa.com", "bbb.com"]'''' domain_names
,''''false'''' as shared_tickets
,''''false'''' as shared_comments
'')'
EXEC (@MyQuery) AT [LS_TO_ZENDESK_IN_GATEWAY]