title: Variable is declared but error messages says not
body: <p>In the stored procedure shown below, I get this error, but I am declaring the <code>@Remarks</code>, so I cannot see what is the issue:</p>
  
  <blockquote>
    <p>Msg 137, Level 15, State 2, Procedure sp_Insert_EMR_PATIENT_CHECK_TRACKING, Line 53<br>
    Must declare the scalar variable "@REMARKS".</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>create PROCEDURE [dbo].[sp_Insert_EMR_PATIENT_CHECK_TRACKING] 
       @HOUR int,
       @TIME int,
       @MRN nchar(10),
       @NURSING_UNIT varchar(12),
       @CHECK_DATE datetime,
       @MONITOR_CODE nchar(10),
       @Remarks nvarchar(500),
       @Pain_Level int
  AS
  BEGIN
      SET NOCOUNT ON;
  
      If @Remarks = 'undefined'
      BEGIN
          SET @Remarks = null
      END
  
      IF NOT EXISTS (SELECT *
                     FROM [OGEN].[EMR_PATIENT_CHECK_TRACKING]
                     WHERE CHECK_DATE = @CHECK_DATE
                       AND MRN = @MRN
                       AND NURSING_UNIT = @NURSING_UNIT
                       AND HOUR = @HOUR
                       AND TIME = @TIME
                       AND MONITOR_CODE = @MONITOR_CODE)
      BEGIN
          INSERT INTO [OGEN].[EMR_PATIENT_CHECK_TRACKING] ([CHECK_DATE], [MRN], [NURSING_UNIT], [HOUR], [TIME], [MONITOR_CODE], [Pain_Level])
          VALUES (@CHECK_DATE, @MRN, @NURSING_UNIT, @HOUR, @TIME, @MONITOR_CODE, @Pain_Level)
  
          IF NOT EXISTS (SELECT *
                         FROM [OGEN].[EMR_PATIENT_CHECK_TRACKING_REMARK]
                         WHERE CHECK_DATE = @CHECK_DATE
                           AND MRN = @MRN
                           AND NURSING_UNIT = @NURSING_UNIT
                           AND HOUR = @HOUR
                           AND MONITOR_CODE = @MONITOR_CODE)
          BEGIN
             INSERT INTO [OGEN].[EMR_PATIENT_CHECK_TRACKING_REMARK] ([CHECK_DATE], [MRN], [NURSING_UNIT], [HOUR], [MONITOR_CODE], [REMARKS])
             VALUES (@CHECK_DATE, @MRN, @NURSING_UNIT, @HOUR, @MONITOR_CODE, ISNULL(@REMARKS,''))
          END
      ELSE
      BEGIN
          UPDATE [OGEN].[EMR_PATIENT_CHECK_TRACKING_REMARK]
          SET REMARKS = ISNULL(@Remarks,REMARKS)
          WHERE CHECK_DATE = @CHECK_DATE
            AND [MRN] = @MRN
            AND [NURSING_UNIT] = @NURSING_UNIT
            AND [HOUR] = @HOUR     
            AND [MONITOR_CODE] = @MONITOR_CODE
      END
    END
    ELSE
    BEGIN
        UPDATE [OGEN].[EMR_PATIENT_CHECK_TRACKING] 
        SET [Pain_Level]=@Pain_Level
        WHERE CHECK_DATE = @CHECK_DATE
          AND MRN = @MRN
          AND NURSING_UNIT = @NURSING_UNIT
          AND HOUR = @HOUR
          AND TIME = @TIME
          AND MONITOR_CODE = @MONITOR_CODE
  
        UPDATE [OGEN].[EMR_PATIENT_CHECK_TRACKING_REMARK]
        SET REMARKS = Isnull(@Remarks,REMARKS)
        WHERE CHECK_DATE = @CHECK_DATE
          AND [MRN] = @MRN
          AND [NURSING_UNIT] = @NURSING_UNIT
          AND [HOUR] = @HOUR 
          AND [MONITOR_CODE] = @MONITOR_CODE
     END
  END
  </code></pre>