如何将包括(函数、触发器、过程等)在内的整个数据库对象导出到另一个新创建的空数据库。我不想通过 Export 或 Import 或 Generate Scripts in SQL Environment 来做。我只想通过 SSIS 包在 BIDS 环境中做我需要做什么?....建议我最好的方法
问问题
1312 次
1 回答
0
这是你的一个开始。这是我目前的项目。以下脚本用于复制然后恢复我们意外删除的任何表的架构信息。此查询将触发器、约束、索引(包括跨多列的索引)和权限复制到一个临时表中作为可执行代码。所以它会查看@fromTable 的所有架构信息,并为@Totable 编写必要的sql 语句。底部有一个注释掉的部分,它执行存储在临时表中的所有查询。
这里唯一缺少的一项是@fromtable 上存在源自另一个表的外键约束。
我们项目的一点小知识,您不能将具有相同名称的约束、触发器、索引添加到同一个数据库中。您将不得不为此更改脚本。我还建议将查询分解成更小的块,这样您就可以使它们成为单独的 SSIS 脚本。
祝你好运!(我在这里给你我最好的东西)
Declare @Fromtable Varchar(255), @ToTable Varchar(255),@indexcount int, @indexcolumnid int,@maxindexcount int
set @Fromtable = 'tblname'
set @ToTable = 'tblname'
if object_id('tblTemp') is not null
Drop table tblTemp
Create table tblTemp (ID int identity, queries varchar(max))
set @indexcount = (select min(si.index_ID) from sys.indexes si inner join sys.tables st
on st.object_id = si.object_id where st.name = @Fromtable
and si.name not like 'PK%')
set @maxindexcount = (select max(si.index_ID) from sys.indexes si inner join sys.tables st
on st.object_id = si.object_id where st.name = @Fromtable
and si.name not like 'PK%')
while (@indexcount <= @maxindexcount)
BEGIN
IF (( select count(si.index_ID) from sys.indexes si inner join sys.tables st
on st.object_id = si.object_id inner join sys.columns col
on si.object_id = col.object_id
inner join sys.index_columns ic
on ic.object_id = si.object_id
and ic.index_id = si.index_id
and col.column_id = ic.column_id where st.name = @Fromtable
and si.name not like 'PK%' and si.index_id = @indexcount) = 0)
BEGIN
SET @indexcount += 1
end
else IF (( select count(si.index_ID) from sys.indexes si
inner join sys.tables st
on st.object_id = si.object_id
inner join sys.columns col
on si.object_id = col.object_id
inner join sys.index_columns ic
on ic.object_id = si.object_id
and ic.index_id = si.index_id
and col.column_id = ic.column_id
where st.name = @Fromtable
and si.name not like 'PK%'
and si.index_id = @indexcount
) = 1)
BEGIN
insert into tblTemp(queries)
select 'CREATE ' + si.type_desc + ' INDEX '
+ si.name + ' ON [Dbo].[' + @ToTable + '] '
+ '(' + col.name + ') '
from sys.indexes si
inner join sys.columns col
on si.object_id = col.object_id
inner join sys.index_columns ic
on ic.object_id = si.object_id
and ic.index_id = si.index_id
and col.column_id = ic.column_id
inner join sys.tables st
on st.object_id = si.object_id
where st.name = @Fromtable
and si.name not like 'PK%'
and si.index_id = @indexcount
SET @indexcount += 1
END
else IF (( select count(si.index_ID) from sys.indexes si inner join sys.tables st
on st.object_id = si.object_id inner join sys.columns col
on si.object_id = col.object_id
inner join sys.index_columns ic
on ic.object_id = si.object_id
and ic.index_id = si.index_id
and col.column_id = ic.column_id where st.name = @Fromtable
and si.name not like 'PK%' and si.index_id = @indexcount) > 1)
Declare @innerloopcounter int, @BuiltString varchar(max)
set @innerloopcounter = 2
BEGIN
SET @indexcolumnid = (SELECT max(ic.index_column_id) from sys.indexes si
inner join sys.columns col
on si.object_id = col.object_id
inner join sys.index_columns ic
on ic.object_id = si.object_id
and ic.index_id = si.index_id
and col.column_id = ic.column_id
inner join sys.tables st
on st.object_id = si.object_id
where st.name = @Fromtable
and si.name not like 'PK%'
and si.index_id = @indexcount)
set @BuiltString = (select 'CREATE ' + si.type_desc + ' INDEX '
+ si.name + ' ON [Dbo].[' + @ToTable + '] '
+ '(' + col.name
from sys.indexes si
inner join sys.columns col
on si.object_id = col.object_id
inner join sys.index_columns ic
on ic.object_id = si.object_id
and ic.index_id = si.index_id
and col.column_id = ic.column_id
inner join sys.tables st
on st.object_id = si.object_id
where st.name = @Fromtable
and si.name not like 'PK%'
and si.index_id = @indexcount
and ic.index_column_id = 1)
while (@innerloopcounter <= @indexcolumnid)
Begin
set @BuiltString = @BuiltString + ', ' + (select col.name
from sys.indexes si
inner join sys.columns col
on si.object_id = col.object_id
inner join sys.index_columns ic
on ic.object_id = si.object_id
and ic.index_id = si.index_id
and col.column_id = ic.column_id
inner join sys.tables st
on st.object_id = si.object_id
where st.name = @Fromtable
and si.name not like 'PK%'
and si.index_id = @indexcount
and ic.index_column_id = @innerloopcounter)
set @innerloopcounter += 1
End
set @BuiltString = @BuiltString + ')'
insert into tblTemp(queries)
select @BuiltString
SET @indexcount += 1
end
end
----------------End indexes, start triggers-------------------------------
Declare @name int, @count int, @max int, @query Varchar(max)
Create table tempTriggers(ID int identity , trigger_ID int)
Insert into tempTriggers (trigger_ID)
select tr.object_id
FROM sys.triggers AS tr
INNER JOIN sys.tables AS o ON tr.parent_id = o.object_id
where o.object_id = @name
set @count = 1
set @max = (Select max(ID) from tempTriggers)
set @query = (Select definition from sys.sql_modules where object_id = (select trigger_ID from tempTriggers where ID = @count))
while(@count <= @max)
begin
set @query = replace (@query, @FromTable, @ToTable)
insert into tblTemp(Queries)
values (@query)
set @count = @count + 1
set @query = (Select definition from sys.sql_modules where object_id = (select trigger_ID from tempTriggers where ID = @count))
end
drop table tempTriggers
-------------------------end triggers, start permissions-----------------------------
Insert into tblTemp(Queries)
select 'GRANT ' + dp.permission_name collate latin1_general_cs_as
+ ' ON ' + s.name + '.' + @ToTable + ' TO ' + '[' + dpr.name + ']'
FROM sys.database_permissions AS dp
INNER JOIN sys.objects AS o ON dp.major_id=o.object_id
INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id
INNER JOIN sys.database_principals AS dpr ON dp.grantee_principal_id=dpr.principal_id
WHERE dpr.name NOT IN ('public','guest')
and o.name = @FromTable
------------------------end permissions, start constraints----------------------------------
Insert into tblTemp(queries)
select 'Alter Table [dbo].[' + @ToTable +'] ADD CONSTRAINT '
+ kc.name +' primary KEY ('
+ col.name +')'
from sys.key_constraints kc
inner join sys.columns col
on kc.parent_object_id = col.object_id
inner join sys.tables st
on kc.parent_object_id = st.object_id
where
st.name = @FromTable
and kc.unique_index_id = col.column_id
Insert into tblTemp(queries)
select 'Alter Table [dbo].[' + @ToTable +'] '
+ 'Add CONSTRAINT [' + dc.name + ']'
+ ' DEFAULT ' + dc.definition + ' '
+ 'FOR [' + col.name + ']'
from sys.default_constraints dc
inner join sys.columns col
on dc.parent_object_id = col.object_id
inner join sys.tables st
on dc.parent_object_id = st.object_id
where
st.name = @FromTable
and col.column_id = dc.parent_column_id
Insert into tblTemp(queries)
select 'Alter Table [dbo].[' + @ToTable + '] WITH NOCHECK '
+ 'Add CONSTRAINT [' + chk.name + ']'
+ ' CHECK (' + chk.definition + ') '
from sys.check_constraints chk
inner join sys.columns col
on chk.parent_object_id = col.object_id
inner join sys.tables st
on chk.parent_object_id = st.object_id
where
st.name = @FromTable
and col.column_id = chk.parent_column_id
Insert into tblTemp(queries)
select distinct 'Alter Table [dbo].[' + @ToTable + '] '
+ 'ADD FOREIGN KEY ('+ col.name + ') '
+ 'REFERENCES ' + ft.name + ' ('+ fkcol.name + ')'
from sys.foreign_key_columns fkc
inner join sys.tables st
on fkc.parent_object_id = st.object_id
inner join sys.columns col
on fkc.parent_object_id = col.object_id
inner join sys.tables ft
on fkc.referenced_object_id = ft.object_id
inner join sys.columns fkcol
on ft.object_id = fkcol.object_id
where
st.name = @FromTable
and col.column_id = fkc.parent_column_id
and fkc.referenced_column_id = fkcol.column_id
select * from tblTemp
--end of script--execution phase--------------------------------------------------------------------------
/*
declare @count int, @max int, @query Varchar(max)
set @count = 186
set @max = (Select max(ID) from tblTemp)
set @query = (select queries from tblTemp where ID = @count)
while(@count <= @max)
begin
exec(@query)
set @count += 1
set @query = (select queries from tblTempwhere ID = @count)
end
select * from tblTemp
drop table tblTemp
*/
于 2013-10-08T14:53:06.600 回答