title: Executing SQL Stored Procedure with Output Parameter from Entity Framework
body: <p>Using EF, I'm trying to execute a stored procedure that returns a single string value, i.e. the status of an SQL Agent Job.</p>
  
  <p>The stored procedure is declared as</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[up_GetJobStatus](@JobStatus NVARCHAR(30) OUTPUT)
  AS
  
  -- some code omitted for brevity
  
  SELECT @JobStatus = (
  SELECT  
      CASE job_state 
      WHEN 1 THEN 'Executing'
      WHEN 2 THEN 'Waiting for thread'
      WHEN 3 THEN 'Between retries'
      WHEN 4 THEN 'Idle'
      WHEN 5 THEN 'Suspended'
      WHEN 6 THEN '&lt;unknown&gt;'
      WHEN 7 THEN 'Performing completion actions'
  END
  FROM @xp_results results 
  INNER JOIN msdb.dbo.sysjobs sj
  ON results.job_id = sj.job_id
  WHERE sj.job_id = @job_id)
  
  RETURN
  </code></pre>
  
  <p>I have verified the stored procedure is working correct as I can execute it in query window and it returns</p>
  
  <pre><code>    @JobStatus
    ------------
    1|Idle
  </code></pre>
  
  <p>However when executing with EF, the param value is NULL</p>
  
  <pre><code>var param = new SqlParameter
  {
      ParameterName = "@JobStatus",
      DbType = DbType.String,
      Size = 30,
      Direction = System.Data.ParameterDirection.Output
  };
  
  var result = this.etlContext.Database.SqlQuery&lt;string&gt;("EXEC dbo.up_GetJobStatus @JobStatus OUTPUT", param);
  </code></pre>
  
  <p>I've also tried the <code>ExecuteSqlCommand</code> method but that didn't work either.</p>
  
  <p>Any ideas?</p>