2016-07-22 66 views
1

我得到一个“使用UNION,INTERSECT或EXCEPT操作符合并的所有查询在其目标列表中必须具有相同数量的表达式”。SQL QUery,Insert,Union和Join

INSERT INTO dbo.FactInternetSales (
    ProductKey 
    ,CustomerKey 
    ,DateKey 
    ,OrderQuantity 
    ,UnitPrice 
    ,UnitPriceDiscount 
    ,TaxAmt 
    ,Freight 
    ) 
SELECT ProductKey 
FROM dbo.dimProduct 

UNION ALL 

SELECT CustomerKey 
FROM dbo.dimCustomer 

UNION ALL 

SELECT DateKey 
FROM dbo.dimDate 

UNION ALL 

SELECT D.OrderQty 
    ,D.UnitPrice 
    ,D.UnitPriceDiscount 
    ,H.TaxAmt 
    ,H.Freight 
FROM AdventureWorksLT2008.SalesLT.SalesOrderDetail AS D 
FULL JOIN AdventureWorksLT2008.SalesLT.SalesOrderHeader H ON D.SalesOrderID = H.SalesOrderID 

回答

0

该错误消息说,工会all..Final查询所有可是没有列作为others..You可以试试下面这工作,因为你没有公共列在你的代码

您的加盟可以试试这个

;With cte 
as 
(
Select ProductKey From dbo.dimProduct 
UNION All 
Select CustomerKey From dbo.dimCustomer 
UNION All 
Select DateKey From dbo.dimDate) 
,cte1 as 
(
Select D.OrderQty, 
    D.UnitPrice, 
    D.UnitPriceDiscount, 
    H.TaxAmt, 
    H.Freight 
From  AdventureWorksLT2008.SalesLT.SalesOrderDetail As D 
FULL JOIN AdventureWorksLT2008.SalesLT.SalesOrderHeader H 
ON D.SalesOrderID = H.SalesOrderID 
) 
select cte.*,cte1.* 
from cte,cte1 
+0

我不明白,你的代码显示不正确的语法。我明白我得到的错误,但不能解决问题,而试图将其插入表 – cnayak

+0

看到更新,这工作。你也可以使用加入,如果他们有相同的列 – TheGameiswar

+0

谢谢我能够得到我想要的东西从你给的概念。我删除了union all,并使用了cte1,cte2,cte3,cte4来获取Productkey,CustomerKey,DateKey的不同列 – cnayak

0

当我尝试通过以下方式我得到一个错误的SQL Server数据库引擎实例不能获得在这个时候锁资源插入。在活动用户较少时重新运行您的声明。请数据库管理员检查此实例的锁定和内存配置,或检查长时间运行的事务。

WITH cte1 
AS (
    SELECT ProductKey 
    FROM dbo.dimProduct 
    ) 
    ,cte2 
AS (
    SELECT CustomerKey 
    FROM dbo.dimCustomer 
    ) 
    ,cte3 
AS (
    SELECT DateKey 
    FROM dbo.dimDate 
    ) 
    ,cte4 
AS (
    SELECT D.OrderQty 
     ,D.UnitPrice 
     ,D.UnitPriceDiscount 
     ,H.TaxAmt 
     ,H.Freight 
    FROM AdventureWorksLT2008.SalesLT.SalesOrderDetail AS D 
    FULL JOIN AdventureWorksLT2008.SalesLT.SalesOrderHeader H ON D.SalesOrderID = H.SalesOrderID 
    ) 
INSERT INTO dbo.FactInternetSales (
    ProductKey 
    ,CustomerKey 
    ,DateKey 
    ,OrderQuantity 
    ,UnitPrice 
    ,UnitPriceDiscount 
    ,TaxAmt 
    ,Freight 
    ) 
SELECT cte1.* 
    ,cte2.* 
    ,cte3.* 
    ,cte4.* 
FROM cte1 
    ,cte2 
    ,cte3 
    ,cte4 
+0

您可以检出错误@TheGameiswar – cnayak