SQL Server guide

Download file and convert to PDF or HTML


Downloads a file and converts it to another format (e.g. PDF or HTML). Supply DriveId, FileId, ConvertTo (e.g. pdf, html), and TargetFilePath. Supported source formats include xlsx, docx, pptx, and others; unsupported conversion returns an error.

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.

SELECT "Status"
FROM download_file
WITH(
     DriveId='me'
     --OR--
     --DriveId='b!7HBaTtrbekqMYJQ-OqV5Q3hrcOIQoyhGiAoWjqWFenIlIJ-Us7DMQ6jvyrsWMJPx'

	, FileId='01R65QTTTF7H7WMCHCKRFJGEJTXAEC7RGX'
	
	--Supported Source Formats: csv, doc, docx, odp, ods, odt, pot, potm, potx, pps, ppsx, ppsxm, ppt, pptm, pptx, rtf, xls, xlsx
	, ConvertTo='pdf'
	, TargetFilePath='C:\temp\converted.pdf'
	
	--OR--
	--Supported Source Formats: loop, fluid, wbtx
	--, ConvertTo='html'
	--, TargetFilePath='C:\temp\converted.html'
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_ONEDRIVE_IN_GATEWAY], 'SELECT "Status"
FROM download_file
WITH(
     DriveId=''me''
     --OR--
     --DriveId=''b!7HBaTtrbekqMYJQ-OqV5Q3hrcOIQoyhGiAoWjqWFenIlIJ-Us7DMQ6jvyrsWMJPx''

	, FileId=''01R65QTTTF7H7WMCHCKRFJGEJTXAEC7RGX''
	
	--Supported Source Formats: csv, doc, docx, odp, ods, odt, pot, potm, potx, pps, ppsx, ppsxm, ppt, pptm, pptx, rtf, xls, xlsx
	, ConvertTo=''pdf''
	, TargetFilePath=''C:\temp\converted.pdf''
	
	--OR--
	--Supported Source Formats: loop, fluid, wbtx
	--, ConvertTo=''html''
	--, TargetFilePath=''C:\temp\converted.html''
)')

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) = 'SELECT "Status"
FROM download_file
WITH(
     DriveId=''me''
     --OR--
     --DriveId=''b!7HBaTtrbekqMYJQ-OqV5Q3hrcOIQoyhGiAoWjqWFenIlIJ-Us7DMQ6jvyrsWMJPx''

	, FileId=''01R65QTTTF7H7WMCHCKRFJGEJTXAEC7RGX''
	
	--Supported Source Formats: csv, doc, docx, odp, ods, odt, pot, potm, potx, pps, ppsx, ppsxm, ppt, pptm, pptx, rtf, xls, xlsx
	, ConvertTo=''pdf''
	, TargetFilePath=''C:\temp\converted.pdf''
	
	--OR--
	--Supported Source Formats: loop, fluid, wbtx
	--, ConvertTo=''html''
	--, TargetFilePath=''C:\temp\converted.html''
)'
EXEC (@MyQuery) AT [LS_TO_ONEDRIVE_IN_GATEWAY]