473,503 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Incrementing a variable in a loop.

16 New Member
Expand|Select|Wrap|Line Numbers
  1. create procedure view_select
  2. (coll int(11))
  3. begin
  4. declare x  int;
  5. declare y int;
  6. set x=0;
  7. select count(*) into y from s_details where college_code=coll;
  8. while(x<y) do
  9. select day_number,title,first_name,middle_name,last_name,
  10. date_of_birth,e_mail,ref_number,
  11. date_of_entry_of_details,course_id,x+1 from s_details;
  12. set x=x+1;
  13. end while;
  14. end//
  15.  

How to increment the value of x alone for each record being displayed?
In this the select statement is repeated as many times as x?
How to solve this problem?
Sep 28 '07 #1
9 4569
pbmods
5,821 Recognized Expert Expert
Heya, Praveena.

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

Change:
Expand|Select|Wrap|Line Numbers
  1. date_of_entry_of_details,course_id,x+1 FROM s_details;
To:
Expand|Select|Wrap|Line Numbers
  1. date_of_entry_of_details,course_id, x := x + 1 FROM s_details;
Sep 28 '07 #2
praveena mani
16 New Member
Heya, Praveena.

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

Change:
Expand|Select|Wrap|Line Numbers
  1. date_of_entry_of_details,course_id,x+1 FROM s_details;
To:
Expand|Select|Wrap|Line Numbers
  1. date_of_entry_of_details,course_id, x := x + 1 FROM s_details;
it becomes infinite loop.
I want x to act like serial number for my table.
Everytime wen a record is displayed x shud be incremented by 1.
Can u pls help me to implement this?
Sep 28 '07 #3
pbmods
5,821 Recognized Expert Expert
Heya, Praveena.

Are you trying to set a variable LIMIT for your SELECT query?

Try using a Prepared Statement.
Sep 28 '07 #4
praveena mani
16 New Member
Heya, Praveena.

Are you trying to set a variable LIMIT for your SELECT query?

Try using a Prepared Statement.

I jus want to view the records what i ve entered in my table.
But with one more field added while displaying that is the serial number x.
I am getting the count of total number of records in the table into variable y.
So until x<y the loop should execute but it is not happening why?
Sep 28 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya, Praveena.

This line:
Expand|Select|Wrap|Line Numbers
  1. SELECT day_number,title,first_name,middle_name,last_name, 
  2. date_of_birth,e_mail,ref_number,
  3. date_of_entry_of_details,course_id,x := x + 1 FROM s_details;
  4.  
does select every record from the table. You don't need to use a loop here.
Sep 28 '07 #6
praveena mani
16 New Member
Heya, Praveena.

This line:
Expand|Select|Wrap|Line Numbers
  1. SELECT day_number,title,first_name,middle_name,last_name, 
  2. date_of_birth,e_mail,ref_number,
  3. date_of_entry_of_details,course_id,x := x + 1 FROM s_details;
  4.  
does select every record from the table. You don't need to use a loop here.
i even tried without a loop but that x=x+1 doesnt get incremented.
each time it is displaying zero only for x. Why?
Sep 28 '07 #7
pbmods
5,821 Recognized Expert Expert
Heya, Praveena.

The colon is important.
Sep 28 '07 #8
praveena mani
16 New Member
Heya, Praveena.

The colon is important.
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE view_select
  2. (coll INT(11))
  3. BEGIN
  4. DECLARE x INT;
  5. set x=0
  6. SELECT course_id,x:=x+1 FROM s_details;
  7. end//
  8.  
this is what u asked me to do right?
but this gives an error.
if i remove the colon it is working
Sep 28 '07 #9
praveena mani
16 New Member
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE view_select
  2. (coll INT(11))
  3. BEGIN
  4. DECLARE x INT;
  5. set x=0
  6. SELECT course_id,x:=x+1 FROM s_details;
  7. end//
  8.  
this is what u asked me to do right?
but this gives an error.
if i remove the colon it is working
it is working in the sense the query is ok..
but x is always displayed as 0.
Sep 28 '07 #10

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

Similar topics

6
6859
by: J. Campbell | last post by:
Hi everyone. I'm sure that this is common knowledge for many/most here. However, it was rather illuminating for me (someone learning c++) and I thought it might be helpful for someone else. I'm...
2
26300
by: Ken | last post by:
The fact that you can not reassign a variable in XSL is an endless source of frustration, causing you to jump through all sorts of non-intuitive hoops. In this case, however, the lack of...
2
4864
by: John Wilkinson | last post by:
Hi, I am new to XSLT. My problem is that I wish to create an HTML table, and give each row an incrementing number from 1.This would increment every itteration of a for-each loop. The XSLT...
2
2993
by: dan | last post by:
I have 2 tables, tab1 ( integer incremented sequence , col2, col3 ) and tab2 ( integer from tab1, col4, col5 ). When I call this function to add a record to each table: LOOP select...
27
8907
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
3
7236
by: boaring | last post by:
I need to use Macro's in Excel and always the same changes to 31 Sheets. (31 days in the Month). I'm trying to use a FOR loop but cannot get the correct format to have the sheet number to increment...
4
1812
by: masterpaladin38 | last post by:
I've been working on this problem for some time and I must say I'm stump. Any help would be appreciated. Basically what I'm trying to do is write the results of a loop to a new text file with...
2
1529
by: Arekkusu | last post by:
Hi, nice to meet everyone, im Arekkusu and im new to the forums. I have a slight problem..hopeing you guys and gals can help with. Its about trying to get a variable ($i), to increase by one every...
3
3330
by: Elementary | last post by:
I have a number of variables, e.g. X1, X2, X3, X4, X5. I want to select only the ones that are non zero(there will only be 2 or 3 at any time), and record them as Y1, Y2 and Y3 I want to know...
9
3343
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
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
7282
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
7339
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...
1
6995
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
5581
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,...
0
4678
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...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
389
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...

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.