我用select查询出来的结果集,想在结果集上再加上一列,这一列的值全是我指定的一个字符串,然后插入数据库去,这样的语句要怎么写?例:表A:
     Col1   Col2    Col3
      1      1-1     1-2
      2      2-1     2-2用 select * from A得出结果集再加上一列然后插入到表B:表B结果:
     Col1    Col2    Col3    Col4
      1       1-1     1-2     OK
      2       2-1     2-2     OK我不知道怎么在结果集上再插入一列然后设置值
还有一个:在SQL 2000上存储过程可以用“表参数”吗?

解决方案 »

  1.   


    insert into b
    select *,'OK' as col4
    from a
    存储过程可以使用表变量!
      

  2.   


    --字段多了限定下
    insert into b(col1,col2,col3,col4)
    select col1,col2,col3,'OK' as col4
    from a
      

  3.   


    declare @t table(id int)
    insert into #t select 1update @t set id = 2--...
      

  4.   

    如果你指定的列不重复的话
      创建一个零时表  create table #a(id  , Col4)
    重复的话
      insert into b
      select col1,col2,col3,'OK' from a或者先insert b
    select col1,col2,col3 from a
    然后再update
      

  5.   

    这个问题我刚刚也才想起来怎么做。那第二个问题呢?SQL 2000存储过程有没有办法使用“表参数”?就是把一个结果集传给一个过程。
      

  6.   


    declare @表A table (Col1 int,Col2 varchar(3),Col3 varchar(3))
    insert into @表A
    select 1,'1-1','1-2' union all
    select 2,'2-1','2-2'select *,'OK' as col4 into #表B from @表Aselect * from #表B
    /*
    Col1        Col2 Col3 col4
    ----------- ---- ---- ----
    1           1-1  1-2  OK
    2           2-1  2-2  OK
    */
    drop table #表B
      

  7.   


    create  table tb(Col1 int,Col2 varchar(3),Col3 varchar(3))
    insert into tb
    select 1,'1-1','1-2' union all
    select 2,'2-1','2-2'create proc pr_name(@str varchar(10))
    as
    begin
        select *,@str as col4 from tb
    endexec pr_name 'ok'/*
    Col1        Col2 Col3 col4
    ----------- ---- ---- ----------
    1           1-1  1-2  ok
    2           2-1  2-2  ok(2 行受影响)