Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

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

Avatar of JRockFL
JRockFLFlag for United States of America asked on
Microsoft SQL Server
4 Comments1 Solution1195 ViewsLast Modified:
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
      
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answers