表A:col1 col2
a    0
b    0
c    0表B
col1需求:把表A的col1插入表B的col1,同时A的col2都更新为'1',而且要有数据更新的顺序,比如把col1值为a插入到表B中,这时值为a对应的col2才更新为1,如果是insert into B select col1 from A  update a set col2 = 1,这样就没有意义了。由于表A与表B是在不同的服务器上,所以必须考虑到执行的效率问题,最好是类似与 insert into B select col1 from A,这种比较快捷的操作。谢谢各位大虾

解决方案 »

  1.   

    create trigger insert_b on b
    alter insert
    as
    update a
    set col2=1
    from inserted i
    where
    i.col1=a.col1
      

  2.   

    create trigger insert_b on b
    alter insert
    as
    update a
    set col2=1
    from inserted i
    where
    i.col1=a.col1
     
    if not object_id('a') is null
    drop table a
    Go
    Create table a([col1] nvarchar(1),[col2] nvarchar(1))
    Insert a
    select N'a',N'0' union all
    select N'b',N'0' union all
    select N'c',N'0'
    Go
    create table b(Col1 nvarchar(1))
    go
    create trigger insert_b on b
    after insert
    as
    update a
    set col2=1
    from inserted i
    where
    i.col1=a.col1
    goinsert b select col1 from aselect * from a
    col1 col2
    ---- ----
    a    1
    b    1
    c    1(3 個資料列受到影響)
      

  3.   

    按條件更新就行了。。
    如:
    insert b select col1 from a where col1='a'
    update a set col2=1 where col1='a'
      

  4.   

    哎,也怪我说不清楚,我现在是得到一个数据表B(短信接口),我发一条数据给B,他就会发短信出去,但是表B的数据同时被他的服务器删除。我这里几乎没有对表B操作的权利。
      

  5.   

    不用操作B
    只需要操作A更新
    或用一個表存儲發過的短信
    以這個作為條件更新A
      

  6.   

    难道非要用游标操作表A,插入表B,再更新表A