2017-11-09 5 views
1
create table #tableA(id int, val varchar(50)) 
create table #tableB(id int, val varchar(50)) 
create table #tableC(id int, val varchar(50)) 
create table #tableD(id int, val varchar(50)) 

insert into #tableB values (1, '11'); 
insert into #tableB values (2, '22'); 

が存在する私は#tableA場合#tableAは、その後挿入値でない場合は、SQL Server 2008 R2で

insert into #tableD 
    select * 
    from #tableA; 
  • を値を持つ場合#tableDと私の条件の挿入値は

    1. ありたいです空の場合

      insert into #tableD 
          select * 
          from #tableB; 
      
    2. #tableA場合と#tableBは空です

      insert into #tableD 
          select * 
          from #tableC; 
      

    私は、これが最も簡単な方法を行うことができますどのように?このような

  • 答えて

    1

    2つの基本的な方法がありますが、一時テーブルの数が動的な場合はうまくいかないIC。私は、クエリに*使用していますが、ベストプラクティスは、列

    最初はうまく動作しますが、膨大なデータに遅くなることがあり、必要以上に余分な作業を行いますが、小さなデータが設定されている場合、それは[OK]をする必要がありを指定することに注意してください

    insert into #tabled 
    select * from #tablea union all 
    select * from #tableb where 0 = (select count(*) from #tableA) union all 
    select * from #tablec where 0 = (select count(*) 
               from (select top 1 id from #tablea 
                 union all 
                 select top 1 id from #tableb 
                 ) x 
               ) 
    

    または2番目の方法は正常に動作し、必要な作業のみが実行されます。

    insert into #tableD select * from #tableA 
    if @@rowcount = 0 
    begin 
        insert into #tableD select * from #tableB 
        if @@rowcount = 0 
        begin 
        insert into #tableD select * from #tableC 
        if @@rowcount = 0 
        begin 
         print('no rows inserted') 
        end 
        else 
        begin 
         print('rows inserted from C') 
        end  
        end 
        else 
        begin 
        print('inserted from B') 
        end 
    end 
    else 
    begin 
        print('insert from A') 
    end 
    
    0

    何か作業をする必要があります:

    if exists (select 1 from #tableA) 
        begin 
         insert into #tableD 
          select * from #tableA 
        end 
    else 
        begin 
         insert into #tableD 
          select * from #tableB 
        end 
    
    if not exists 
        (select 1 from #tableA union select 1 from #tableB) 
        begin 
         insert into #tableD 
          select * from #tableC 
        end 
    
    0

    使用(のTransact-SQL)EXISTS条件

    if exists (select * from #tableA) 
    begin 
        insert into #tableD select * from #tableA; 
    end 
    else 
    begin 
        insert into #tableD select * from #tableB; 
    end 
    if not exists (select * from #tableA) 
    begin 
         if not exists (select * from #tableB) 
         begin 
         insert into #tableD select * from #tableC; 
         end 
    end 
    
    0

    IF..THEN..ELSE

    IF EXISTS(SELECT * FROM #TableA) 
    BEGIN 
        insert into #tableD select * from #tableA;  
    END 
    ELSE IF EXISTS(SELECT * FROM #TableB) 
    BEGIN 
        insert into #tableD select * from #tableB; 
    END 
    ELSE 
    BEGIN 
        insert into #tableD select * from #tableC; 
    END 
    
    のTransact-SQLステートメントを使用しようとに基づいて値を挿入するには
    -1

    使用この

    INSERT INTO #tabled 
        SELECT * FROM #tableA 
    UNION ALL 
        SELECT * FROM #tableB 
    UNION ALL 
        SELECT * FROM #tableC 
    
    関連する問題