Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


identity insql server


identity insql server

Author
Message
sunileadara1986
sunileadara1986
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 15
i want to put identity to a column bt tht column datatype must be varchar and tht identity must be starts with P001.pls help me
thank u
jgrubb
jgrubb
Old Hand
Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)

Group: General Forum Members
Points: 320 Visits: 567
First, be careful of your terms. "Identity" means something specific in sql server. In this case, you can't use it directly, since it must be a number or a guid. It exists because to rapidly assign unique values to a table with little overhead. It was not intended to be a user visible field or hold updatable information. Avoid exposing it. What you want is a unique key.

Some options:

1) Insert trigger. Create a column of any type, make it a unique index (possibly the Primary Key) and you can assign a value programatically, or get the last with a lookup. You can store the last value somewhere, for example in a table. I worked on an "Enterprise" application that had 1 table with 1 row for each application table to hold the last value. Not suggesting that, since I thought it silly, but you can go that route. I've also seen another that had a table with 1 row per table with the last value. Lock the record to enforce integrity. It does serialize inserts.

2) make a compound key, with say (assuming the value changes), a char field containing the prefix, and an int identity field. Then build a view that casts them together as a char.

There's more, but this is off the top of my head. Both have some performance impacts, and the shape of your data and usage patterns will indicate the best path.
Steve Jones
Steve Jones
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: Administrators
Points: 41094 Visits: 18868
I'd use an int identity column and then a computed column or a view that concatenates them.

You can't do a varchar auto-incrementing field.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
sunileadara1986
sunileadara1986
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 15
thank u sir
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search