如何将多个SQL Server查询合并为一个

时间:2013-01-16 23:07:40

标签: sql-server

如何将以下查询合并为一个。

SELECT MID,
       Status,
       Owner1Fname,
       Owner1Lname,
       LegalName,
       DBAName,
       DBAEmail,
       DBAPhone,
       CorpPhone,
       SICCode
FROM   vw_merLeadData

SELECT replastname,
       replastname
FROM   vw_XXX_Page1

SELECT installdate,
       model,
       ownership,
       serialno
FROM   dbo.merequip 

1 个答案:

答案 0 :(得分:1)

和以前的评论者一样,我不确定为什么你会想要这样做但是我再也不知道你需要它的上下文,所以我猜你有充分的理由!无论如何,这是你如何做到的(为dud格式化道歉):

SELECT MID,
   Status,
   Owner1Fname,
   Owner1Lname,
   LegalName,
   DBAName,
   DBAEmail,
   DBAPhone,
   CorpPhone,
   SICCode,
   replastname=NULL,
   replastname=NULL,
   installdate=NULL,
   model=NULL,
   ownership=NULL,
   serialno=NULL
   FROM   vw_merLeadData
   UNION ALL
   SELECT MID=NULL,
   Status=NULL,
   Owner1Fname=NULL,
   Owner1Lname=NULL,
   LegalName=NULL,
   DBAName=NULL,
   DBAEmail=NULL,
   DBAPhone=NULL,
   CorpPhone=NULL,
   SICCode=NULL,
   replastname=NULL,
   replastname=NULL,
   installdate=NULL,
   model=NULL,
   ownership=NULL,
   serialno=NULL       
FROM   vw_XXX_Page1
UNION ALL
SELECT 
MID=NULL,
   Status=NULL,
   Owner1Fname=NULL,
   Owner1Lname=NULL,
   LegalName=NULL,
   DBAName=NULL,
   DBAEmail=NULL,
   DBAPhone=NULL,
   CorpPhone=NULL,
   SICCode=NULL,
replastname=NULL,
   replastname=NULL,
installdate,
   model,
   ownership,
   serialno
 FROM   dbo.merequip 

基本上你可以将任何tqo查询组合在一起,只要它们具有相同数量的列,并且每个字段中的数据类型与其他查询中的相应字段相同(或至少可转换),因此所有NULL这只是假列 希望有所帮助。