简体   繁体   中英

How do I create a temporary table with identity column?

I am planning to create a temporary table with an extra column( Id ) that will generate a number sequentially when new data is added

Id    LastName    FirstNmae
...  ..........  ...........
1        A           B
2        C           D

The column Id should maintain order for the insert logic ie, Id should increment each time an insert occurs. I also need to retrieve data from the table according to the order of Id . How do I do that?

When creating the table set NUM as IDENTITY field

CREATE TABLE #Table
(
NUM int NOT NULL IDENTITY(1,1),
FirstName varchar(255),
LastName varchar(255)
)

Now insert leaving NUM column(it will automatically increment

INSERT INTO #Table VALUES('A','B')
INSERT INTO #Table VALUES('C','D')

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM