title: Pass string with quotes as input parameter to a SQL Server 2016 stored procedure using R Service
body: <p>I am using SQL Server 2016 with R service. I am trying to create a stored procedure which I want to execute inserting text as parameter value.</p>
  
  <pre><code>e.g. exec test N'Hello World' 
  </code></pre>
  
  <p>Generally it works fine but if input text which contain quotes it shows error. </p>
  
  <p>Here my sample stored procedure:</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[test] 
      (@param1 NVARCHAR(MAX)) 
  AS
  BEGIN
      SET NOCOUNT ON;
  
      DECLARE @sql VARCHAR(MAX)
      SET @sql = 'select = ''' + @param1 + ''';'
  
      PRINT(@sql)
      EXEC sp_execute_external_script 
                 @language = N'R', 
                 @script = N'data&lt;-mynumbers 
                              OutputDataSet&lt;-data.frame(data)',
                 @params = N' @mynumbers nvarchar (max)',
                 @mynumbers = @sql
   END
  </code></pre>
  
  <p>If I execute the procedure inserting text as parameter value which contain single or multiple quotes (e.g. don't , can't, Tom's), it shows an error:</p>
  
  <pre><code>exec test N' Don't do late making Tom's work '
  </code></pre>
  
  <p>Error: </p>
  
  <blockquote>
    <p>Msg 102, Level 15, State 1, Line 1<br>
    Incorrect syntax near 'ot'.  </p>
    
    <p>Msg 105, Level 15, State 1, Line 1<br>
    Unclosed quotation mark after the character string ''.</p>
  </blockquote>