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

How to increment a variable inside FOR loop in Stored procedure?

Hi all,

I have a trivial stored procedure that needs to reorder data by changing displayorder field value.

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE SVC.REORDER_DIVISIONS_ALPHA
  2. (
  3.  IN code VARCHAR(12),
  4.  IN year INTEGER
  5. )
  6. LANGUAGE SQL P1:
  7. BEGIN
  8.     DECLARE counter INTEGER;
  9.     SET counter = 1;
  10.     FOR divisions AS 
  11.        SELECT ID FROM SVC.DIVISIONS SV 
  12.            WHERE   SV.LYEAR = year AND CODE = code 
  13.            ORDER BY NAME ASC
  14.  
  15.        DO
  16.          UPDATE SVC.DIVISIONS S SET S.DISPLAYORDER = counter WHERE S.ID = ID;
  17.          SET counter = counter + 1;    
  18.     END FOR;
  19.  
  20. END P1
  21. @
  22.  
I get result where all my displayorder fields are set to the same strange value of 292.

If I remove the line "SET counter = counter + 1;" I get all my values set to 1.

My goal is to have the values in the displayorder field to be 1,2,3,4,5 etc.

What am I missing?

THanks.
Feb 9 '11 #1
1 18605
Rabbit
12,516 Expert Mod 8TB
I don't know the DB2 sytnax but I'm pretty sure you meant to use S.ID = divisions and not S.ID = ID
Feb 9 '11 #2

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

Similar topics

15
by: Jarrod Morrison | last post by:
Hi All Im generally a vb programmer and am used to referencing multiple records returned from a query performed on an sql database and im trying to move some functions of my software into sql...
5
by: JMack | last post by:
Okay, so I have a problem and I would be REALLY grateful for any assistance anyone can offer because I have found little or no help on the web anywhere. I want to access and do joins between...
2
by: andres | last post by:
hi all, thanks for everyone for your support! i have a report being built in studio 2005. It calls a stored procedure. The SP runs a query and gets a data set which then loops through to get...
1
by: Arielle | last post by:
Okay here's what I'm trying to accomplish. I'm trying to create a stored procedure to make my life simple when it comes to a form I'm creating. One of the fields in it however is a calculation...
1
by: eRTIS SQL | last post by:
hi, I want to use a stored procedure inside a stored procedure simulteanously changing the database. this is my base store procedure alter PROCEDURE create_file @dbname sysname AS declare...
1
by: uvasag | last post by:
I'm converting SQL Server code into Oracle and would like to know if I can create and drop a function from inside an Oracle stored procedure. The SQL Server code seems to do it but when I try the...
1
by: Ir0neagle | last post by:
I am generating upgrade/new install scripts for my project. I am able to do this in oracle and sql server but an running into problems in db2. What I am trying to do is to use some logic to only...
0
by: jith87 | last post by:
hi, i need to handle a "NOT FOUND"exception inside Stored Procedure in MySQL. i get an error in the syntax.here is my code... DROP PROCEDURE IF EXISTS insert1// create procedure...
1
by: kensushi | last post by:
Hi, Im somewhat new to oracle SP writing. I need to do the following from the SP 1. execute the query 2. create a view and populate with the result of the above query 3 query that view and...
1
by: AngelaA | last post by:
The syntax to access a table is Select .. . How do I make the DatabaseName a variable – ideally to be passed as a parameter into the stored procedure? The reason being my test database uses a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.