Create table under a specific FileGroup WHERE 1=2

烈酒焚心 提交于 2019-12-11 13:02:21

问题


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?


回答1:


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]



回答2:


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.



来源:https://stackoverflow.com/questions/36574130/create-table-under-a-specific-filegroup-where-1-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!