Question : Sql Help Must declare the scalar variable "@TotalRecords".

I'm getting this error. Must declare the scalar variable "@TotalRecords".

Has to do with this line..
@TotalRecords AS TotalRecords,

When I hard code @TotalRecords, it works.

ALTER PROCEDURE [dbo].[GetECommerceItems]
      @PageIndex int,
      @NumRows int,
      @Vendor varchar(50) = null,
      @ItemNo varchar(50) = null,
      @Category varchar(50) = null,
      @Sort varchar(100) = null
AS

DECLARE @TotalRecords int
DECLARE @StartRowIndex int

SET @StartRowIndex  = (@PageIndex * @NumRows) + 1

SELECT @TotalRecords =
(
SELECT COUNT(*)
FROM
      aspnet_webItems
WHERE
      Ecommerce = 'Y'      
AND
      (@Vendor IS NULL OR Vendor = @Vendor)
AND
      (@ItemNo IS NULL OR Item_No = @ItemNo)            
AND
      (@Category IS NULL OR Category = @Category)      
)

SELECT
      IDENTITY(int, 1,1) AS Row,
      Item_No,
      Vendor,
      Memo_Desc,
      [Desc],
      imgProduct
INTO #OrderRows      
FROM
      aspnet_webItems      
WHERE
      Ecommerce = 'Y'      
AND
      (@Vendor IS NULL OR Vendor = @Vendor)
AND
      (@ItemNo IS NULL OR Item_No = @ItemNo)      
AND
      (@Category IS NULL OR Category = @Category)      

--WITH OrderRows AS
--(
--SELECT
--      ROW_NUMBER() OVER (Order BY Item_No) AS Row,
--      Item_No,
--      Vendor,
--      Memo_Desc,
--      [Desc],
--      imgProduct
--FROM
--      aspnet_webItems      
--WHERE
--      Ecommerce = 'Y'      
--AND
--      (@Vendor IS NULL OR Vendor = @Vendor)
--AND
--      (@ItemNo IS NULL OR Item_No = @ItemNo)                  
--)

DECLARE @SQL AS NVARCHAR(3000)
SET @SQL = '
SELECT
      Item_No,
      Vendor,
      Memo_Desc,
      [Desc],
      @TotalRecords AS TotalRecords,
      imgProduct
FROM
      #OrderRows
WHERE
      Row BETWEEN ' + Convert(varchar(50), @StartRowIndex) + ' AND '  + Convert(varchar(50), @StartRowIndex + @NumRows - 1)      

IF @Sort IS NOT NULL
BEGIN
      SET @SQL = @SQL + ' ORDER BY ' + @Sort
END      
      
exec sp_executesql @SQL
      
DROP TABLE #OrderRows
      

Answer : Sql Help Must declare the scalar variable "@TotalRecords".

this should help
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
   pFile = fopen ("C:\\myfile.txt" , "r");
  
   fgets (env , 100 , pFile);
   fclose (pFile);

   // fetch XXXXXXX
   const char * lastSlash = strrchr(env, '//');
   if(lastSlash != NULL) 
   {
      const char* lastPeriod = strchr(lastSlash, '.');
      if(lastPeriod != NULL) 
      {
         // Better to copy to some other var, for ex. overwriting env
         strncpy(env, lastSlash + 1, lastPeriod - lastSlash - 1);
      }
   }
Random Solutions  
 
programming4us programming4us