Home All Groups Group Topic Archive Search About

'For Each' equivilent in SQL stored procedure?

Author
2 Oct 2006 2:47 PM
Assimalyst
Hi,

I have a situation where i want to count the number of forms input into
a database by each user, but am unsure how to do this in SQL without
passing each users name one by one as a parameter.

The database design is not ideal, and the users name is stored in the
forms table i.e:

Form1Key    User1   .........
Form2Key    User1   .........
Form3Key    User2   .........
Form4Key    User1   .........
Form5Key    User2   .........

So what i'd like to do is create a statement that counts the number of
instances of each user name resulting in a table like this:

User    Count
-------------------------
User1  3
User2  2

The only way I can see to do this is to pass each users name as a
parameter, then count the rows and return the count value one by one,
but this requires running the query several times and saving the
results in an array, but that doesn't seem very efficient.

Ideally i'd like to have a result set returned that can be conveniently
used as a datasource for a GridView. Is there any other way to do this,
my sql skills are limited so i wondered if i was missing something.

Any advise is much appreciated.

AddThis Social Bookmark Button