the "@@ROWCOUNT" Returns the number of rows affected by the LAST STATEMENT; and at the point your declare it,your last statement is "CREATE TABLE"...so just set your "CREATE TABLE " statement after your rowcount declaration as below:
CREATE TABLE #ActiveCustomer (
RowID int IDENTITY(1, 1),
moduleID int,
moduleName varchar(50)
)
DECLARE @NumberRecords int, @RowCount int
DECLARE @moduleID int, @moduleName varchar(50)
-- Insert the resultset we want to loop through
-- into the temporary table
INSERT INTO #ActiveCustomer (moduleID, moduleName)
SELECT moduleID, moduleName
FROM Modules
-- Get the number of records in the temporary table
SET @NumberRecords = @@ROWCOUNT
SET @RowCount = 1
CREATE TABLE #Loop (
moduleID int,
moduleName varchar(50)
)
-- loop through all records in the temporary table
-- using the WHILE loop construct
WHILE @RowCount <= @NumberRecords
BEGIN
SELECT @moduleID = moduleID, @moduleName = moduleName
FROM #ActiveCustomer
WHERE RowID = @RowCount
INSERT INTO #Loop (moduleID, moduleName)
SELECT moduleID, moduleName
FROM #ActiveCustomer
WHERE RowID = @RowCount
SET @RowCount = @RowCount + 1
END
SELECT * FROM #Loop
-- drop the temporary table
DROP TABLE #ActiveCustomer
DROP TABLE #Loop
This work !