title: Failed to compare two TSQL variables
body: <p>I am having a little problem with comparing two variables in TSQL.</p>
  
  <p>Here's the code</p>
  
  <pre><code>SET ANSI_NULLS ON
  SET QUOTED_IDENTIFIER ON
  GO
  ALTER PROCEDURE [dbo].[sspUpdateActivityDate] (
      @e nvarchar(15),
      @i nvarchar(11),
      @c nvarchar(50), 
      @u nvarchar(50)
  )
  AS
  BEGIN
      DECLARE @var_Count int
      DECLARE @var_RecordLimit int
  
      SET @var_RecordLimit = 20
  
      SELECT @var_Count = COUNT(*) FROM tblLastAccess WHERE employeeID = @e
      IF @var_Count &gt; @var_RecordLimit
          BEGIN
              DELETE FROM tblLastAccess
                  WHERE lastDate NOT IN (
                      SELECT TOP @var_RecordLimit lastDate
                      FROM tblLastAccess
                      WHERE employeeID = @e
                      ORDER BY lastDate DESC
                  )
          END
      INSERT INTO tblLastAccess(employeeID, ip, computerName, username) VALUES (@e, @i, @c, @u)
  END
  </code></pre>
  
  <p>Error says</p>
  
  <blockquote>
    <p>Msg 102, Level 15, State 1, Procedure sspUpdateActivityDate, Line 19<br>
    Incorrect syntax near '@var_RecordLimit'.</p>
  </blockquote>
  
  <p>Which points after the comparison between @var_Count and @var_RecordLimit (as far as I understand it)</p>
  
  <p>The idea is to add records but limit the record count dynamically, which I set initially to 20. The code block inside the IF is to delete the records next to <strong><em>TOP n</em></strong> values.</p>
  
  <p>I am using SQL Server 2000 by the way.</p>
  
  <p>Please guide me.</p>
  
  <p><em>It is for update purposes by the way, that is why I am using ALTER and not CREATE</em></p>