473,769 Members | 5,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

t-sql cursor with loop problem

4 New Member
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 6854
BalochDude
3 New Member
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
2071
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: proc_evaluate_results @StudentId = '222222', Course = 'PSY101' In order to obtain those values I run a cursor accross my records and SELECT THEM INTO 2 @variables, which then replace 222222 and PSY101 with dynamic values eg.
1
21712
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 default state when the sort is over. The first row of table displays the columns titles when each cell (TD)
1
2039
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 (in another DB). In the insert trigger on Table3, a series of checks are done on the inserted record and in case of an error, an email is sent and the trigger returns. This break the cursorloop in SP1 and the rest of the records in the cursor is...
0
795
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 original color of my cursor is yellow. Anyone has suggestion? Thanks.
2
3404
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... declare EffectiveDate_Cursor cursor for select field1,fld2 from tab1,tab2 where tab1.effectivedate<Getdate()
2
23123
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 DECLARE @intPeriod smallint DECLARE @strTekst varchar(50)
1
4173
by: nareshpaaptan | last post by:
how to exit from a cursor loop on a specified condition
1
5774
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 (O)336126(G)83(I)21823(M)630065120(L)LOT-DISB2P-02(Q)2500 (O)336126(G)83(I)21823(M)630065120(L)LOT-DISB2P-03(Q)2500 (O)336126(G)83(I)21823(M)630065120(L)LOT-DISB2P-04(Q)2500 System Reads in (O) = Move Order
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.