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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Autosequence Generation

Author  Topic 

buzzi
Starting Member

48 Posts

Posted - 2009-03-30 : 18:07:36
Hello All,
I have concurrency issue with one of our table and am trying to resolve the issue,Can somebody help me clarify this

The table has a varchar field that is custom generated based on a function

the first 5 characters currently holds 2 types, which are standard based on the input,
and the rest of the value is incrementing by 1 for each insert


Type1:9999A
Type2:8888B

SAMPLE DATA

col1 col2
1 9999A101
2 9999A102
3 9999A103
4 8888B101
5 8888B102

the select statement i use to get the next increment value is like below:

if we assume the input type is "A"; then the 2 paramters specified in the select will be as:
@type = '9999A'
@typeref = '9999A%'

and the output should be
basenumber = 104

and then i have other formating statements which will give the final value of '9999A104' as per the sample data

select @basenumber= SUBSTRING(col2,len(@type) + 1,(len(col2) - len(@type))) + 1 FROM table where col2 like @typeref

I currently see 2 problems which i rectified
1.the column was missing a unique constraint
2.added the index on col2

earlier it was doing a clusted index scan, now its doing a index seek, hope this helps a bit. But the problem is i am not storing the old & new values anywhere, and am running into the concurrency issue.

Thanks a lot for the help

darkdusky
Aged Yak Warrior

591 Posts

Posted - 2009-03-31 : 09:24:45
You can use an identity column in the same table as the base for the incrementing number and have a second computed column which uses the identity column and a function to generate a custom code. this removes possibility of duplication so it can have a unique index.

http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-31 : 09:36:57
Also refer http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=57069

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -