SQL Server guide

Create a deal with lookup fields


Creates a new Deal record with lookup fields populated. This example demonstrates how to set Account_Name, Contact_Name, and Owner using their respective Names, IDs, or Emails during the INSERT operation.

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 Deals(
	Deal_name,
	Amount,
	Lead_Source,
	Account_Name,
	Contact_Name,
	Owner
	)
VALUES
   (
   'My Test Deal Creatyed on <<FUN_NOW>>', --deal name
   1000.50, --amount
   'Cold Call', --lead source
   '{name:"ZappySys"}', --account name or use id '{id:"1558554000030180013"}'
   '{id:"1558554000089352998"}', --contact id 
   '{id:"1558554000089352912"}' --owner id or use email {email: "bob-the-salesman@abc.com"}
   )

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_ZOHO_CRM_IN_GATEWAY], 'INSERT INTO Deals(
	Deal_name,
	Amount,
	Lead_Source,
	Account_Name,
	Contact_Name,
	Owner
	)
VALUES
   (
   ''My Test Deal Creatyed on <<FUN_NOW>>'', --deal name
   1000.50, --amount
   ''Cold Call'', --lead source
   ''{name:"ZappySys"}'', --account name or use id ''{id:"1558554000030180013"}''
   ''{id:"1558554000089352998"}'', --contact id 
   ''{id:"1558554000089352912"}'' --owner id or use email {email: "bob-the-salesman@abc.com"}
   )')

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

DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO Deals(
	Deal_name,
	Amount,
	Lead_Source,
	Account_Name,
	Contact_Name,
	Owner
	)
VALUES
   (
   ''My Test Deal Creatyed on <<FUN_NOW>>'', --deal name
   1000.50, --amount
   ''Cold Call'', --lead source
   ''{name:"ZappySys"}'', --account name or use id ''{id:"1558554000030180013"}''
   ''{id:"1558554000089352998"}'', --contact id 
   ''{id:"1558554000089352912"}'' --owner id or use email {email: "bob-the-salesman@abc.com"}
   )'
EXEC (@MyQuery) AT [LS_TO_ZOHO_CRM_IN_GATEWAY]