Question : How do I convert a stored procedure to be used for batch processing?

I have created a stored procedure that currently calculates results for one set of parameters. I have setup a table that would store multiple rows of parameters and would like to loop through the stored procedure to calculate results for each row. How can this looping be setup?

Answer : How do I convert a stored procedure to be used for batch processing?

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 !
Random Solutions  
 
programming4us programming4us