Click here to Skip to main content
15,910,872 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
How to get the last value found in a column and store it in variable? E.g.

id is not primary or unique key.
C#
id  name  age
6   jer   19 
8   mat   30 
9   ben   40 

i will insert this new column below, name sis, but first i want to create code that will automatically found the last higher number and when the last higher number found. the next inserted row will increment by 10. so the age of row inserted below will become 50 since the last age found is 40.
C#
10 sis _?_

To make it easier. i want a database code that will automatically found the higher/large number found in age and once the code found the higher number i will add new row. it will increment(ed) by 10.
Posted
Updated 25-Jul-13 18:56pm
v3
Comments
Sergey Alexandrovich Kryukov 25-Jul-13 21:25pm    
Can you define "last"? Keep in mind that you cannot assume any predefined order of records in a table...
—SA
Gishisoft 25-Jul-13 21:35pm    
assuming the last age is 40. so your code for the age of sis will increment by 10 since the last age found in the table which is 40. therefore when you insert sis as a new row, the age of sis will automatically incremented by the last age found which it will become to 50.
Gishisoft 25-Jul-13 21:37pm    
Your code will depend the last age found and generate a new age incremented by 10.

C#
Select TOP 1 age from table_name ORDER BY age DESC

Take this output in variable,increment it by 10,enter it in your desired field.

Call again if still have any doubts.

Regards..:laugh:
 
Share this answer
 
v2
Try this code:
@num = max(age);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900