Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Re-using dynamic stored procedures in rs loop

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-11-20 : 08:01:36
Dominic Allkins writes "I'm currently using a ADO command to do a count of records for each category in a categories recordset - looping thru the recordset and running the count each time, i.e...

Set cmd = Server.CreateObject.....

While Not rs.EOF
...
cmd.CommandText = "SELECT Count(*) AS Count... etc.... WHERE Category = " & rs("CatID")
...
Set rsCount = cmd.Execute

rs.MoveNext

Wend

Set cmd = Nothing
This works fine, but I'm trying to take the load off the web server and use the database server as much as possible, even for doing simple stuff like this... so I tried doing it with a stored procedure. The SP is a dynamic SP that uses a @WhereClause and EXEC - so I thought the simple thing would be to redefine the @WhereClause as I loop thru the recordset and call the SP each time, i.e.
Set cmd = Server....

While Not rs.EOF
...
WhereClause = searchSQL & rs("CatID")
...
cmd.Parameters ("@WhereClause") = WhereClause
...

Set rsCount = cmd.Execute

rs.MoveNext

Wend

Set cmd = Nothing
When I do it this way, it throws an error - too many arguments specified for SP as soon as it hits the second record in the categories recordset.

I adapted it and re-instantiated the cmd object and closed it within the rs loop and it worked fine, but then all I'm doing is loading up the web server by closing and re-instantiating the cmd object everytime, this losing the benefit of using the SP - even if it only a small benefit in this example.

Any ideas???

Thanks

Dominic"

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2002-11-20 : 08:15:30
Can you use

SELECT Category, Count(*) As NumOfRecords
FROM
Table
GROUP BY Category

to return 1 result set -- which is 100% run on the server in 1 pass - and then work from there? MUCH more efficient than running a stored proc or command text over and over and over ....

- Jeff
Go to Top of Page
   

- Advertisement -