title: stored procedure confusing named parameters 'Error converting data type nvarchar to int.'
body: <p>Running the exact same sql command, I get an error depending on what order the parameters are defined in the stored proc. This error was originally encountered using a stored proc mapped through entity framework, but that does not seem to be the cause of the issue. </p>
  
  <p>The error message 'Error converting data type nvarchar to int.' makes it seem like the sproc is trying to jam the @CagIdList parameter into one of the nullable int parameters. Thoughts?</p>
  
  <p>Sql command:</p>
  
  <pre><code>exec sp_executesql 
      N'rptAll.usp_SprocParameterTest @StartDate, @EndDate, @CAGIdList',
      N'@StartDate datetime,@EndDate datetime,@CAGIdList nvarchar(1317)',
      @StartDate='2014-11-16 00:00:00',@EndDate='2014-12-16 00:00:00',@CAGIdList=N'857,858,859'
  </code></pre>
  
  <p>The above command will fail with this stored proc:</p>
  
  <pre><code>SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  
  ALTER procedure [rptAll].[usp_SprocParameterTest]
  (
          @StartDate datetime,
          @EndDate datetime,
          @StartRow int = null, -- please note where this parameter started
          @MaxRows int = null, -- me too
          @Sort varchar(255)= null,
          @mfgCode varchar(255) = null,
          @CAGIdList varchar(max) = null
  )
  as
  
  select 1
  </code></pre>
  
  <p>The same will succeed for this stored proc :</p>
  
  <pre><code>--Move the nullable int params to the end of the list
  SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  
  ALTER procedure [rptAll].[usp_SprocParameterTest]
  (
          @StartDate datetime,
          @EndDate datetime,
          @Sort varchar(255)= null,
          @mfgCode varchar(255) = null,
          @CAGIdList varchar(5000) = null,
          @StartRow int = null, --look at mee
          @MaxRows int = null --hey me too
  )
  as
  
  select 1
  </code></pre>