现在有一个现成的select 语句。
我想把这个select语句查询出的字段 再加上 一个字段 后插入表中。因为很多插入都要用到这个select语句的结果。
所以我想用INSERT  talbeA SELECT 这种形式,
但是不知道怎么写?
或者有没有其他办法。
谢谢。

解决方案 »

  1.   

    insert into B(c1,c2,c3,c4) select c1,c2,c3,'xx' as c4 from A
      

  2.   

    create table A(ID int,CODE varchar(4))
    create table B(ID int,CODE varchar(4),[DESC] varchar(4))
    goinsert into A select 1,'AAAA'
    insert into A select 2,'BBBB'
    goinsert into B(ID,CODE,[DESC]) select ID,CODE,'XXXX' from A
    goselect * from B
    /*
    ID          CODE DESC 
    ----------- ---- ---- 
    1           AAAA XXXX
    2           BBBB XXXX
    */
    godrop table A,B
    go
      

  3.   

    我可能没说清楚
    再来
    当前已经有了这么个语句select ID,CODE, from A  而且很复杂。
    我现在想往很多表里插入数据,都要用到上面这个查询结果。但是有些表里的字段比上面这个查询出的字段多。
    比如
    insert into B(ID,CODE,TR1)
    insert into B(ID,CODE,TR2)
    insert into B(ID,CODE,TR3,TR4)由于都用到了上面那个查询结果,应该怎么办
      

  4.   

    這麼寫insert into B(ID,CODE,TR1)
    Select * From (select ID,CODE from A) T
    insert into B(ID,CODE,TR2)
    Select * From (select ID,CODE from A) T
    insert into B(ID,CODE,TR3,TR4)
    Select *,'AAA' From (select ID,CODE from A) T
      

  5.   

    1.放入临时表中
    select ID,CODE, xxx,...,yyy into # from A 
    2.从临时表insert
    insert into B(ID,CODE,TR1) select 'abc','dbg',* from #
      

  6.   

    insert into 表2 select 表1.*,新字段值 as 新字段名  from (你的SELECT 语句) 表1
      

  7.   

    多了不怕,比如表T1有A,B,C三个字段,表T2有A,B,C,D四个字段
    那就insert into T2 select *,'a' FROM T1
      

  8.   

    insert into B select *,tr1=(你想插入的字查询) from A
    insert into B select *,tr2=(你想插入的字查询) from A
    insert into B select *,tr3=(你想插入的字查询),tr4=(你想插入的字查询) from A