Solved

Automatically get record column content + 1

Posted on 2008-10-06
3
207 Views
Last Modified: 2013-11-05
Hello,

We are experiencing some problems when executing the next SQL query:

SELECT MAX(Code) + 1 FROM [@OLL1]

This query works fine for the first 10 results, but after the column content is 10 or higher it always returns 10; without adding the +1..

How can we get the next itterated number? Is there any other sql function to use?

0
Comment
Question by:AGION
3 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22649350
>>This query works fine for the first 10 results
I would think that your query would only return one record

try this instead since you're on 2005


select code, row_number() over(order by code desc)
from @OLL1

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22649356
your field (code) is varchar and not int.
change the data type to int, and you should get better values.

NOW, if this is to generate the next code value for a INSERT, you really should make that column int (identity), so sql server will generate the value for you (multi-user-safe !!!), you will then retrieve the generated value with, for example:

SELECT SCOPE_IDENTITY()

after the insert.
0
 

Author Closing Comment

by:AGION
ID: 31503380
The only problem is that it's a Table that is created by a SAP application, which has strict standard. But i solved it by converting it in the query itself.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now