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
 Transact-SQL (2000)
 Please, Please help to do loop in SQL

Author  Topic 

olga
Starting Member

22 Posts

Posted - 2004-09-24 : 17:41:35
I need to export all the relevant data from the database . I'm trying to do it in the stored procedure but I don't know how to do loop in SQL.
Can anybody tell me how I can get the ID's out of the first query and than for each of those ID get the complete record?
It has to be one line per id (id represents one user)

Thank you very much in advance.]

Here is what I have:

Declare firstQuery Cursor for
SELECT distinct(a.IdDbSession) from DemographicResult a, SurveyTaker b, Questionresult c where a.IdDbSession = b.IdDbSession and a.IdDbSession = c.IdDbSession and b.SurveyID = 91
Declare @IdDbSession int;
Open firstQuery;
Fetch Next from firstQuery Into @IdDbSession;
WHILE (@@fetch_status <> -1)
BEGIN
SELECT CreatedDayTime,SurveyID,iddbSession,Flagged FROM Surveytaker
where iddbSession = @IdDbSession
SELECT Demographics_1.Demographic FROM DemographicResult INNER JOIN Demographics ON DemographicResult.DemographicID = Demographics.DemographicID INNER JOIN Demographics Demographics_1 ON DemographicResult.ResultID = Demographics_1.DemographicID
WHERE DemographicResult.idDbSession = @IdDbSession
Fetch Next from firstQuery Into @IdDbSession;
SELECT Answer FROM QuestionResult where IdDbSession = @IdDbSession
SELECT Answer FROM QuestionResult2 where IdDbSession = @IdDbSession
SELECT Answer FROM QuestionResult3 where IdDbSession = @IdDbSession
SELECT Answer FROM QuestionResult4 where IdDbSession = @IdDbSession
END;
close firstQuery;
deAllocate firstQuery;

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-09-24 : 17:43:23
Please do not post multiple times:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=40480

Tara
Go to Top of Page
   

- Advertisement -