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 2005 Forums
 Transact-SQL (2005)
 help with looping in stored procedure

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2008-05-15 : 04:26:17

help combinign stored procedure queries


first get userid

select distinct userid from allrecords where stage=1 and datediff(day,0,mydatestage1)=datediff(day,0,getdate()-1)


then i need to take this and loop through the results and do queries based on the results.

like


select @username=username from users where userid=@userid
select @totalrecords=count(id) from allrecords where stage=1 and datediff(day,0,mydatestage1)=datediff(day,0,getdate()-1)

and more

how can i do this looping through the users and then running sub queries?

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2008-05-15 : 04:31:41
You can use a WHILE, but not too sure that this is what you are after, please explain exactly what you are trying to do and you may get a better way to go about it.
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2008-05-15 : 04:40:55
ok basically i need to first query a list of users and then loop through and run a few sql statements on those users - - i'm looking to return a recordset with the user and then the counts from my other sql statements.
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2008-05-15 : 04:45:19
So why loop one by one? Why not do what SQL is good for and try doing it set based?

You have enough posts to know by now that SQL is set based and the quickest way to get an answer is to post examples and what you have so far surely?
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2008-05-15 : 04:52:57
ok thought it would be better to make an sql sp to return all

i guess i'll try that in asp calling sql

tahnks
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2008-05-15 : 05:00:28
That doesn't make sense either. Why not just produce all you results at the backend and publish them to the front end? That is what SQL is for, why do you feel you need to pass through userids one at a time?
Go to Top of Page
   

- Advertisement -