2017-04-04 85 views
0

可以創建具有不同列和列長度的2個或更多表的視圖。我正在使用MS SQL。構造具有不同列和列長度的表的視圖

例如,我有下面的表和列結構

USAAddress (door no, street char(30), city, zipcode (50)) 
GBPAddress (door no, addressline1 char(30), addressline2 char(30), 
addressline3 city, country, postcode char(30), phoneno char(30)) 

是否可以創建具有不同列和數據類型大小以上兩代表的視圖?

i) street would be mapped to addressline1 
ii) zipcode would be mapped to postcode (zipcode is char(50) while postcode is char(30)) 
iii)phoneno is missing in USAAddress table, while it is present in GBPAddress table. I view should have phoneno but should show as empty in USAAddress. 
+0

你試過了嗎?對的,這是可能的。 –

回答

0

使用視圖類似下面的查詢應該做的伎倆:

select addressline1, postcode, phoneno 
from GBPAddress 

union all 

select street, left(zipcode,30), null 
from USAAddress 

一點題外話,它更常見的是使用VARCHAR對於其中長度變化長文本字段。而不是在數據的長度相當固定時使用CHAR。

相關問題