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)
 Performing a query on each returned result

Author  Topic 

akiller
Starting Member

3 Posts

Posted - 2008-08-01 : 08:44:04
Hi All,

I'm doing some work for a company updating their crystal reports generating system and have got to a point where I'm stuck and need some help (I primarily use MySQL and am missing some of its features).

In effect, what I'm trying to achieve idealy has to be done in either a stored procedure or UDF. It would be far easier for me to write it in C# but I'm sure that would be less efficient.

I've added a tree system, whereby each user can be assigned to a parent, as in the following example:

----1000064 (AK Test)
--------1000065 (AK Sub Account 1)
------------1000066 (AK Sub Account 1_1)
--------1000067 (AK Sub Account 2)

This works fine and I'm able to retrieve the list of user IDs under a certian node. I.e. at at the root level (AK Test) these would be:

1000064
1000065
1000066
1000067

What I need to do is to perform a query on each of these IDs, in-effect I want to run (in the above example) the following queries and append their results to a single table:

SELECT * FROM [fFunction] (1000064, '2000-01-01', '2010-01-01')
SELECT * FROM [fFunction] (1000065, '2000-01-01', '2010-01-01')
SELECT * FROM [fFunction] (1000066, '2000-01-01', '2010-01-01')
SELECT * FROM [fFunction] (1000067, '2000-01-01', '2010-01-01')


I found some code online which almost works (I'm still hacking away at it so it's not efficient at all) but I need a way of telling the call to getFlightDataNew the ID of the next user ID (stored in @Import). Effectively all I need to do in the loop is UserID=@Import[@RowCnt] if I could use arrays.

DECLARE @RC int
DECLARE @ID int
DECLARE @Level int


EXEC @RC = [dbo].[sp_userHierarchy] @UserID, 1

declare @RowCnt int
declare @MaxRows int
declare @UserID int
select @RowCnt = 1

declare @Import table (UserID varchar(9))
insert into @Import SELECT ID AS UserID FROM [dbo].[userHierarchy] (@UserID ,1)

declare @tblAir table (userID varchar(50), Distance varchar(50))

select @MaxRows=count(*) from @Import
while @RowCnt <= @MaxRows
begin
SET @UserID = (SELECT TOP 1 UserID FROM @Import)

insert into @tblAir SELECT * FROM [dbo].[getFlightDataNew] (@UserID, '2000-01-01', '2010-01-01')
Select @RowCnt = @RowCnt + 1
end

SELECT * FROM @tblAir



Hopefully I've explained myself well. I know it's probably going to be something stupidly easy as it always is.

Thanks :).

BorisCallens
Yak Posting Veteran

50 Posts

Posted - 2008-08-01 : 09:58:09
While this isn't a direct answer to your question, you might want to check on rowcount (http://doc.ddart.net/mssql/sql70/set-set_32.htm)
It's a built in function to limit your output rows. This could eliminate while @RowCnt <= @MaxRows and Select @RowCnt = @RowCnt + 1

Also, I didn't completely grasp your case, but usually, when I'm asking myself how to do a procedure on each line, the problem is solvable with a join of some kind.
A bit vague, but untill the specialsts chip in, maybe this can alread get you somewhere.
Go to Top of Page

akiller
Starting Member

3 Posts

Posted - 2008-08-01 : 16:25:27
quote:
Originally posted by BorisCallens

While this isn't a direct answer to your question, you might want to check on rowcount (http://doc.ddart.net/mssql/sql70/set-set_32.htm)
It's a built in function to limit your output rows. This could eliminate while @RowCnt <= @MaxRows and Select @RowCnt = @RowCnt + 1

Also, I didn't completely grasp your case, but usually, when I'm asking myself how to do a procedure on each line, the problem is solvable with a join of some kind.
A bit vague, but untill the specialsts chip in, maybe this can alread get you somewhere.



Thanks, I hadn't heard of rowcount before (I'm still new to MSSQL). I'm using the loop at the moment as this is the part where I need to run the query on individual rows.

Here is what I'm effectively trying to do:
Get a list of all children belonging to a particular user (I have this working and it returns the IDs in temporary table) then for each one of these IDs I would like to call the getFlightDataNew() function which returns a list of all flights that user has taken and calculates various emissions figures (this function also works). The only problem I'm having is getting the two to work together :).
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-02 : 06:02:44
refer this for getting children from hierarchy

http://msdn.microsoft.com/en-us/library/aa172799(SQL.80).aspx
Go to Top of Page

akiller
Starting Member

3 Posts

Posted - 2008-08-04 : 06:01:29
Heh I searched for ages for how to get the hierarchys working and never thought to check the Microsoft sight, I'll bookmark it for future reference, cheers :).

Does anyone have any more ideas for how I can solve my other problem?
Go to Top of Page
   

- Advertisement -