2015-12-14 64 views
2

我想在3個不同的表使用UNION ALL,通過使用SELECT INTO將它們合併成一個表。UNION ALL與每個表中的列數不同的3個表格 - 如何這樣做有效地

表1,2和3分別有15個,第7和8列。

那麼,有沒有辦法讓我使用UNION ALL,並且對於缺失的列,表2和表3默認爲NULL,而沒有單獨對它們進行分類?

舉例來說,我一直在做:

SELECT NULL as [Company_Code], NULL as [Doc_Code], 
NULL as [Doc_Type], [H ID] as [Document_No] FROM [table_2] 
INTO BIG_TABLE 
UNION ALL 
SELECT 
[Document Company] as [Company_Code], [Document Company] as [Doc_Code], 
[Doc Type] as [Doc_Type], NULL as [Document_No] 
FROM [table_3] 

這樣,列數匹配,我可以UNION他們。

不過,我想知道是否有辦法避免了繁瑣的機制,以避免爲失蹤的每一列中插入NULL,並有一次性自動完成?

謝謝。

+0

默認情況下,它將採用第一個select語句中列的名稱,例如,只需選擇「NULL NULL,NULL,NULL,h_id FROM table_2」。 – ZLK

+0

查看我的答案以便隨心所欲地幫助到您。 – Ajay2707

回答

3

總之,沒有。結果集合必須具有相同的列/數據類型。如果你想有剩餘的套填充null,要做到這一點最簡單的方法是做類似所謂

select col1 
, col2 
, col3 
, col4 
from tbl1 

union all 

select null as col1 
, null as col2 
, null as col3 
, null as col4 
from tbl2 
+0

無後顧之憂 - 感謝您的輸入 – elbarto

1

聯盟應該是裏面的另一個選擇

SELECT * FROM(
    SELECT col1, col2 FROM test_table1 
    UNION ALL 
    SELECT col1, col2,col3 FROM test_table2 
); 

結果將col1和COL2未匹配列跳過

+1

我想他正試圖以填充空進COL3,而不是完全將其忽略 – iliketocode

+0

@iliketocode這是正確的 - 我只是希望,而不是爲每列輸入NULL項,有一個更快的方法。 – elbarto

+1

@liketocode亞我同意你的看法,但主要意圖縮短查詢 – Ankanna

0

儘可能最好的方式來抵消其失蹤的列是使用null作爲列名您要看到空列,如下圖所示:...

SELECT id as var1, mgr_id as var2,name as var3 into exp_test1 FROM 
emp123 UNION ALL 
     SELECT null as employeeid, null as departmentid,null as lastname FROM employee 
+0

這是我做了什麼,但希望避免,由於任務的繁瑣性質。 雖然謝謝。 – elbarto

0

我還是建議去與@iliketocode。

動態列生成按您的要求

這只是另一種選擇,但你想要的(只有它需要一定的時間來執行,而不是上面的回答)純粹是動態的。爲了在不定義每列的情況下實現結果,可以創建動態查詢,通過循環自動將列添加爲null。

我面對,並且由於時間很短,我是如上面做了同樣的問題。但是今天我會填充以給出適當的解決方案(當時我還沒有完成),並且我會爲您創建一個樣本,希望這對您有所幫助。

--create table t1 (col1 int , col2 int, col3 int) 
--create table t2 (col1 int , col2 int, col3 int, col4 int) 

--insert into t1 values (1,11,111), (2,22,222) 
--insert into t2 values (1,11,111,1111), (2,22,222,null) 

--Step 1 - Declaration of variable 
Declare @NoOfColumnForUnion int = 5 

declare @T1TableColumnList nvarchar(max) ='' ,@T2TableColumnList nvarchar(max) ='' , @colName nvarchar(500) , @test cursor 

--Step 2 - Get the column list of first table i.e. t1 and store into @T1TableColumnList variable 
set @test = cursor for 
       select name from syscolumns 
       where id = object_id('t1')     

open @test 
fetch next from @test into @colName 
while @@fetch_status = 0 
begin 
    set @T1TableColumnList = @T1TableColumnList + @colName + ',' 

    fetch next from @test into @colName 
end 

set @T1TableColumnList = left(@T1TableColumnList , len(@T1TableColumnList)-1) 

close @test 
deallocate @test 

--Step 3 - Get the column list of Second table i.e. t2 and store into @T2TableColumnList variable 
set @test = cursor for 
       select name from syscolumns 
       where id = object_id('t2')     

open @test 
fetch next from @test into @colName 
while @@fetch_status = 0 
begin 
    set @T2TableColumnList = @T2TableColumnList + @colName + ',' 

    fetch next from @test into @colName 
end 

set @T2TableColumnList = left(@T2TableColumnList , len(@T2TableColumnList)-1) 

close @test 
deallocate @test 

--Step 4 - Check the length of column list to add null columns or remove columns 
--First table check 
Declare @T1lengthofColumnList int 
set @T1lengthofColumnList = (len(@T1TableColumnList) - len(replace(@T1TableColumnList, ',', ''))) + 1 

--add columns 
if(@T1lengthofColumnList < @NoOfColumnForUnion) 
Begin 
    While (@T1lengthofColumnList < @NoOfColumnForUnion) 
    Begin 
     set @T1lengthofColumnList = @T1lengthofColumnList + 1 
     Set @T1TableColumnList = @T1TableColumnList + ', null col' + cast(@T1lengthofColumnList as varchar(10)) 
    End 
End 
--remove columns 
Else if(@T1lengthofColumnList > @NoOfColumnForUnion) 
Begin 
    While (@T1lengthofColumnList > @NoOfColumnForUnion) 
    Begin 
     set @T1lengthofColumnList = @T1lengthofColumnList - 1  
     Set @T1TableColumnList = LEFT(@T1TableColumnList, LEN(@T1TableColumnList) - CHARINDEX(',',REVERSE(@T1TableColumnList))) 
    End 
End 

--Second table check 
Declare @T2lengthofColumnList int 
set @T2lengthofColumnList = (len(@T2TableColumnList) - len(replace(@T2TableColumnList, ',', ''))) + 1 

--add columns 
if(@T2lengthofColumnList < @NoOfColumnForUnion) 
Begin 
    While (@T2lengthofColumnList < @NoOfColumnForUnion) 
    Begin 
     set @T2lengthofColumnList = @T2lengthofColumnList + 1 
     Set @T2TableColumnList = @T2TableColumnList + ', null col' + cast(@T2lengthofColumnList as varchar(10)) 
    End 
End 
--remove columns 
Else if(@T2lengthofColumnList > @NoOfColumnForUnion) 
Begin 
    While (@T2lengthofColumnList > @NoOfColumnForUnion) 
    Begin 
     set @T2lengthofColumnList = @T2lengthofColumnList - 1  
     Set @T2TableColumnList = LEFT(@T2TableColumnList, LEN(@T2TableColumnList) - CHARINDEX(',',REVERSE(@T2TableColumnList))) 
    End 
End 

--Step 5 - create dynamic query and execute 
DECLARE @template AS varchar(max) 

SET @template = 'select ' + @T1TableColumnList + ' from t1 union all ' 
+ ' select ' + @T2TableColumnList + ' from t2 ' 

select @template 

EXEC (@template) 


--drop table t1 
--drop table t2 
相關問題