473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

T-SQL Loop on selected query

Cshrek
8
hi,

i have a small query :

-----------------------------------------------------------------------

SELECT *
FROM Fares INNER JOIN Routes
ON Fares.RouteID = Routes.RouteID
WHERE Routes.Origion = @Origion
AND Routes.Destenaition = @Destenaition
AND Fares.ValidFrom < @Date AND Fares.ValidUntill > @Date

-------------------------------------------------------------------------

as i see it my first option is get the results as the are back to my code and do all the loops in there while executing another Query's, but that will consume lots of network traffic between my web application and the SQL Server.

so am wondering if there is any way to loop on each row that was returned from the query above


thank you.
Jan 18 '08 #1
7 11305
Delerna
1,134 Expert 1GB
huh ???
what loops???
what other query ???
You don't have enough info
Jan 18 '08 #2
Cshrek
8
sorry for the miss info.

i mean, after the above was executed, i like to loop on each row that the query returns, and execute another SELECT on the table that returned.

but am not so sure if it is possible, so excuse me if not please.
Jan 18 '08 #3
Delerna
1,134 Expert 1GB
I am pretty sure you can but
more detail on the first query in particular field names??
is the first query in a stored procedure or where??
more detail on the second query??
what extra info are retrieving when you run the second query on the first

perhaps a diagram of the fields and data from the first query
and a diagram of the data that will be returned by the second query
Jan 18 '08 #4
Cshrek
8
Oky thanks a lot for the willing to help.

as i already found some solution for the current problem, am still face times when i like to loop on returned tables. so i will give an example:

----------------------------------------------

SELECT * FROM Customers

will return:

Name LastName Age

Jacob Smith 35
John Doe 63
Anja Brow 43

----------------------------------------------

so now for example, i would like to get the total age of all the customers.
and so i can do SELECT SUM(Age). but in case where it will be more complicated then just the sum, how will i iterate trough each row and use the Age value for something.
any example of just how to loop to each row and do something on it such as getting the name and using it for nested select or something would be grate.


hope my question make sense. thank you very much.
Jan 18 '08 #5
Delerna
1,134 Expert 1GB
The simplest and possibly the easiest way to iterate thhrough a recordset is to use cursors (check your SQL helpfile for specifics and if you have problems ask how to resolve the specific problem). Cursors however can be slow, and the more data you have in your recordset the slower they become. Therefore cursors are only recommended when you have small amounts of data and you can't come up with a set base solution.

Set based solutions are when you work with the recordset as a whole rather than stepping through it 1 record at a time. Set based solutions are much faster than cursors.
As far as examples are concerned, there are a million and 1 possible ways of achieving a result and the particular solution you come up with will depend on the data you have and the results you want to achieve. I probably exagerate with 1 million and one, but youve heard the expression "theres more than 1 way to skin a cat"
Give me half an hour and I will try and post something here that will get you started.
Jan 18 '08 #6
Delerna
1,134 Expert 1GB
Sorry couldn't think up a simple scenario that made sense to do in a loop so I will just do an example without worrying about making sense

we have this table called tblStock with the fields
Sun,Mon,Tue,Wed,Thu,Fri

and on sunday there is a certain amount of stock recorded. At the end of each day the amount that has been added and removed is recorded in another table called tblTodaysMovement. After the movements have been recorded the exampl loop is run to update the stock

Expand|Select|Wrap|Line Numbers
  1. Declare @Day int,@Added int,@Removed int, @Stock int
  2. set @Day=1
  3. while @Day<5
  4. begin
  5.    set @Added=(Select Added from tblTodaysMovement)
  6.    set @Removed=(Select Removed from tblTodaysMovement)
  7.    set @Stock=(Select case when @Day=1 then Sun else 0 end + 
  8.                                  case when @Day=2 then Mon else 0 end +
  9.                                  case when @Day=3 then Tue else 0 end +
  10.                                  case when @Day=4 then Wed else 0 end +
  11.                                  case when @Day=5 then Thu else 0 end 
  12.  
  13.    if @Day=1 begin update tblstock set Mon=@Stock+@Added-@Removed end
  14.    if @Day=2 begin update tblstock set Tue=@Stock+@Added-@Removed end
  15.    if @Day=3 begin update tblstock set Wed=@Stock+@Added-@Removed end
  16.    if @Day=4 begin update tblstock set Thu=@Stock+@Added-@Removed end
  17.    if @Day=5 begin update tblstock set Fri=@Stock+@Added-@Removed end
  18.    set @Date=@Date+1
  19. end
  20.  
As I said, this is not a good example scenario and there is no way I would do it like this. It is just an example of one way of looping a query and adjusting it according to parameters that change on each loop. I have better examples but they are too long and complex to explain here.

I hope you find this helpful in getting you started
Jan 18 '08 #7
Cshrek
8
oh, yeah it seems like very good answer.
thanks a lot (:
Jan 18 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: shivers3000 | last post by:
Can someone explain why $displayResults will not query twice? Scripts Objective: to take a car's VIN retrieve the car info and display in nice rows Issue with Script: The while loop displays...
1
by: NANCY HEHIR | last post by:
I am using Report Writer for Ingres II. Is it possible to write a query in a loop? e.g. My table is like this time position 09:01 pos01 09:02 pos03 09:02 pos01 09:04 pos05
1
by: ED | last post by:
I currently have an ODBC query that hits an Oracle database. I want to bring back records for a given month based on a job completion date in the Oracle database. I would like to have the user...
5
by: strauss.sean | last post by:
Hi! I am trying to import a query as a table from a MS Access database in a specified drive, path, and filename; my filenames and paths are being stored in a table for easy reconfiguration....
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
1
by: ohana | last post by:
I am trying to design Query and I need to use looping and I do not know how?
2
by: Chucara | last post by:
Hi, I'm trying to build a simple search in Access. I'll just give a simplified example, as I think I can solve the problem, if you can help me with this subproblem.. I have 2 listboxes -...
1
by: ljungers | last post by:
Hi and I hope that someone may have an answer for this, or an example of what I need to do. I have a Access database that a clerk will be entering a Order Number or Client Name or a Client City in a...
10
by: Dan2kx | last post by:
Still duin the holiday database... and i have discovered something that i cant explain... can you guys? ok i have a procedure which basically (quite a longwinded process) selects the start date...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.