title: Running a sample stored procedure with error Object reference not set to an instance of an object
body: <p>Please help.</p>
  
  <p>I am trying to create a procedure similar to this code: </p>
  
  <pre><code> DECLARE @OldPK AS VARCHAR(50)
   DECLARE @PK AS VARCHAR(50)
   DECLARE @S AS NVARCHAR(MAX)
   DECLARE @P AS NVARCHAR(MAX)
  
   SET @PK = 'ModeName'
   SET @S = N'SELECT @pOldPK = @pPK FROM Modes '
   SET @P = N'@pPK VARCHAR(50), @pOldPK VARCHAR(50) OUTPUT'
   EXECUTE dbo.sp_executesql @S, @P, @pPK = @PK, @pOldPK = @OldPK OUTPUT
   SELECT @OldPK
  </code></pre>
  
  <p>When I try to debug and after the line</p>
  
  <pre><code> EXECUTE dbo.sp_executesql @S, @P, @pPK = @PK, @pOldPK = @OldPK OUTPUT
  </code></pre>
  
  <p>it stops and generates an error </p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object</p>
  </blockquote>
  
  <p>Your valuable inputs is very much appreciated.</p>
  
  <p>Thanks. </p>