Author |
Topic |
sateesh.sqldba
Starting Member
25 Posts |
Posted - 2010-07-21 : 07:51:27
|
Hi experts, i want to create column like this data col1custmer00001custmer00002custmer00003||||customer10000that must be in auto incrmentalthanq so much in advancesateesh |
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2010-07-21 : 08:38:52
|
You can use identity column as a base for another computed column that produce the output you want. Something like this:CREATE TABLE T1( id SMALLINT NOT NULL IDENTITY, col1 AS 'customer' + RIGHT('00000' + CAST(id AS VARCHAR(5)), 5)); |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-07-21 : 10:12:32
|
quote: Originally posted by malpashaa You can use identity column as a base for another computed column that produce the output you want. Something like this:CREATE TABLE T1( id SMALLINT NOT NULL IDENTITY, col1 AS 'customer' + RIGHT('00000' + CAST(id AS VARCHAR(5)), 5));
There is no need to specify NOT NULL for id column as it is a Identity columnMadhivananFailing to plan is Planning to fail |
 |
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2010-07-21 : 11:06:41
|
quote: There is no need to specify NOT NULL for id column as it is a Identity columnMadhivanan
You are right. But I prefer to include the constraint even if it is redundant. By the way SQL Server Management Studio also includes the constraint in creation script. |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-07-21 : 12:43:12
|
"There is no need to specify NOT NULL for id column as it is a Identity column"Required if it is a PK column?Possibly will be used as a hint to Query Optimiser?(Dunno the answer to either, just thinking out loud ) |
 |
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2010-07-21 : 13:46:25
|
quote: Originally posted by Kristen "There is no need to specify NOT NULL for id column as it is a Identity column"Required if it is a PK column?Possibly will be used as a hint to Query Optimiser?(Dunno the answer to either, just thinking out loud )
Actually IDENTITY attribute is not allowed on a nullable column. So when you do not constrain the column with NOT NULL constraint, the constraint will be added automatically behind the scene. That is why I prefer to make it crystal clear, and add the NOT NULL constraint manually. |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-07-21 : 18:26:44
|
I do that too. I didn't know I didn't need to but, like you, I explicitly do such things anyway for the avoidance of doubt / during maintenance / by others. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
sateesh.sqldba
Starting Member
25 Posts |
Posted - 2010-07-21 : 23:17:08
|
Thank you so much madhivananquote: Originally posted by madhivanan
quote: Originally posted by malpashaa You can use identity column as a base for another computed column that produce the output you want. Something like this:CREATE TABLE T1( id SMALLINT NOT NULL IDENTITY, col1 AS 'customer' + RIGHT('00000' + CAST(id AS VARCHAR(5)), 5));
There is no need to specify NOT NULL for id column as it is a Identity columnMadhivananFailing to plan is Planning to fail
sateesh |
 |
|
|