rtexample:insert ..
  when type = 1 then
    insert tab_a(...) values(...)
  when type = 2 then
    insert tab_b(...) values(...)
  when type = 3 then
    insert tab_c(...) values(..)
  ....
select *
  from src_a
 where type in(1,2,3...)查询src_a表,根据type 的值,分别insert到不同的表。请问这个sql server 2005 是否可以做到?

解决方案 »

  1.   

    将以下语句作成函数
    when type = 1 then 
        insert tab_a(...) values(...) 
      when type = 2 then 
        insert tab_b(...) values(...) 
      when type = 3 then 
        insert tab_c(...) values(..) 
    然后
    select 函数名(type) from  src_a 
    where type in(1,2,3...) 
      

  2.   

    用动态语句,不过也不会是一条语句:形如:declare @s varchar(max)set @s='insert ' + case when @type=1 then 'tbA' when @type=2 then 'tbC' when ..... end + '(字段列表) select 值列表'exec(@s)
      

  3.   

    declare @typeid int
    select @typeid=typeid from yourtableif(@typeid=1)
    begin
    //////////////////
    end
    else if(@typid=2)
    begin
    ///////////////
    end
    else
    begin
    ///////////
    end