1.查看文件组 sql语句

SELECT Data_located_on_filegroup = fg.groupname,
Table_name = obj.name
FROM sysfilegroups fg, sysindexes idx, sysobjects obj
WHERE idx.id = obj.id
AND obj.type in ('S ','U ')

AND idx.groupid = fg.groupid
 and  obj.name like '%%'       --表名
ORDER BY fg.groupname

2.更改文件组

说明: 在给表 创建索引的时候,可以指定索引在 不同的文件组, 这样 使 表 迁移到 另外的文件组

步骤1: 给表 创建索引 指定 文件组

步骤2: 将表的 索引删除(是表结构复原)

3.使用案例

CREATE procedure [olap].[usp_SwitchOutFact_Partition]
())
as
begin
BEGIN TRANSACTION
)
)

 ),   + day(@date))
 ), ,  ,  , @date)))

 /*
 --逻辑描述:
 1.先检查 临时表是不是存在, 存在则删除后 使用select into 新建表
 2.select into 创建的表 默认存放在 PRIMARY 文件组, 我们要更改 文件组
 3.给临时表创建索引的时候, 可以更改文件组
 4.使表结构一致(包括索引) 所以删除刚创建的索引,此时完成 表的 文件组的更改
 5.进行表 压缩 ,指定 WITH(DATA_COMPRESSION = PAGE
 6. SWITCH PARTITION
 */

 from sys.tables where object_id=object_id('[olap].[staging_'+@tablename+']'))
    exec('drop table [olap].[staging_'+@tablename+']')

exec ('select top(0) * into olap.staging_'+@tablename+' from olap.'+@tablename)

exec('CREATE CLUSTERED INDEX PK_staging_'+@tablename+' ON [olap].[staging_'+@tablename+']([PartitionRangeKey]) WITH(ONLINE=ON) ON [FactFG]')
exec('DROP INDEX PK_staging_'+@tablename+' ON [olap].[staging_'+@tablename+']')

exec('ALTER TABLE olap.staging_' + @tablename + ' REBUILD WITH(DATA_COMPRESSION = PAGE )')
exec('ALTER TABLE [olap].[staging_'+@tablename+']  WITH CHECK ADD  CONSTRAINT [chk_staging_'+@tablename+'] CHECK  ([PartitionRangeKey]>=' + @RangeKey1 + ' AND [PartitionRangeKey]<' + @RangeKey2 + ' AND [PartitionRangeKey] IS NOT NULL)')

exec('ALTER TABLE [olap].[staging_'+@tablename+'] CHECK CONSTRAINT [chk_staging_'+@tablename+']')

exec('ALTER TABLE [olap].['+@tablename+'] SWITCH PARTITION $PARTITION.[pf_'+@tablename+'] ('+@RangeKey1+')  TO [olap].[staging_'+@tablename+']')

COMMIT TRANSACTION
end

调用语句

declare @date datetime
set @date = convert(datetime,'2016-09-23')
print @date
) ),   + day(@date))
print @RangeKey1
exec [olap].[usp_SwitchOutFact_Partition] @date,'Fact_XM_XMBJXX'

查看表分区情况

SELECT $PARTITION.[pf_Fact_XM_XMBJXX](PartitionRangeKey) AS Partition,
COUNT(*) AS [COUNT] FROM olap.Fact_XM_XMBJXX
GROUP BY $PARTITION.[pf_Fact_XM_XMBJXX](PartitionRangeKey)
ORDER BY Partition ;
05-02 18:23