Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Recently I saw a video in YouTube where one of the SQL developers was saying that "I never used a cursor. When developers have no other idea then they use it cursor".
Now this is quite strange. How will one iterate without using cursor and while loop. Yes joins can be an option. But is it possible we can get ride of cursor 100%.

I have written a stored procedure, where I am using a cursor and it fetches all the records and I am doing calculation type of things for all the records and then final row I am showing the avg(particular column), sum(particular column).

for eg, I am showing only 3 columns as of now. Now I m using cursor to fetch each hub(A,B..)
and then calculate.

-->to calcualte totalrequest
I calculate totalpick
I calculate totaldrop
totalrequest=totalpick + totaldrop
(lot of joins used)

-->avgrequest(using joins from multiple tables)
-->occupancy(using joins from multiple tables)
==
then after the cursor ends
-->calculate totalsum of totalrequest
-->calculate average of avgrequest
-->calculate occupancy average
hub   totalrequest  avgrequest  occupancy  

A         34          20         2.4

B         16          20         1.6

C         20          20         1.0

D         10          10         2.5

E         20          30         2.5

FINAL     100         20         2.5


Is it possible to handle it in set based operation. This SP runs slow as in realtime I am doing it for one month data.

Kindly need your intervention..please help.
Posted
Updated 22-Dec-13 11:43am
v4
Comments
Madhav Hatwalne 21-Dec-13 13:35pm    
R u tried using hashtables????
anurag19289 21-Dec-13 13:56pm    
nopes....
anurag19289 21-Dec-13 14:03pm    
I have never used it. Will it solve the problem..can you suggest me a good site so that i can get a clear picture of it...

It is almost always possible to not use cursors. One thing that can help is to use CTEs ( common table expressions ) to turn elements of your task in to discrete statements. You can do most calculations inline in your SQL. You can use group by to calculate totals and averages within groups. There are all sorts of ways to avoid RBAR (row by agonising row ) queries, and to do set based operations, which is what SQL is for.
 
Share this answer
 
Comments
anurag19289 23-Dec-13 1:15am    
so is it i have to use recursive cte or normal cte can do it? I will take an example and will try and then get back to you.
Christian Graus 23-Dec-13 4:14am    
You don't need recursion, a CTE is not NEEDED, it simply helps to make the code cleaner and more readable. If you posted the data you're using, and the code, then perhaps we could help more
anurag19289 23-Dec-13 14:54pm    
actually the stored procedure is about 440 lines....so i thought it would create much confusion...so i didn't post
Christian Graus 23-Dec-13 16:10pm    
Yeah, if you had, people would have said it was a code dump. Can you create a shortened version that just gets across the point of why you think you need a cursor ?
In past, I have answered similar question, check it out alternatives
Cursor alternatives[^]
 
Share this answer
 
Comments
anurag19289 23-Dec-13 14:53pm    
good links

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900