有两个数据库A和B
其中:
A数据库有表tA1和tA2:
tA1:
----------
id     title
50      none
51      none
52      none
53      none
54      none
----------tA2:
----------
id   title    page_id
[ 空表 ]
----------B数据库中有表tB:
tB1:
----------
id   title
1      a
2      b
3      c
----------我想实现的效果是这样的,从B数据库里的表tB1中copy复制到A数据库的表中
在A数据库的
表tA1:
----------
id    title
1      a
2      b
3      c
4      none
5      none
----------表tA2:
----------
id   title    page_id
1      a        51 
2      b        52
3      c        53
4      none     none
5      none     none
----------我遇到的问题是:page_id该怎么自增啊?
谢谢大家

解决方案 »

  1.   

    表tA2: 
    ---------- 
    id  title    page_id 
    1      a        51 
    2      b        52 
    3      c        53 
    4      none    none 
    5      none    none -->是否应该是:表tA2: 
    ---------- 
    id  title    page_id 
    1      a        50 
    2      b        51 
    3      c        52 
    4      none    none 
    5      none    none 
      

  2.   

    试试:
    (语句未测试,不同库需要先链接,链接的语句我放在最后了.你自己对tb表带上对应的前缀,我这里偷懒了.)
    insert into ta2
    select m.px , isnull(n.title,m.title) title , isnull(m.id,'none') page_id from
    (select t.* , px = (select count(1) from tA1 where id < t.id) + 1 from tA1 t) m
    left join tb n
    on m.px = n.idupdate ta1
    set id = m.px,
        title = n.title        
    from ta1 t , (select t.* , px = (select count(1) from tA1 where id < t.id) + 1 from tA1 t) m , tb n
    where t.id = m.id 不同服务器数据库之间的数据操作--创建链接服务器 
    exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' 
    exec sp_addlinkedsrvlogin  'ITSV ', 'false ',null, '用户名 ', '密码 ' --查询示例 
    select * from ITSV.数据库名.dbo.表名 --导入示例 
    select * into 表 from ITSV.数据库名.dbo.表名 --以后不再使用时删除链接服务器 
    exec sp_dropserver  'ITSV ', 'droplogins ' --连接远程/局域网数据(openrowset/openquery/opendatasource) 
    --1、openrowset --查询示例 
    select * from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名) --生成本地表 
    select * into 表 from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名) --把本地表导入远程表 
    insert openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名) 
    select *from 本地表 --更新本地表 
    update b 
    set b.列A=a.列A 
     from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)as a inner join 本地表 b 
    on a.column1=b.column1 --openquery用法需要创建一个连接 --首先创建一个连接创建链接服务器 
    exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' 
    --查询 
    select * 
    FROM openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ') 
    --把本地表导入远程表 
    insert openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ') 
    select * from 本地表 
    --更新本地表 
    update b 
    set b.列B=a.列B 
    FROM openquery(ITSV,  'SELECT * FROM 数据库.dbo.表名 ') as a  
    inner join 本地表 b on a.列A=b.列A --3、opendatasource/openrowset 
    SELECT   * 
    FROM   opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ' ).test.dbo.roy_ta 
    --把本地表导入远程表 
    insert opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ').数据库.dbo.表名 
    select * from 本地表  
      

  3.   

    dawugui:
    我改动了一下tA2表,增加了3列,c 、 d 、e
    tA2:
    ----------
    id  title    page_id  c  d   e
    [ 空表 ]
    ---------- 那现在该如何写?我看着头大了,我不懂数据库,不好意思,见笑
      

  4.   

    这样行不?--A数据库中
    if object_id('tA1') is null
    create table tA1 (id varchar(4) ,title varchar(10))
    delete tA1
    insert into ta1 select '50','none'
    insert into ta1 select '51','none'
    insert into ta1 select '52','none'
    insert into ta1 select '53','none'
    insert into ta1 select '54','none'if object_id('tA2') is null
    create table tA2 (id varchar(4) ,title varchar(10),page_id varchar(10))
    delete tA2
    --创建连接到B的LINK
    exec   sp_addlinkedserver     'LenkToB', ' ', 'SQLOLEDB ', 'B数据库IP'--制定連接對象,與間接名稱
    exec   sp_addlinkedsrvlogin   'LenkToB', 'false ',null, 'user1', 'passwd'--連接用戶/密碼
    go
    EXEC   sp_serveroption   'LenkToB','rpc out','TRUE'   
    go--B数据库中
    if object_id('tB1') is null
    create table tB1 (id varchar(4) ,title varchar(10))
    delete tB1
    insert into tB1 select '1','a'
    insert into tB1 select '2','b'
    insert into tB1 select '3','c'
    --在B中开启外连接
    exec sp_configure 'show advanced options',1   --開啓高級設置
    reconfigure
    exec sp_configure 'Ad Hoc Distributed Queries',1--允許openrowset opendatasource訪問
    reconfigure
    exec sp_configure 'show advanced options',0   --關閉高級設置
    reconfigure---以上预先工作做完后
    --在A数据库中执行下面语句
    --填充A1
    delete tA2
    insert into tA2
    select t.px,isnull(t1.title,'none') as btitle,(case when t1.id is null then 'none' else  t.id end ) as page_id-- ,t.title,isnull(t1.id,'none') as bid 
    from 
    (
    select t.* , px = (select count(1) from tA1 where id < t.id) + 1 from tA1 t
    ) as t
    left join LenkToB.B数据库名.dbo.tB1 t1 on t1.id=t.px
    --填充B1
    delete tA1
    insert into tA1
    select t.px,isnull(t1.title,'none') as btitle--,(case when t1.id is null then 'none' else  t.id end ) as page_id,t.title,isnull(t1.id,'none') as bid 
    from 
    (
    select t.* , px = (select count(1) from tA1 where id < t.id) + 1 from tA1 t
    ) as t
    left join LenkToB.B数据库名.dbo.tB1 t1 on t1.id=t.px
      

  5.   

    要把none补上数据还是让page_id变成自增列?
      

  6.   

    看来得等你把需求说完全了再来.最好是结了此帖,然后再开帖,把需求说清楚.最好给出完整的表结构,测试数据,计算方法和正确结果.发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281