Uncategorized

SELECT INTO

Backup the entire table into a new table:

SELECT *
	INTO [dbo].[NewTable]
FROM [dbo].[ExistingTable]

Select only particular columns into the new table or rename existing columns to new names:

SELECT [Col1], [Col2] AS [NewCol2Name]
	INTO [dbo].[NewTable]
FROM [dbo].[ExistingTable]

Limit and filter the number of rows in the new table using TOP and WHERE:

SELECT TOP(10) *
	INTO [dbo].[NewTable]
FROM [dbo].[ExistingTable]
WHERE [Col1] = 'Value'

Create a new IDENTITY column:

SELECT 
	IDENTITY(INT,1,1) AS [Id]
	INTO [dbo].[New_Table]
FROM [dbo].[Existing_Table]
GO

Copy table data to a new table in a different filegroup (SQL Server 2016 SP2 and higher):

SELECT *
	INTO [dbo].[NewTable] ON [DifferentFilegroup]
FROM [dbo].[ExistingTable]