高手请进~帮忙解决问题!!!如何获取一个数据库的某个表中的数据,然后插到另外一个库中的某个表中去???

xciennini 2004-08-26 05:46:12
如上所述,由于小妹目前在做数据库的合库工作,需要将以前老的CS结构下的分离的数据库中的数据导入现在的BS结构下的新的数据库,由于结构已经变掉,所以我只能按字段来插入到新的数据库,可是碰到上述问题,不知道该如何做???请各位高手帮忙!!!小妹感激不尽!!!
...全文
138 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xciennini 2004-08-30
  • 打赏
  • 举报
回复
十分感谢各位,现在已经解决了,由于初次写SP,所以有些愚笨,以后还要象各位多多讨教!!!:)
xciennini 2004-08-27
  • 打赏
  • 举报
回复
十分感谢邹建大哥和冒牌经理等兄弟的热心帮助~~~由于现在不能上外网,所以不能及时回复帖子,我已经保存下来了,回去慢慢看,明后天我再请教各位大哥!!!:P
jiangchuandong 2004-08-27
  • 打赏
  • 举报
回复
DTS最简单了,点点就可以了
hxnet 2004-08-27
  • 打赏
  • 举报
回复
insert dept1 select * from dept
首先表结构应该一样。


zjcxc 2004-08-26
  • 打赏
  • 举报
回复
2.你可以尝试用sql的导入/导出向导,定义表之间各字段的对应转换关系
zjcxc 2004-08-26
  • 打赏
  • 举报
回复
--1.试试下面的存储过程

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_copydb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_copydb]
GO


/*--数据库数据复制

将一个数据库中的数据复制到另一个数据库
如果某列在目标数据库中为标识列,将不会被复制

适用范围:数据库结构发生了变化,想将旧数据库进行升级
这样就可以根据新的数据库结构创建一个空库,然后
将旧数据库的所有数据复制到新库中

--邹建 2003.10(引用请保留此信息)--*/

/*--调用示例

exec p_copydb 'bns_aa','bns_new'
exec p_copydb 'acc_五医','acc_演示数据8'
--*/
create proc p_copydb
@o_dbname sysname, --要复制数据的数据库--源数据库
@n_dbname sysname --接收数据的数据库--目标数据库
--@cleardb bit=0 --清空目标数据库
as
declare @sql nvarchar(4000)

--禁用约束/触发器,防止复制时的数据冲突
set @sql='declare #tbc cursor for select name
from '+@n_dbname+'..sysobjects where xtype=''U'' and status>=0'
exec(@sql)

declare @tbname sysname
open #tbc
fetch next from #tbc into @tbname
while @@fetch_status=0
begin
set @sql='alter table '+@n_dbname+'..['+@tbname+'] NOCHECK CONSTRAINT ALL'
exec(@sql)
set @sql='alter table '+@n_dbname+'..['+@tbname+'] disable trigger ALL'
exec(@sql)
fetch next from #tbc into @tbname
end
close #tbc

--复制数据
declare @sql1 varchar(8000)
set @sql='declare #tb cursor for select a.name from '
+@o_dbname+'..sysobjects a inner join '
+@n_dbname+'..sysobjects b on a.name=b.name
where a.xtype=''U'' and b.xtype=''U'''
exec(@sql)
open #tb
fetch next from #tb into @tbname
while @@fetch_status=0
begin
select @sql1=''
,@sql='select @sql1=@sql1+'',[''+a.name+'']'' from(
select name from '+@o_dbname+'..syscolumns where id in
(select id from '+@o_dbname+'..sysobjects where name='''+@tbname+''')
) a inner join (
select name from '+@n_dbname+'..syscolumns where status<>0x80 and id in
(select id from '+@n_dbname+'..sysobjects where name='''+@tbname+''')
) b on a.name=b.name'
exec sp_executesql @sql,N'@sql1 nvarchar(4000) out',@sql1 out

select @sql1=substring(@sql1,2,8000)
exec('insert into '+@n_dbname+'..['+@tbname+']('+@sql1
+') select '+@sql1+' from '+@o_dbname+'..['+@tbname+']')
if @@error<>0
print('insert into '+@n_dbname+'..['+@tbname+']('+@sql1
+') select '+@sql1+' from '+@o_dbname+'..['+@tbname+']')
fetch next from #tb into @tbname
end
close #tb
deallocate #tb

--数据复制完成后启用约束
open #tbc
fetch next from #tbc into @tbname
while @@fetch_status=0
begin
set @sql='alter table '+@n_dbname+'..['+@tbname+'] CHECK CONSTRAINT ALL'
exec(@sql)
set @sql='alter table '+@n_dbname+'..['+@tbname+'] enable trigger ALL'
exec(@sql)
fetch next from #tbc into @tbname
end
close #tbc
deallocate #tbc
go


skyboy0720 2004-08-26
  • 打赏
  • 举报
回复
insert 库2..表2 select 字段1,字段2 from 库1..表1
CSDMN 2004-08-26
  • 打赏
  • 举报
回复
insert 另外一个库..另外一个库某个表(...)
select ... from 一个数据库..一个数据库的某个表

注意两个...是字段列表,要保持个数和次序一致

hisi 2004-08-26
  • 打赏
  • 举报
回复
insert into 目的库.dbo.目的表(字段1,字段2..)
select 源库.dbo.源表(字段1,字段2...)

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧