SQL Server guide

Export file to another format (e.g., Excel or PDF)


Exports a Google file (e.g. Google Sheets, Docs) to another format and saves it to a local path. Supply the file ID, the target MIME type (ExportAs), and the destination path (ResponseDataFile). For shared drives, add DriveId and DriveType='drive'.

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 export_file
WITH(
	Id='1j0HFOP4gsoE-Zbf2xN3IsPSj8wgwNtIfyyvM1_BWkW4',
	ExportAs='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
	ResponseDataFile='c:\temp\sheet1.xlsx',
	FileOverwriteMode='AlwaysOverwrite'
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_GOOGLE_DRIVE_IN_GATEWAY], 'SELECT "Status" FROM export_file
WITH(
	Id=''1j0HFOP4gsoE-Zbf2xN3IsPSj8wgwNtIfyyvM1_BWkW4'',
	ExportAs=''application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'',
	ResponseDataFile=''c:\temp\sheet1.xlsx'',
	FileOverwriteMode=''AlwaysOverwrite''
)')

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

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT "Status" FROM export_file
WITH(
	Id=''1j0HFOP4gsoE-Zbf2xN3IsPSj8wgwNtIfyyvM1_BWkW4'',
	ExportAs=''application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'',
	ResponseDataFile=''c:\temp\sheet1.xlsx'',
	FileOverwriteMode=''AlwaysOverwrite''
)'
EXEC (@MyQuery) AT [LS_TO_GOOGLE_DRIVE_IN_GATEWAY]