title: Exec stored procedure with OPENQUERY with parameters
body: <p>I have the following procedure:</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[meals] @EncounterID nvarchar(15), @EventDate nvarchar(30)
  AS
     DECLARE @TSQL varchar(8000);
  
     SELECT  @TSQL = 'SELECT * FROM OPENQUERY(cerner, ''select ce.result_val  PercentEaten,
  ce.event_cd Meal
   from clinical_event ce
   where
   ce.task_assay_cd in ( 5362408, 5362296, 5361870)
   and ce.event_end_dt_tm &gt;= sysdate-30
   and ce.event_cd in (636033, 636036, 636039)
   and ce.event_class_cd = 233
  and ce.encntr_id = ''' +CAST(@EncounterID as varchar(15))+ '''
  and ce.event_end_dt_tm between to_char('''+CAST(@EventDate as varchar(30))+''' - .5) and 
      to_char('''+CAST(@EventDate as varchar(30))+''' - 1)
  '')';
  EXEC (@TSQL);
  GO
  </code></pre>
  
  <p>When I execute with this line:</p>
  
  <pre><code>exec dbo.meals @EncounterID =  '12345678',@EventDate = '07/18/2013'
  </code></pre>
  
  <p>I get an error that just says <code>error at line 9</code></p>
  
  <p>What am I doing wrong??</p>