すべてのテーブルがDBOであると仮定すると、これはうまくいくはずです。あなたはアイデンティティフィールドを持っている場合に 、私のコメント
-- select only tables that have the same number of columns
declare tbls cursor for with tblsA as (
select t.name, count(*) as colCount
from DatabaseA.sys.tables t
inner join DatabaseA.sys.columns c on c.object_id = t.object_id
group by t.name
)
, tblsB as (
select t.name, count(*) as colCount
from DatabaseB.sys.tables t
inner join DatabaseB.sys.columns c on c.object_id = t.object_id
group by t.name
)
select tblsA.name as TableName
from tblsA
inner join tblsB on tblsB.name = tblsA.name and tblsB.colCount = tblsA.colCount
declare @tblName varchar(100)
declare @sqlText nvarchar(max)
open tbls
fetch next from tbls into @tblName
while @@FETCH_STATUS = 0
begin
print 'Inserting into ' + @tblName
--un-comment if you don't have identity fields.
--you will need more code here if some table have identity field and some don't
--set @sqlText = 'SET IDENTITY_INSERT DatabaseB..' + @tblName + ' ON'
--exec(@sqlText)
set @sqlText = 'insert into DatabaseB..' + @tblName + ' select * from DatabaseA..' + @tblName
exec(@sqlText)
fetch next from tbls into @tblName
end
close tbls
deallocate tbls
だからあなたの質問は何であり、あなたがこれまで何をしているを参照してください? –
私は試してみました: "Select * into DatabaseB.dbo.table1 DatabaseA.dbo.table1から....それは動作しますが、私はすべてのテーブルをループしてデータベースAの各テーブルの内容をコピーできるスクリプトが必要ですデータベースBの対応する各テーブル – DoreenSly
@DoreenSly、まだ答えを待っていますか?DBOスキーマ内のすべてのテーブルですか?IDフィールドがありますか? – FLICKER