Using INSERT INTO from SQL Server To Change Salesforce Data

Recently a Salesforce ODBC driver customer contacted our Support team to ask how to insert SQL Server BLOB data into Salesforce, where the source for the data was the results of a query. This blog shares that solution.

In our example solution, the source for the INSERT INTO statement was this table:

create table NewProducts ( "Name" nvarchar(30), ProductCode nvarchar(10),
	                    Description nvarchar(max))
insert into NewProducts values ( 'Easysoft ODBC-Salesforce Driver', 'EAS01',
	                         'ODBC Driver for Salesforce.com.
                                  Available for Linux, Unix and Windows.' )
insert into NewProducts values ( 'Easysoft ODBC-SQL Server Driver', 'EAS02',
                                 'ODBC Driver for SQL Server.
                                  Available for Linux, Unix and Windows.' )
insert into NewProducts values ( 'Easysoft ODBC-Oracle Driver', 'EAS03',
                                 'ODBC Driver for Oracle.
                                  Available for Linux, Unix and Windows.' )
select * from NewProducts

To insert the data from this SQL Server table into Salesforce, we ran:

declare @Name nvarchar(30)
declare @ProductCode nvarchar(10)
declare @Description nvarchar(max)

declare ins_cursor cursor for 
select "Name", ProductCode, Description from NewProducts
open ins_cursor
fetch next from ins_cursor into @Name, @ProductCode, @Description
while @@FETCH_STATUS=0
Begin
exec ('insert into Product2 ( "Name", ProductCode, Description ) Values (?, ?, ?)',
       @Name, @ProductCode, @Description ) at MySalesforceLinkedServer
fetch next from ins_cursor into @Name, @ProductCode, @Description
End
close ins_cursor
deallocate ins_cursor

See Also