title: Microsoft R Server Row by Row Insert
body: <p>I have a for loop that usually writes to a flat file.  This way, if anything breaks, I can start where I left off.  I want to convert this process to read and write from a SQL table directly using the new RevoScaleR functions in a SQL Server 2016 stored procedure that executes my R code.</p>
  
  <p>Here is a simple SPROC:</p>
  
  <pre><code>USE [master]
  GO
  
  /****** Object:  StoredProcedure [dbo].[Rscript_geocodeUSACities_TEST]    Script Date: 8/8/2017 11:40:40 AM ******/
  SET ANSI_NULLS ON
  GO
  
  SET QUOTED_IDENTIFIER ON
  GO
  
  
  
  
  CREATE PROCEDURE [dbo].[Rscript_geocodeUSACities_TEST]
      @usrOutputFilePath varchar(150)
      ,@usrOutputFileName varchar(150)
  
  AS
  BEGIN
  
      SET NOCOUNT ON;
  
  DECLARE @rScript nvarchar(max) = N'
  
  #### USER INPUTS ####
  
  usrOutputFile &lt;- "' + @usrOutputFilePath + @usrOutputFileName + '"
  
  
  #### ESTABLISH ENVIRONMENT ####
  
  library(data.table)
  library(foreach)
  library(XML)
  library(RCurl)
  library(RJSONIO)
  
  ##turn off scientific notation
  options(scipen=999)
  
  ##establish compute context
  sqlServerConnString &lt;- "Server=.;Database=External;Trusted_Connection=true"
  sqlServerCC &lt;- RxInSqlServer(connectionString=sqlServerConnString)
  rxSetComputeContext(sqlServerCC)
  print(rxGetComputeContext())
  
  
  #### GEOCODE ####
  
  print(dfInputData)
  rxDataStep(data=dfInputData,outFile=imp.USA_Cities_Map,append="rows")
  
  '
  
  EXECUTE  sp_execute_external_script
                  @language = N'R'
                , @script = @rScript
                ,@input_data_1 =N'select 5 as test_insert'
              ,@input_data_1_name =N'dfInputData'
                ;
  
  END
  </code></pre>
  
  <p>Error ouput:</p>
  
  <pre><code>Error in rxDataStep(data = dfInputData, outFile = imp.USA_Cities_Map,  : 
    object 'imp.USA_Cities_Map' not found
  </code></pre>