MySQL Procedure To Get Incremental Numbers
Published by philipnorton42 on Mon, 03/09/2009 - 18:32Run the following table definition to create a table called tests in the test database.
1 2 3 | <p>Fill it with some default values:
<pre language="sql}INSERT INTO tests(var) VALUES('one'), ('two'), ('three'), ('four'); |
Now lets say that you want to find the values in the table, but that you want to increment a value for each row, the following procedure will do this. If you are using the MySQL Query browser then open a script table and past the following and execute, changing any database/table definitions.
1 2 3 | <p>You will now be able to call the procedure in the following way:</p> <pre language="sql}CALL Increment(4); |
The value that you pass to the procedure is used to work out the increment. So in this case the output would be something like the following:
1 2 3 4 5 | id RunningSum 1 4 2 8 3 12 4 16 |
Category:
Add new comment