如何将整个数据库对象(包括(函数,触发器,过程等))导出到另一个新创建的空数据库。我不想通过SQL环境中的导出或导入或生成脚本来完成。我只想通过BIDS环境中的SSIS包装方式做我需要做的事情?....建议我最好的方式
答案 0 :(得分:0)
这是一个开始。这是我目前的项目。以下脚本用于复制,然后为我们意外删除的任何表恢复架构信息。此查询将触发器,约束,索引(包括跨多个列的索引)和权限复制到temptable中作为可执行代码。因此,它查看@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
*/