Bulk update sheet rows using SQL Server data
Bulk updates rows in a sheet by reading data from an external SQL Server database using the SOURCE clause. The source query must include the Id of the rows to update.
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 [My Sheet]
SOURCE( 'MSSQL'--OR 'ODBC'--OR 'OLEDB'
, 'Data Source=localhost;Initial Catalog=tempdb;Integrated Security=true'
--OR for ODBC
--,'DSN=MyOdbcDsn'
--OR (DSN less connection)
--,'Driver={Some ODBC Driver};uid=xxx;pwd=xxx;prop1=xxx;prop2=xxx....'
, 'SELECT 4081850437570436 as Id,''2012-12-31'' [Some Date Column],''true'' [Some CheckBox Column],''Good example'' [Some Text Column]
UNION
SELECT 8585450064940932 as Id,''2012-10-01'' [Some Date Column],''true'' [Some CheckBox Column],''Good example'' [Some Text Column]')
--WITH(AllowPartialSuccess='true',output=1)
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_SMARTSHEET_IN_GATEWAY], 'UPDATE [My Sheet]
SOURCE( ''MSSQL''--OR ''ODBC''--OR ''OLEDB''
, ''Data Source=localhost;Initial Catalog=tempdb;Integrated Security=true''
--OR for ODBC
--,''DSN=MyOdbcDsn''
--OR (DSN less connection)
--,''Driver={Some ODBC Driver};uid=xxx;pwd=xxx;prop1=xxx;prop2=xxx....''
, ''SELECT 4081850437570436 as Id,''''2012-12-31'''' [Some Date Column],''''true'''' [Some CheckBox Column],''''Good example'''' [Some Text Column]
UNION
SELECT 8585450064940932 as Id,''''2012-10-01'''' [Some Date Column],''''true'''' [Some CheckBox Column],''''Good example'''' [Some Text Column]'')
--WITH(AllowPartialSuccess=''true'',output=1)')
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_SMARTSHEET_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = 'UPDATE [My Sheet]
SOURCE( ''MSSQL''--OR ''ODBC''--OR ''OLEDB''
, ''Data Source=localhost;Initial Catalog=tempdb;Integrated Security=true''
--OR for ODBC
--,''DSN=MyOdbcDsn''
--OR (DSN less connection)
--,''Driver={Some ODBC Driver};uid=xxx;pwd=xxx;prop1=xxx;prop2=xxx....''
, ''SELECT 4081850437570436 as Id,''''2012-12-31'''' [Some Date Column],''''true'''' [Some CheckBox Column],''''Good example'''' [Some Text Column]
UNION
SELECT 8585450064940932 as Id,''''2012-10-01'''' [Some Date Column],''''true'''' [Some CheckBox Column],''''Good example'''' [Some Text Column]'')
--WITH(AllowPartialSuccess=''true'',output=1)'
EXEC (@MyQuery) AT [LS_TO_SMARTSHEET_IN_GATEWAY]