Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Best way to select max auto number ?

Author  Topic 

naruponk
Starting Member

1 Post

Posted - 2010-10-09 : 03:43:49
Hi,

I have a table named as po
This table has the following columns:
ID (auto number), pono, podate,membercode

15 computers will query the last number of po.pono
some time 2-3 computers will get the same number
of max(pono)

I have added the Begin Trans / Commit Trans in my program
to avoid for getting the same number.
But they still get the same number

How can I do to get unique number as result?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-09 : 03:48:39
You can lock the row (UPDLOCK) to prevent others from getting it, but you are going to need some mechanism to specify which rows are locked so that the next query doesn't just get stuck on a locked row. Add a column to indicate the locking.


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-10-09 : 13:43:44
is the nature of your table PO that everytime query is run against this table (regardless of number of computers) one should get different results?

after reading your problem i assume users are getting the correct data returned from the table, because data stored in table are not getting any new values.

Usually when a simple select is run against the table it locks the table in order to get correct results (and avoid dirty reads).
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-10-11 : 05:58:14
INSERT ...
OUTPUT inserted.pono

Make use of the SQL Server 2005 and later OUTPUT functionality.
See http://weblogs.sqlteam.com/peterl/archive/2007/10/03/New-OUTPUT-operator.aspx



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -