-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableWithAutoNumber.sql
20 lines (17 loc) · 1.04 KB
/
TableWithAutoNumber.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*************************************************************************************************************************************
# Instructions:
# TableId doesn't have to be primary key, but probably should be.
# TableId has to be any integer type, int, smallint, tinyint, bigint.
# Replace the "T" with whatever prefix is required.
# Replace the 0000000 with as many 0's you want to the number to always consist of
# Replace the (7) with the number of 0's you've picked
#
# if the "Number" will be used to query big datasets just create an index on it to make database store it.
#
# technically this can work with none identity "TableId" columns as well.. but then the numbers won't be unique
*************************************************************************************************************************************/
create table dbo.TableTable(
TableId int identity(1,1) not null
,TableNumber as (('T'+left('0000000',(7)-len(convert(varchar(10),TableId))))+convert(varchar(10),TableId))
,SomeValue varchar(100) NULL
)