SQL Server guide

Update / Insert Sharepoint List Data (Mixed Data Types - e.g. Multi Choice, Lookup, Person, DateTime, Number)


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  "Travel Requests" --list name
SET   
	 Title='MyTitle @ <<FUN_NOW>>'  
	 --Method#1 Pass Value and Type in 2 fields
	, CustomText = 'This is text'
	, CustomTextLarge = 'This is really long text ....'	                     
	, CustomNumber = 10000.12345
	, CustomDateTime ='2012-12-31T23:59:59.123Z' --End with Z to treat as UTC time (avoid local-UTC conversion)
	, CustomChoice = 'BB'
	
	--Choice Multi-Select
	, CustomChoiceMulti = '["AA", "BB"]'
	, CustomChoiceMultiODataType='Collection(Edm.String)' --for multi select must supply OData Type 
	--OR-- append like below in one field
	--, CustomChoiceMulti = '["AA", "BB"], "CustomChoiceMulti@odata.type" : "Collection(Edm.String)"'
	
	--Lookup
	--field with Lookup type must be set to [<field-name>LookupId] field (suffix with LookupId)
	, CustomLookupSingleLookupId = 1 
	
	--Lookup Multi-Select
	, CustomLookupMultiLookupId = '[1,3]' --for multi select must supply OData Type 
	, CustomLookupMultiLookupIdODataType='Collection(Edm.Int32)'
	--OR-- append like below in one field
	--, CustomLookupMultiLookupId = '[1,3], "CustomLookupMultiLookupId@odata.type" : "Collection(Edm.String)"'
	
	--Person 
	,CustomPersonLookupId = 11  
	
	--Person Multi-Select
	, CustomPersonMultiLookupId = '[11, 22]' 
	, CustomPersonMultiLookupIdODataType='Collection(Edm.Int32)'
	--OR-- append like below in one field
	--, CustomPersonMultiLookupIdODataType = '[11,22], "CustomPersonMultiLookupIdODataType@odata.type" : "Collection(Edm.Int32)"'
	
Where Id=1 --List Item Id

--- Insert Example ----

INSERT INTO "Travel Requests" --list name
(
	 Title
	, CustomText 
	, CustomTextLarge 
	, CustomNumber 
	, CustomDateTime 
	, CustomChoice 
	
	, CustomChoiceMulti 
	, CustomChoiceMultiODataType

	, CustomLookupSingleLookupId 
	
	--Lookup Multi-Select
	, CustomLookupMultiLookupId 
	, CustomLookupMultiLookupIdODataType
	
	--Person 
	,CustomPersonLookupId 
	
	, CustomPersonMultiLookupId 
	, CustomPersonMultiLookupIdODataType
)
VALUES(
	  'MyTitle @ <<FUN_NOW>>'  
	, 'This is text'
	, 'This is really long text .. '	                     
	, 10000.12345
	, '2012-12-31T23:59:59.123Z' --End with Z to treat as UTC time (avoid local-UTC conversion)
	, 'BB'

	--Choice Multi-Select
	, '["AA", "BB"]'
	, 'Collection(Edm.String)' --for multi select must supply OData Type 
	
	--Lookup
	,  1 
	
	--Lookup Multi-Select
	, '[1,3]' --for multi select must supply OData Type 
	, 'Collection(Edm.Int32)'
	
	--Person 
	,11  
	
	--Person Multi-Select
	, '[11, 22]' 
	, 'Collection(Edm.Int32)'
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY], 'UPDATE  "Travel Requests" --list name
SET   
	 Title=''MyTitle @ <<FUN_NOW>>''  
	 --Method#1 Pass Value and Type in 2 fields
	, CustomText = ''This is text''
	, CustomTextLarge = ''This is really long text ....''	                     
	, CustomNumber = 10000.12345
	, CustomDateTime =''2012-12-31T23:59:59.123Z'' --End with Z to treat as UTC time (avoid local-UTC conversion)
	, CustomChoice = ''BB''
	
	--Choice Multi-Select
	, CustomChoiceMulti = ''["AA", "BB"]''
	, CustomChoiceMultiODataType=''Collection(Edm.String)'' --for multi select must supply OData Type 
	--OR-- append like below in one field
	--, CustomChoiceMulti = ''["AA", "BB"], "CustomChoiceMulti@odata.type" : "Collection(Edm.String)"''
	
	--Lookup
	--field with Lookup type must be set to [<field-name>LookupId] field (suffix with LookupId)
	, CustomLookupSingleLookupId = 1 
	
	--Lookup Multi-Select
	, CustomLookupMultiLookupId = ''[1,3]'' --for multi select must supply OData Type 
	, CustomLookupMultiLookupIdODataType=''Collection(Edm.Int32)''
	--OR-- append like below in one field
	--, CustomLookupMultiLookupId = ''[1,3], "CustomLookupMultiLookupId@odata.type" : "Collection(Edm.String)"''
	
	--Person 
	,CustomPersonLookupId = 11  
	
	--Person Multi-Select
	, CustomPersonMultiLookupId = ''[11, 22]'' 
	, CustomPersonMultiLookupIdODataType=''Collection(Edm.Int32)''
	--OR-- append like below in one field
	--, CustomPersonMultiLookupIdODataType = ''[11,22], "CustomPersonMultiLookupIdODataType@odata.type" : "Collection(Edm.Int32)"''
	
Where Id=1 --List Item Id

--- Insert Example ----

INSERT INTO "Travel Requests" --list name
(
	 Title
	, CustomText 
	, CustomTextLarge 
	, CustomNumber 
	, CustomDateTime 
	, CustomChoice 
	
	, CustomChoiceMulti 
	, CustomChoiceMultiODataType

	, CustomLookupSingleLookupId 
	
	--Lookup Multi-Select
	, CustomLookupMultiLookupId 
	, CustomLookupMultiLookupIdODataType
	
	--Person 
	,CustomPersonLookupId 
	
	, CustomPersonMultiLookupId 
	, CustomPersonMultiLookupIdODataType
)
VALUES(
	  ''MyTitle @ <<FUN_NOW>>''  
	, ''This is text''
	, ''This is really long text .. ''	                     
	, 10000.12345
	, ''2012-12-31T23:59:59.123Z'' --End with Z to treat as UTC time (avoid local-UTC conversion)
	, ''BB''

	--Choice Multi-Select
	, ''["AA", "BB"]''
	, ''Collection(Edm.String)'' --for multi select must supply OData Type 
	
	--Lookup
	,  1 
	
	--Lookup Multi-Select
	, ''[1,3]'' --for multi select must supply OData Type 
	, ''Collection(Edm.Int32)''
	
	--Person 
	,11  
	
	--Person Multi-Select
	, ''[11, 22]'' 
	, ''Collection(Edm.Int32)''
)')

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

DECLARE @MyQuery NVARCHAR(MAX) = 'UPDATE  "Travel Requests" --list name
SET   
	 Title=''MyTitle @ <<FUN_NOW>>''  
	 --Method#1 Pass Value and Type in 2 fields
	, CustomText = ''This is text''
	, CustomTextLarge = ''This is really long text ....''	                     
	, CustomNumber = 10000.12345
	, CustomDateTime =''2012-12-31T23:59:59.123Z'' --End with Z to treat as UTC time (avoid local-UTC conversion)
	, CustomChoice = ''BB''
	
	--Choice Multi-Select
	, CustomChoiceMulti = ''["AA", "BB"]''
	, CustomChoiceMultiODataType=''Collection(Edm.String)'' --for multi select must supply OData Type 
	--OR-- append like below in one field
	--, CustomChoiceMulti = ''["AA", "BB"], "CustomChoiceMulti@odata.type" : "Collection(Edm.String)"''
	
	--Lookup
	--field with Lookup type must be set to [<field-name>LookupId] field (suffix with LookupId)
	, CustomLookupSingleLookupId = 1 
	
	--Lookup Multi-Select
	, CustomLookupMultiLookupId = ''[1,3]'' --for multi select must supply OData Type 
	, CustomLookupMultiLookupIdODataType=''Collection(Edm.Int32)''
	--OR-- append like below in one field
	--, CustomLookupMultiLookupId = ''[1,3], "CustomLookupMultiLookupId@odata.type" : "Collection(Edm.String)"''
	
	--Person 
	,CustomPersonLookupId = 11  
	
	--Person Multi-Select
	, CustomPersonMultiLookupId = ''[11, 22]'' 
	, CustomPersonMultiLookupIdODataType=''Collection(Edm.Int32)''
	--OR-- append like below in one field
	--, CustomPersonMultiLookupIdODataType = ''[11,22], "CustomPersonMultiLookupIdODataType@odata.type" : "Collection(Edm.Int32)"''
	
Where Id=1 --List Item Id

--- Insert Example ----

INSERT INTO "Travel Requests" --list name
(
	 Title
	, CustomText 
	, CustomTextLarge 
	, CustomNumber 
	, CustomDateTime 
	, CustomChoice 
	
	, CustomChoiceMulti 
	, CustomChoiceMultiODataType

	, CustomLookupSingleLookupId 
	
	--Lookup Multi-Select
	, CustomLookupMultiLookupId 
	, CustomLookupMultiLookupIdODataType
	
	--Person 
	,CustomPersonLookupId 
	
	, CustomPersonMultiLookupId 
	, CustomPersonMultiLookupIdODataType
)
VALUES(
	  ''MyTitle @ <<FUN_NOW>>''  
	, ''This is text''
	, ''This is really long text .. ''	                     
	, 10000.12345
	, ''2012-12-31T23:59:59.123Z'' --End with Z to treat as UTC time (avoid local-UTC conversion)
	, ''BB''

	--Choice Multi-Select
	, ''["AA", "BB"]''
	, ''Collection(Edm.String)'' --for multi select must supply OData Type 
	
	--Lookup
	,  1 
	
	--Lookup Multi-Select
	, ''[1,3]'' --for multi select must supply OData Type 
	, ''Collection(Edm.Int32)''
	
	--Person 
	,11  
	
	--Person Multi-Select
	, ''[11, 22]'' 
	, ''Collection(Edm.Int32)''
)'
EXEC (@MyQuery) AT [LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY]