Use Test
If object_id('sp_MoveTable') Is Not Null
Drop Proc sp_MoveTable
Go
/*移动表数据到别的文件组 ok_008 2008-12-29*/
Create Proc sp_MoveTable
(
@objectname sysname,
@NewFileGroup sysname=null
)
As
Set Nocount On
Declare @objectid int
Select @objectid=object_id,@objectname=name
From sys.objects As a
Where name=@objectname
And Type='U'
And is_ms_shipped=0
And Not Exists(Select 1
From sys.extended_properties
Where major_id=a.object_id And
minor_id = 0 and
class = 1 and
name = N'microsoft_database_tools_support'
)
If @objectid Is Null
Begin
Raiserror 50001 N'无效的表名!'
Return
End
If filegroup_id(@NewFileGroup) Is Null And @NewFileGroup>''
Begin
Raiserror 50001 N'错误的文件组!'
Return
End
IF @NewFileGroup Is Null
Select @NewFileGroup=name From sys.filegroups Where is_default=1
If Exists(Select 1 From sys.indexes As a Inner Join sys.filegroups As b On b.data_space_id=a.data_space_id Where a.object_id=@objectid And b.name=@NewFileGroup And (a.type=0 Or is_primary_key=1))
Begin
Print N'表'+@objectname+N'已在文件组'+@NewFileGroup+N' .不需要移动! '
Return
End
Declare @sql nvarchar(4000),
@Enter nvarchar(20),
@PrimaryKey sysname
Select @sql='',@Enter=Char(13)+Char(10)
--删除主键、外键、索引
Select @sql=@sql+'Alter Table '+Quotename(object_name(a.parent_object_id))+' Drop Constraint '+Quotename(a.name)+@Enter
From sys.Foreign_keys As a
Where a.referenced_object_id=@objectid
Select @sql=@sql+
Case When b.object_id Is Not Null Then 'Alter Table '+Quotename(@objectname)+' Drop Constraint '+Quotename(a.name)+
Case b.Type When 'PK' Then ' With (Move To '+Quotename(@NewFileGroup)+')' Else '' End
Else 'Drop Index '+Quotename(a.name)+'.'+Quotename(@objectname)
End +@Enter
From sys.indexes As a
Left Outer Join sys.objects As b On b.parent_object_id=a.object_id And b.Type In('PK','UQ') And b.name=a.name
Where a.object_id=@objectid And a.name Is Not Null
If Not Exists(Select * From sys.indexes Where object_id=@objectid And is_primary_key=1)
Begin
Set @PrimaryKey='ID'+Replace(Newid(),'-','')
--创建主键(在表没有主键的情况)
Set @sql=@sql+'Alter Table '+Quotename(@objectname)+' Add '+@PrimaryKey +' uniqueidentifier Not Null ,Constraint DF_'+@objectname+'_'+@PrimaryKey+' Default(newid()) For '+@PrimaryKey+''+
',Constraint PK_'+@objectname+'_'+@PrimaryKey+' Primary Key ('+@PrimaryKey+' Asc)'+@Enter
--删除主键
Set @sql=@sql+'Alter Table '+Quotename(@objectname)+' Drop Constraint PK_'+@objectname+'_'+@PrimaryKey +' With (Move To '+Quotename(@NewFileGroup)+')'+@Enter
Set @sql=@sql+'Alter Table '+Quotename(@objectname)+' Drop Constraint DF_'+@objectname+'_'+@PrimaryKey +@Enter
Set @sql=@sql+'Alter Table '+Quotename(@objectname)+' Drop Column '+@PrimaryKey +@Enter
End
--创建主键、外键、索引
Select @sql=@sql+
Case When b.object_id Is Not Null Then 'Alter Table '+Quotename(@objectname)+' Add Constraint '+Quotename(a.name)+
Case a.is_primary_key When 1 Then ' Primary Key ' Else 'Unique ' End+'('+c.x+')'
Else
'Create Index '+Case a.is_unique When 1 Then 'Unique ' Else '' End+
Case a.type When 1 Then 'Clustered ' Else '' End +
Quotename(a.name)+' On '+Quotename(@objectname)+'('+c.x+')'+ Isnull(' Include('+d.x+')','')
End +@Enter
From sys.indexes As a
Left Outer Join sys.objects As b On b.parent_object_id=a.object_id And b.Type In('PK','UQ') And b.name=a.name
Outer Apply(Select x=Stuff((Select ','+Quotename(y.name)+Case x.is_descending_key When 1 Then ' Desc' Else ' Asc' End
From sys.index_columns As x
Inner Join sys.columns As y On y.object_id=x.object_id And x.column_id=y.column_id
Where x.object_id=a.object_id And x.index_id=a.index_id And x.is_included_column=0
For Xml Path('')
),1,1,'')
) As c
Outer Apply(Select x=Stuff((Select ','+Quotename(y.name)
From sys.index_columns As x
Inner Join sys.columns As y On y.object_id=x.object_id And x.column_id=y.column_id
Where x.object_id=a.object_id And x.index_id=a.index_id And x.is_included_column=1
For Xml Path('')
),1,1,'')
) As d
Where a.object_id=@objectid And a.name Is Not Null
Select @sql=@sql+'Alter Table '+Quotename(object_name(a.parent_object_id))+' Add Constraint '+Quotename(a.name)
+' Foreign Key ('+b.x+') References '+Quotename(@objectname)+'('+c.x+')'+@Enter
From sys.Foreign_keys As a
Outer Apply(Select x=Stuff((Select ','+Quotename(y.name )
From sys.Foreign_key_columns As x
Inner Join sys.columns As y On y.object_id=x.parent_object_id And y.column_id=x.parent_column_id
Where x.constraint_object_id=a.object_id
For Xml Path('')
),1,1,'')
) As b
Outer Apply(Select x=Stuff((Select ','+Quotename(y.name )
From sys.Foreign_key_columns As x
Inner Join sys.columns As y On y.object_id=x.referenced_object_id And y.column_id=x.referenced_column_id
Where x.constraint_object_id=a.object_id
For Xml Path('')
),1,1,'')
) As c
Where a.referenced_object_id=@objectid
--执行脚本
Begin Try
Begin Tran
Exec(@sql)
Commit Tran
Print N'表'+@objectname+N'数据移动到到文件组'+@NewFileGroup+N' .成功! '
End Try
Begin Catch
Declare @Error nvarchar(1024)
Set @Error=ERROR_MESSAGE()
Raiserror 50001 @Error
Print N'表'+@objectname+N'数据移动到到文件组'+@NewFileGroup+N' .失败! '
Rollback Tran
End Catch
Go
Exec sp_MoveTable A ,'MyFileGroup2'
--Select * From sys.filegroups
--Select * From sys.indexes
/*
If object_id('test2') Is Not Null
Drop Table test2
If object_id('test1') Is Not Null
Drop Table test1
Go
Create Table test1
(
id int Identity(1,1) Not Null ,
x nvarchar(50),
Constraint PK_test1_id Primary Key(id Asc)
)
Create nonClustered Index IX_test1_x On Test1(x Asc)
Create Table test2
(
id int Identity(1,1) Not Null,
test1id int not null,
x nvarchar(50),
Constraint PK_test2_id Primary Key(id Asc),
Constraint FK_test2_test1id Foreign Key (test1id) References Test1(id)
)
*/
If object_id('sp_MoveTable') Is Not Null
Drop Proc sp_MoveTable
Go
/*移动表数据到别的文件组 ok_008 2008-12-29*/
Create Proc sp_MoveTable
(
@objectname sysname,
@NewFileGroup sysname=null
)
As
Set Nocount On
Declare @objectid int
Select @objectid=object_id,@objectname=name
From sys.objects As a
Where name=@objectname
And Type='U'
And is_ms_shipped=0
And Not Exists(Select 1
From sys.extended_properties
Where major_id=a.object_id And
minor_id = 0 and
class = 1 and
name = N'microsoft_database_tools_support'
)
If @objectid Is Null
Begin
Raiserror 50001 N'无效的表名!'
Return
End
If filegroup_id(@NewFileGroup) Is Null And @NewFileGroup>''
Begin
Raiserror 50001 N'错误的文件组!'
Return
End
IF @NewFileGroup Is Null
Select @NewFileGroup=name From sys.filegroups Where is_default=1
If Exists(Select 1 From sys.indexes As a Inner Join sys.filegroups As b On b.data_space_id=a.data_space_id Where a.object_id=@objectid And b.name=@NewFileGroup And (a.type=0 Or is_primary_key=1))
Begin
Print N'表'+@objectname+N'已在文件组'+@NewFileGroup+N' .不需要移动! '
Return
End
Declare @sql nvarchar(4000),
@Enter nvarchar(20),
@PrimaryKey sysname
Select @sql='',@Enter=Char(13)+Char(10)
--删除主键、外键、索引
Select @sql=@sql+'Alter Table '+Quotename(object_name(a.parent_object_id))+' Drop Constraint '+Quotename(a.name)+@Enter
From sys.Foreign_keys As a
Where a.referenced_object_id=@objectid
Select @sql=@sql+
Case When b.object_id Is Not Null Then 'Alter Table '+Quotename(@objectname)+' Drop Constraint '+Quotename(a.name)+
Case b.Type When 'PK' Then ' With (Move To '+Quotename(@NewFileGroup)+')' Else '' End
Else 'Drop Index '+Quotename(a.name)+'.'+Quotename(@objectname)
End +@Enter
From sys.indexes As a
Left Outer Join sys.objects As b On b.parent_object_id=a.object_id And b.Type In('PK','UQ') And b.name=a.name
Where a.object_id=@objectid And a.name Is Not Null
If Not Exists(Select * From sys.indexes Where object_id=@objectid And is_primary_key=1)
Begin
Set @PrimaryKey='ID'+Replace(Newid(),'-','')
--创建主键(在表没有主键的情况)
Set @sql=@sql+'Alter Table '+Quotename(@objectname)+' Add '+@PrimaryKey +' uniqueidentifier Not Null ,Constraint DF_'+@objectname+'_'+@PrimaryKey+' Default(newid()) For '+@PrimaryKey+''+
',Constraint PK_'+@objectname+'_'+@PrimaryKey+' Primary Key ('+@PrimaryKey+' Asc)'+@Enter
--删除主键
Set @sql=@sql+'Alter Table '+Quotename(@objectname)+' Drop Constraint PK_'+@objectname+'_'+@PrimaryKey +' With (Move To '+Quotename(@NewFileGroup)+')'+@Enter
Set @sql=@sql+'Alter Table '+Quotename(@objectname)+' Drop Constraint DF_'+@objectname+'_'+@PrimaryKey +@Enter
Set @sql=@sql+'Alter Table '+Quotename(@objectname)+' Drop Column '+@PrimaryKey +@Enter
End
--创建主键、外键、索引
Select @sql=@sql+
Case When b.object_id Is Not Null Then 'Alter Table '+Quotename(@objectname)+' Add Constraint '+Quotename(a.name)+
Case a.is_primary_key When 1 Then ' Primary Key ' Else 'Unique ' End+'('+c.x+')'
Else
'Create Index '+Case a.is_unique When 1 Then 'Unique ' Else '' End+
Case a.type When 1 Then 'Clustered ' Else '' End +
Quotename(a.name)+' On '+Quotename(@objectname)+'('+c.x+')'+ Isnull(' Include('+d.x+')','')
End +@Enter
From sys.indexes As a
Left Outer Join sys.objects As b On b.parent_object_id=a.object_id And b.Type In('PK','UQ') And b.name=a.name
Outer Apply(Select x=Stuff((Select ','+Quotename(y.name)+Case x.is_descending_key When 1 Then ' Desc' Else ' Asc' End
From sys.index_columns As x
Inner Join sys.columns As y On y.object_id=x.object_id And x.column_id=y.column_id
Where x.object_id=a.object_id And x.index_id=a.index_id And x.is_included_column=0
For Xml Path('')
),1,1,'')
) As c
Outer Apply(Select x=Stuff((Select ','+Quotename(y.name)
From sys.index_columns As x
Inner Join sys.columns As y On y.object_id=x.object_id And x.column_id=y.column_id
Where x.object_id=a.object_id And x.index_id=a.index_id And x.is_included_column=1
For Xml Path('')
),1,1,'')
) As d
Where a.object_id=@objectid And a.name Is Not Null
Select @sql=@sql+'Alter Table '+Quotename(object_name(a.parent_object_id))+' Add Constraint '+Quotename(a.name)
+' Foreign Key ('+b.x+') References '+Quotename(@objectname)+'('+c.x+')'+@Enter
From sys.Foreign_keys As a
Outer Apply(Select x=Stuff((Select ','+Quotename(y.name )
From sys.Foreign_key_columns As x
Inner Join sys.columns As y On y.object_id=x.parent_object_id And y.column_id=x.parent_column_id
Where x.constraint_object_id=a.object_id
For Xml Path('')
),1,1,'')
) As b
Outer Apply(Select x=Stuff((Select ','+Quotename(y.name )
From sys.Foreign_key_columns As x
Inner Join sys.columns As y On y.object_id=x.referenced_object_id And y.column_id=x.referenced_column_id
Where x.constraint_object_id=a.object_id
For Xml Path('')
),1,1,'')
) As c
Where a.referenced_object_id=@objectid
--执行脚本
Begin Try
Begin Tran
Exec(@sql)
Commit Tran
Print N'表'+@objectname+N'数据移动到到文件组'+@NewFileGroup+N' .成功! '
End Try
Begin Catch
Declare @Error nvarchar(1024)
Set @Error=ERROR_MESSAGE()
Raiserror 50001 @Error
Print N'表'+@objectname+N'数据移动到到文件组'+@NewFileGroup+N' .失败! '
Rollback Tran
End Catch
Go
Exec sp_MoveTable A ,'MyFileGroup2'
--Select * From sys.filegroups
--Select * From sys.indexes
/*
If object_id('test2') Is Not Null
Drop Table test2
If object_id('test1') Is Not Null
Drop Table test1
Go
Create Table test1
(
id int Identity(1,1) Not Null ,
x nvarchar(50),
Constraint PK_test1_id Primary Key(id Asc)
)
Create nonClustered Index IX_test1_x On Test1(x Asc)
Create Table test2
(
id int Identity(1,1) Not Null,
test1id int not null,
x nvarchar(50),
Constraint PK_test2_id Primary Key(id Asc),
Constraint FK_test2_test1id Foreign Key (test1id) References Test1(id)
)
*/