/*移動表資料到別的檔案組 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
endif filegroup_id(@newfilegroup) is null and @newfilegroup>''
begin
raiserror 50001 n'錯誤的檔案組!'
return
endif @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
enddeclare @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(@objectname)+'.'+quotename(a.name)
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
--print @sql
--執行指令碼
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
表檔案組修改
首先 假設有乙個表 t,建立在檔案組 oldfilegroup 上,現在要將表 t移到新的檔案組 newfilegroup上。1 處理主鍵的情形,即聚集索引和主鍵建在相同的字段上 假設有乙個主鍵名稱為 pk t 首先刪除主鍵 alter table tdrop constraint pk t 然後重...
ssis 到別的表查詢臨時變數值
原文 ssis 到別的表查詢臨時變數值 etl過程過,往乙個資料庫表插入資料,插入的值往往需要到另外乙個資料庫讀取。例如下面的客戶跟蹤,需要乙個 專案id 這個id需要到另乙個資料庫的一張表查詢,找到後返回給 客戶跟蹤 任務流。執行sql任務 原專案id 的配置如下圖 sql語句是 if exist...
ssis 到別的表查詢臨時變數值
etl過程過,往乙個資料庫表插入資料,插入的值往往需要到另外乙個資料庫讀取。例如下面的客戶跟蹤,需要乙個 專案id 這個id需要到另乙個資料庫的一張表查詢,找到後返回給 客戶跟蹤 任務流。執行sql任務 原專案id 的配置如下圖 sql語句是 if exists select from dbo.tb...