SQL Server guide

Create or update a CSV file from external source


Creates or updates a CSV file using a streaming approach. Reads records from an external source (e.g. Microsoft SQL Server) and writes to a CSV file. If the file does not exist, a new one is created.

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 FileReaderWriterCsv
SOURCE( 'MSSQL'--OR 'ODBC'
, 'Data Source=localhost;Initial Catalog=Northwind;Integrated Security=true'
, 'select OrderId,CustomerId,OrderDate FROM Northwind.dbo.Orders'
)
WITH(
	  DriveId='b!XpzQciaV_k6my5II5L22J0C4iRhyz21Js89PUyZ6-w0lH0AYv_I8RJHpXZQ81efD'
	--Path must end with colon
	  , FileId='root:/test_out.csv:'	--Path must end with colon
	--, FileId='root:/subfolder/test_out.csv:'
	--, FileId='01N3NxxxxxxxWZYSDJ'  --exising File ID
	--, ContinueOn404Error=0 --Fail if file not found (Useful for overwrite mode for exising file)
	--, FileId='01N3NI7YU6DYBSLCEDKBB23CR4FSWZYSDJ'  --exising File ID for overwrite action
	--, ColumnDelimiter=',' --{LF}, {TAB}, | , \x0009 ...
	--, RowDelimiter='{NEWLINE}' --{LF}, {TAB}, | , \x0009 ...
	--, HasColumnHeaderRow=0 --set for header less file
    --, WriterDateTimeFormat='yyyy-MM-ddTHH:mm:ss.fff'  
    --See Query Builder for more options
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_ONEDRIVE_IN_GATEWAY], 'INSERT INTO FileReaderWriterCsv
SOURCE( ''MSSQL''--OR ''ODBC''
, ''Data Source=localhost;Initial Catalog=Northwind;Integrated Security=true''
, ''select OrderId,CustomerId,OrderDate FROM Northwind.dbo.Orders''
)
WITH(
	  DriveId=''b!XpzQciaV_k6my5II5L22J0C4iRhyz21Js89PUyZ6-w0lH0AYv_I8RJHpXZQ81efD''
	--Path must end with colon
	  , FileId=''root:/test_out.csv:''	--Path must end with colon
	--, FileId=''root:/subfolder/test_out.csv:''
	--, FileId=''01N3NxxxxxxxWZYSDJ''  --exising File ID
	--, ContinueOn404Error=0 --Fail if file not found (Useful for overwrite mode for exising file)
	--, FileId=''01N3NI7YU6DYBSLCEDKBB23CR4FSWZYSDJ''  --exising File ID for overwrite action
	--, ColumnDelimiter='','' --{LF}, {TAB}, | , \x0009 ...
	--, RowDelimiter=''{NEWLINE}'' --{LF}, {TAB}, | , \x0009 ...
	--, HasColumnHeaderRow=0 --set for header less file
    --, WriterDateTimeFormat=''yyyy-MM-ddTHH:mm:ss.fff''  
    --See Query Builder for more options
)')

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

DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO FileReaderWriterCsv
SOURCE( ''MSSQL''--OR ''ODBC''
, ''Data Source=localhost;Initial Catalog=Northwind;Integrated Security=true''
, ''select OrderId,CustomerId,OrderDate FROM Northwind.dbo.Orders''
)
WITH(
	  DriveId=''b!XpzQciaV_k6my5II5L22J0C4iRhyz21Js89PUyZ6-w0lH0AYv_I8RJHpXZQ81efD''
	--Path must end with colon
	  , FileId=''root:/test_out.csv:''	--Path must end with colon
	--, FileId=''root:/subfolder/test_out.csv:''
	--, FileId=''01N3NxxxxxxxWZYSDJ''  --exising File ID
	--, ContinueOn404Error=0 --Fail if file not found (Useful for overwrite mode for exising file)
	--, FileId=''01N3NI7YU6DYBSLCEDKBB23CR4FSWZYSDJ''  --exising File ID for overwrite action
	--, ColumnDelimiter='','' --{LF}, {TAB}, | , \x0009 ...
	--, RowDelimiter=''{NEWLINE}'' --{LF}, {TAB}, | , \x0009 ...
	--, HasColumnHeaderRow=0 --set for header less file
    --, WriterDateTimeFormat=''yyyy-MM-ddTHH:mm:ss.fff''  
    --See Query Builder for more options
)'
EXEC (@MyQuery) AT [LS_TO_ONEDRIVE_IN_GATEWAY]