473,385 Members | 1,901 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,385 software developers and data experts.

t-sql cursor with loop problem

4
i am working on a web analysis project. i am having difficulty with counting hits and visits (a group of hits without a 30 minute break between hits). i decided to build a cursor in order to calculate visits. however, i have no experience with coding cursors and very little experience with both loops and t-sql.

in the cursor, it gets the data from a table that contains the IIS log (used logparser to transfer the logs to sql server). it will then calculate the hits and visits and insert this data into another table that will store a record of total hits and visits for each day.

the problem i am having is that there is no output from this current code. i can't seem to get it to loop back through the if statements and calculate.

any help?

Expand|Select|Wrap|Line Numbers
  1. set nocount on
  2.  
  3. declare @datetime datetime, @username varchar(30), @holddate datetime, @holdusername varchar(30), @hits int, @visits int, @counter int, @inshits int, @insvisits int
  4.  
  5. set @counter = 0
  6. declare visitcalculator cursor
  7. for select datetime, cs_username from testmetrics.dbo.alldata1 order by cs_username, datetime
  8.  
  9. begin
  10. open visitcalculator
  11.  
  12.  
  13. fetch next from visitcalculator
  14. into @datetime, @username
  15.  
  16. while @@fetch_status = 0
  17. begin 
  18. if @username = @holdusername --compares username to previous row.  
  19.     begin
  20.     set @hits = @hits + 1 --count hits
  21.     set @holdusername = @username
  22.     set @holddate = @datetime
  23.     fetch next from visitcalculator into @datetime, @username
  24.     end
  25.  
  26.   if @username = @holdusername and datediff(minute, @holddate, @datetime) > 30  --find a break of 30 or more minutes
  27.     begin
  28.    set @visits = @visits + 1 --count visits
  29.  set @holdusername = @username
  30.     set @holddate = @datetime
  31.     fetch next from visitcalculator into @datetime, @username
  32.     end
  33.  
  34.  if @username != @holdusername
  35. begin 
  36. set @holdusername = @username
  37. set @holddate = @datetime
  38. fetch next from visitcalculator into @datetime, @username  --grab next row. 
  39. set @visits = @insvisits --transfer visits to insert
  40. set @hits = @inshits --transfer hits to insert
  41. set @visits = 0 --reset visits
  42. set @hits = 0 --reset hits
  43. set @counter = @counter + 1 --add to counter
  44. end 
  45.  
  46. if @counter > 1 --write data for each user into table
  47. begin
  48. insert into testmetrics.dbo.userinfo (username, date, hits, visits) values (@holdusername, @holddate, @inshits, @insvisits)
  49. end 
  50.  
  51. end 
  52. end
  53. close visitcalculator
  54. deallocate visitcalculator
Jul 25 '07 #1
1 6843
A simple example of how to write sql cursors. You may want to download the zip file (which contains the sql file in it.)

http://www.chapterzero.co.uk/article...r-cursors.aspx

Cheers!
Sep 14 '07 #2

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

Similar topics

4
by: R. Z. | last post by:
I 've have a stored procedure that compares fields across databases. In order to do so it requires 2 values it acquires from 2 tables. The search is based on the ID of the data owner and a subject:...
1
by: Yaron Cohen | last post by:
Hi, I would like to ask for your help (I am using only IE5.5). I have a very big table that I would like to sort. During the sort I would like the cursor changed to 'wait' state and back to...
1
by: Søren Larsen | last post by:
In a stored procedure (SP1) I am looping through a cursor with records from Table1. Each record in the cursor is inserted into Table2. Insert trigger on Table2 is inserting the record into Table3...
0
by: Jasonsky | last post by:
I add this code in my InitializeComponent() procedure Me.Cursor = New Cursor("D:\WindowsApplication1\cur\Yellow.cur") I don't know why the color of the cursor become "Black and white", even the...
2
by: satishchandra999 | last post by:
I have SP, which has a cursor iterations. Need to call another SP for every loop iteration of the cursor. The pseudo code is as follows.. Create proc1 as Begin Variable declrations... ...
2
by: Chris Zopers | last post by:
Hello, I've created a stored procedure that loops through a cursor, with the following example code: DECLARE curPeriod CURSOR LOCAL for SELECT * FROM tblPeriods DECLARE @intYear smallint...
1
by: nareshpaaptan | last post by:
how to exit from a cursor loop on a specified condition
1
by: yeohyc | last post by:
Hi All, I have a problem here. I have done a request for move order it was stored into a custom table with the format: (O)336126(G)83(I)21823(M)630065120(L)LOT-DISB2P-01(Q)2500...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.