SQL Server guide

Create or update a JSON file


Creates or updates a JSON file directly in a SharePoint document library using a streaming approach. Reads records from an external source and writes to JSON.

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 FileReaderWriterJson
SOURCE( 'MSSQL'--OR 'ODBC'
, 'Data Source=localhost;Initial Catalog=Northwind;Integrated Security=true'
, 'select OrderId,CustomerId,OrderDate FROM Northwind.dbo.Orders'
)
WITH(
	    DriveId='b!0zqXLXXJh0uUMzl-JXAd9Ztngc-5utVDqRyD2lKpD2535-11HLQTR5z4hOzmA7Q0'
	  , FileId='root:/test_out.json:'
	--, FileId='root:/subfolder/test_out.json:'
	--, FileId='01N3NxxxxxxxWZYSDJ'  --exising File ID
	  , JsonOutputFormat='Multicontent' --Default , Array2D, ArrayLines ...
	--, DoNotOutputNullProperty='True'
	--, Encoding='UTF8' --UTF8WithoutBOM
  --, WriterDateTimeFormat='yyyy-MM-ddTHH:mm:ss.fff'
	
	--Example#1: Output all columns
	, LayoutMap='<?xml version="1.0"  ?>
<settings>
  <dataset id="root" main="True" readfrominput="True" />
  <map src="*" />  
</settings>'

	--Example#2: Nested JSON (Records under an array)
	/*
	, LayoutMap='<?xml version="1.0" ?>
<settings singledataset="True">
  <dataset id="root" main="True" readfrominput="True" />
  <map name="MyArray" dataset="root" maptype="DocArray">
    <map src="OrderID" name="OrderID" />
    <map src="OrderDate" name="OrderDate" />
  </map>
</settings>'
   */
   
	--Example#3: Nested JSON (Records under a sub document section)
	/*	
	, LayoutMap='<?xml version="1.0" ?>
<settings>
  <dataset id="dsRoot" main="True" readfrominput="True" />
  <map name="NestedSection">
    <map src="OrderID" name="OrderID_MyLabel" />
    <map src="OrderDate" name="OrderDate_MyLabel" />
  </map>
</settings>'
	*/
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY], 'INSERT INTO FileReaderWriterJson
SOURCE( ''MSSQL''--OR ''ODBC''
, ''Data Source=localhost;Initial Catalog=Northwind;Integrated Security=true''
, ''select OrderId,CustomerId,OrderDate FROM Northwind.dbo.Orders''
)
WITH(
	    DriveId=''b!0zqXLXXJh0uUMzl-JXAd9Ztngc-5utVDqRyD2lKpD2535-11HLQTR5z4hOzmA7Q0''
	  , FileId=''root:/test_out.json:''
	--, FileId=''root:/subfolder/test_out.json:''
	--, FileId=''01N3NxxxxxxxWZYSDJ''  --exising File ID
	  , JsonOutputFormat=''Multicontent'' --Default , Array2D, ArrayLines ...
	--, DoNotOutputNullProperty=''True''
	--, Encoding=''UTF8'' --UTF8WithoutBOM
  --, WriterDateTimeFormat=''yyyy-MM-ddTHH:mm:ss.fff''
	
	--Example#1: Output all columns
	, LayoutMap=''<?xml version="1.0"  ?>
<settings>
  <dataset id="root" main="True" readfrominput="True" />
  <map src="*" />  
</settings>''

	--Example#2: Nested JSON (Records under an array)
	/*
	, LayoutMap=''<?xml version="1.0" ?>
<settings singledataset="True">
  <dataset id="root" main="True" readfrominput="True" />
  <map name="MyArray" dataset="root" maptype="DocArray">
    <map src="OrderID" name="OrderID" />
    <map src="OrderDate" name="OrderDate" />
  </map>
</settings>''
   */
   
	--Example#3: Nested JSON (Records under a sub document section)
	/*	
	, LayoutMap=''<?xml version="1.0" ?>
<settings>
  <dataset id="dsRoot" main="True" readfrominput="True" />
  <map name="NestedSection">
    <map src="OrderID" name="OrderID_MyLabel" />
    <map src="OrderDate" name="OrderDate_MyLabel" />
  </map>
</settings>''
	*/
)')

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) = 'INSERT INTO FileReaderWriterJson
SOURCE( ''MSSQL''--OR ''ODBC''
, ''Data Source=localhost;Initial Catalog=Northwind;Integrated Security=true''
, ''select OrderId,CustomerId,OrderDate FROM Northwind.dbo.Orders''
)
WITH(
	    DriveId=''b!0zqXLXXJh0uUMzl-JXAd9Ztngc-5utVDqRyD2lKpD2535-11HLQTR5z4hOzmA7Q0''
	  , FileId=''root:/test_out.json:''
	--, FileId=''root:/subfolder/test_out.json:''
	--, FileId=''01N3NxxxxxxxWZYSDJ''  --exising File ID
	  , JsonOutputFormat=''Multicontent'' --Default , Array2D, ArrayLines ...
	--, DoNotOutputNullProperty=''True''
	--, Encoding=''UTF8'' --UTF8WithoutBOM
  --, WriterDateTimeFormat=''yyyy-MM-ddTHH:mm:ss.fff''
	
	--Example#1: Output all columns
	, LayoutMap=''<?xml version="1.0"  ?>
<settings>
  <dataset id="root" main="True" readfrominput="True" />
  <map src="*" />  
</settings>''

	--Example#2: Nested JSON (Records under an array)
	/*
	, LayoutMap=''<?xml version="1.0" ?>
<settings singledataset="True">
  <dataset id="root" main="True" readfrominput="True" />
  <map name="MyArray" dataset="root" maptype="DocArray">
    <map src="OrderID" name="OrderID" />
    <map src="OrderDate" name="OrderDate" />
  </map>
</settings>''
   */
   
	--Example#3: Nested JSON (Records under a sub document section)
	/*	
	, LayoutMap=''<?xml version="1.0" ?>
<settings>
  <dataset id="dsRoot" main="True" readfrominput="True" />
  <map name="NestedSection">
    <map src="OrderID" name="OrderID_MyLabel" />
    <map src="OrderDate" name="OrderDate_MyLabel" />
  </map>
</settings>''
	*/
)'
EXEC (@MyQuery) AT [LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY]