title: Variable With TOP Keyword Can't Be Used Together
body: <p>So in the query below, I am getting the error that there is incorrect syntax next to the keyword TOP and ORDER BY. I don't see a syntax issue with either. After further testing, it seems that my database platform doesn't allow TOP and ORDER BY in a derived table. Does anyone see a workaround for this query? I can't seem to find another way to have an integer variable after TOP. The unfortunate platform that I am using is SqlDbx.</p>
  
  <pre><code>DECLARE @SQL VARCHAR(16384)
  DECLARE @CC_N INT
  
  SELECT 
      @CC_N = B.PP - A.CC_PP 
  FROM
  (
      SELECT 
          Carline, 
          SUM(PP_Floor) AS CC_PP
      FROM PP_Balancing
      WHERE Carline = '01'
      GROUP BY Carline
  ) AS A
  JOIN PP_National AS B ON A.Carline = B.Carline
  
  SET @SQL = 
  '
  UPDATE PP_Balancing
  SET PP_Floor = PP_Floor + 1
  WHERE
  Sales_Locality IN
      (
          SELECT TOP '+CAST(@CC_N AS VARCHAR(255))+' 
              Sales_Locality
          FROM PP_Balancing
          WHERE Carline = ''01''
          ORDER BY PP_Decimal DESC
      )
  AND Carline = ''01''
  '
  
  EXEC (@SQL)
  </code></pre>