473,405 Members | 2,379 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,405 software developers and data experts.

Loop headaches

dbrewerton
115 100+
Hello folks. I'm kinda stumped here so I could use a second pair of eyes. I'm trying to increment the x_device_id variable to the next ID in the database. MySQL is complaining about my incrementing the ID for some stupid reason. Here is what I have:

Expand|Select|Wrap|Line Numbers
  1. DELIMITER $$
  2. DROP PROCEDURE IF EXISTS `monthly_btus` $$
  3. CREATE PROCEDURE `monthly_btus`()
  4.  
  5. BEGIN
  6.  
  7. DECLARE x_device_id bigint;
  8. DECLARE startdate TIMESTAMP;
  9. DECLARE monthenddate TIMESTAMP;
  10. declare my_slice bigint;
  11. declare monthly_btus bigint;
  12. declare lastdate timestamp;
  13.  
  14. select device_id from ewise_device limit 1 into x_device_id;
  15.  
  16. main_loop: LOOP
  17.  
  18. if (x_device_id IS NULL) then
  19.   LEAVE main_loop;
  20. end if;
  21.  
  22. set startdate = (SELECT last_day( NOW( ) - INTERVAL 2 MONTH )); # Gets last timestamp of 2 months ago
  23. set monthenddate = (SELECT DATE_FORMAT(NOW() ,'%Y-%m-01')); # Gets last timestamp for last month
  24.  
  25. SELECT slice_id FROM `ewise_slice` WHERE device_id = x_device_id AND slice_timestamp BETWEEN startdate AND monthenddate ORDER BY slice_id DESC LIMIT 1 INTO my_slice;
  26.  
  27. SELECT slice_timestamp FROM `ewise_slice` WHERE device_id = x_device_id AND slice_timestamp BETWEEN startdate AND monthenddate ORDER BY slice_id DESC LIMIT 1 INTO lastdate;
  28.  
  29. select cumul_btus from ewise_cumulative_btus where slice_id = my_slice INTO monthly_btus;
  30.  
  31. INSERT into ewise_arch_btus (device_id,enddate,totalbtus) values(x_device_id,lastdate,monthly_btus)
  32. set x_device_id = x_device_id + 1;
  33.  
  34. END LOOP main_loop;
  35. END $$
  36.  
  37. DELIMITER ;
  38.  
Call me crazy but I really don't see why it doesn't like it. I took out my select echo statements so the code isn't so long.
Mar 24 '10 #1
0 1402

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

Similar topics

1
by: Andreas Paasch | last post by:
Hi there, below a little code snippet that causes me some headaches. I had a similar one working fine, until I needed to redo my code on this one page entirely. Now my problem is, that the test...
0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
19
by: Richard | last post by:
Hi All, I copied a script example from http://www.irt.org/script/640.htm into a local .html file. I opened that file first in HTML-kit, which hung (in an infinite loop, I think) when I...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.