1.创建resset2数据库,将Company_report20221019数据库中所有表复制到resset2数据库中
CREATE DATABASE resset2
ON
PRIMARY
(
NAME='resset2_data1',
FILENAME='D:\database\resset2_data1.mdf',
SIZE=20MB,
MAXSIZE=UNLIMITED,
FILEGROWTH=10%
),
(
NAME='resset2_Log1',
FILENAME='D:\database\resset2_log1.ldf',
SIZE=50MB,
MAXSIZE=100MB,
FILEGROWTH=10MB
);
GO
---定义两个变量,一个是存储表名称的;一个是存储插入语句的
declare @tablename varchar(50),@SQL varchar(500)
---创建游标
declare RessetTest cursor for
---查看数据库中的表名
select name from sysobjects where xtype='u'
---打开游标
open RessetTest
---逐行读取游标中的数据存储到@tablename中
fetch next from RessetTest into @tablename
---查看每一条数据读取是否成功,为0时成功,-1时失败
while @@fetch_status=0
---循环执行@sql中的语句 每次从fetch next from RessetTest into @Tablename中读取一行
begin
SET @Sql='select * into resset2.dbo.'+@TableName +' from [Company_report20221019].[dbo].'+@TableName
Exec (@Sql)
fetch next from RessetTest into @Tablename
end
close RessetTest
deallocate RessetTest