T-SQL 合并两个具有常见列和不常见列的表 [英] T-SQL merging two tables with common and uncommon columns

查看:22
本文介绍了T-SQL 合并两个具有常见列和不常见列的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题.我有两个带有常见和不常见列的表,如下所示:在 T SQL 或 SSIS 中是否有一种有效的方法可以做到这一点?谢谢

Here is my problem. I have two tables with common and uncommon columns as follows: Is there an effective way of doing this in T SQL or SSIS? Thank you

表 1:

a b c
1 2 3
7 8 9 

表 2:

a d k
4 6 7
9 0 2

合并表:

a   b   c      d    k
1   2   3     null  null
7   8   9     null  null
4 null  null   6    7
9 null  null   0    2

推荐答案

您可以使用 union 为缺少的列添加 null

You could use union adding null for missing column

  select a,b,c, null d, null k
  from table1
  union 
  select a,null, null,d, k
  from table1

这篇关于T-SQL 合并两个具有常见列和不常见列的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆