How to add an IDENTITY INT or varchar value on SELECT INTO WITH SQL Server

Hi, Adding Identity Column in SELECT INTO Statement in SQL SERVER

When copying data into a new table using SELECT INTO it can be to add an identity column. To do this we can just define an identity column in the select into the statement.

Note: Is used only in a SELECT statement with an INTO table clause to insert an identity column into a new table.

HOW TO ADD IDENTITY WITH SELECT INTO

SELECT IDENTITY(INT,1,1) AS Id, Name, LastName FROM Customer
adding value int identity with select into

HOW TO ADD IDENTITY VARCHAR WITH SELECT INTO

CREATE TABLE BTOMER(
  ID int IDENTITY NOT NULL, 
  CustomerNo AS 'BT'+CAST(ID AS VARCHAR(100)) PERSISTED PRIMARY KEY, 
  CustomerName VARCHAR(50)
)
INSERT INTO BTOMER (CustomerName) VALUES ('ADMIN')

SELECT * FROM BTOMER
adding value varchar identity with select into

Thank You!

Leave a Reply