用存储过程,exec(@sql) 的语句可以达到目的。

解决方案 »

  1.   

    create proc proc_formula 
    as
    begin
      declare @a int
      declare @s varchar(256)
      declare @str varchar(2560)
      declare cur_x cursor for select id, gongshi from table2 
      open cur_x
      fetch next from cur_x
      into @a,@s
      while @@fetch_status = 0
      begin
        select @str ='insert into table3 select '+ convert(varchar(20),@a) +' , '+ @s + ' from table1 where id = ' + convert(varchar(20),@a)
        exec (@str)
        fetch next from cur_x
        into @a,@s
      end
      
      close cur_x
      deallocate cur_x  
    endgo
    proc_formula 
    go
      

  2.   

    exec('select id,sum(+'replace(b.gongshi,'/',' as decimal(12,6))/')'+') from table1 left outer join table2 b on table1.id=b.id')
      

  3.   

    update:
    exec('select id,sum(+'replace(b.gongshi,'/',' as decimal(12,6))/')'+') from table1 RIGHT outer join table2 b on table1.id=b.id')