alwaysVBNET
alwaysVBNET

Reputation: 3310

Create table under a specific FileGroup WHERE 1=2

I have an WinForms application which creates tables dynamically based on a given table such as:

SELECT * INTO TempTable FROM MyTable WHERE 1=2

I want those Temp tables to be created under a specific filegroup though using the above syntax.

The syntax to create the table under a filegroup is:

CREATE TABLE [dbo].[TempTable](

            [RECORDID] [numeric](10, 0) NOT NULL,
            --etc etc

) ON [TempFileGroup] TEXTIMAGE_ON [TempFileGroup]

Is it possible to use my syntax above to create the table under the specific filegroup?

Upvotes: 1

Views: 1480

Answers (2)

Lukasz Szozda
Lukasz Szozda

Reputation: 175716

I want those Temp tables to be created under a specific filegroup though using the above syntax

From SQL Server 2017+ you could use ON filegroup syntax.

INTO clause

SELECT…INTO creates a new table in the default filegroup and inserts the resulting rows from the query into it.

[ INTO new_table ]
[ ON filegroup]

filegroup

Specifies the name of the filegroup in which new table will be created. The filegroup specified should exist on the database else the SQL Server engine throws an error. This option is only supported beginning with SQL Server 2017.

Example from MSDN:

Creating a new table as a copy of another table and loading it a specified filegroup

ALTER DATABASE [AdventureWorksDW2016] ADD FILEGROUP FG2;
ALTER DATABASE [AdventureWorksDW2016]
ADD FILE
(
NAME='FG2_Data',
FILENAME = '/var/opt/mssql/data/AdventureWorksDW2016_Data1.mdf'
)
TO FILEGROUP FG2;
GO
SELECT *  INTO [dbo].[FactResellerSalesXL] ON FG2 from [dbo].[FactResellerSales]

Upvotes: 1

Shaneis
Shaneis

Reputation: 1085

BOL states that

SELECT…INTO creates a new table in the default filegroup

(Emphasis added)

so unless you can find the table that was created and alter it with the argument syntax below, the newly created table is going to get created in the default filegroup.

MOVE TO { partition_scheme_name ( column_name [ 1, ... n] ) | filegroup | "default" }

Applies to: SQL Server 2008 through SQL Server 2016, SQL Database V12.

Specifies a location to move the data rows currently in the leaf level of the clustered index. The table is moved to the new location. This option applies only to constraints that create a clustered index.

Upvotes: 0

Related Questions