表A
id    pid    description
100    1        aaa
101    1        bbb
102    2        ccc
...
现在想把表A的pid=1的再copy一份到原表,其它不变,id为自增型。
要求用while 去做。因为表A有触发器要运行。
有其它简单解决办法更好。
在此先谢谢各位路过的朋友。

解决方案 »

  1.   

    1、修改触发器使之支持批量insert,然后
    insert into 表 select * from 表A where pid=12、用游标
    declare @id int,@pid int,@desc varchar(20)
    declare tc cursor for select * from 表A where pid=1open tcfetch next from tc into @id,@pid,@descwhile @@fetch_status=0
    begin
        insert into 表 values(@id,@pid,@desc)    fetch next from tc into @id,@pid,@desc
    endclose tc
    deallocate tc
      

  2.   

    1、修改触发器使之支持批量insert,然后
    insert into 表A select * from 表A where pid=1
    2、用游标
    declare @pid int,@desc varchar(20)
    declare tc cursor for select pid,description from 表A where pid=1open tcfetch next from tc into @pid,@descwhile @@fetch_status=0
    begin
        insert into 表A values(@pid,@desc)    fetch next from tc into @pid,@desc
    endclose tc
    deallocate tc
      

  3.   

    想请问下,怎么样使触发器支持批量insert
    能简单举个例吗?
      

  4.   


    create table A(id int identity(100,1), pid int, [description] varchar(10))
    insert A(pid, [description]) select 1, 'aaa'
    insert A(pid, [description]) select 1, 'bbb'
    insert A(pid, [description]) select 2, 'ccc'insert A(pid, [description]) select pid, [description] from A where pid=1select * from A--result
    id          pid         description 
    ----------- ----------- ----------- 
    100         1           aaa
    101         1           bbb
    102         2           ccc
    103         1           aaa
    104         1           bbb(5 row(s) affected)
      

  5.   

    create table t1(id int identity( 1,1),name varchar(10))
    create table t2(id int identity(10,1),t1_id int,code varchar(10))
    gocreate trigger trg_t1 on t1
    for insert
    as
        insert into t2 select id,'' from inserted
    goinsert into t1 select 'AAAA' union select 'BBBB'select * from t1
    select * from t2
    godrop trigger trg_t1
    drop table t1,t2
    go
      

  6.   

    Inserted Scan 逻辑运算符和物理运算符扫描在触发器内插入的表。
    insert into t2 select id,'' from inserted
    inserted  -- 请问这个是什么东西啊?
      

  7.   

    deleted 和 inserted 是逻辑(概念)表。这些表在结构上类似于定义触发器的表(也就是在其中尝试用户操作的表);这些表用于保存用户操作可能更改的行的旧值或新值。