使用外连接进行递归查询?

时间:2013-10-02 15:21:54

标签: sql sql-server outer-join recursive-query

我正在尝试以下查询,

DECLARE @EntityType varchar(25)
SET @EntityType = 'Accessory';

WITH Entities (
        E_ID, E_Type,
        P_ID, P_Name, P_DataType, P_Required, P_OnlyOne,
        PV_ID, PV_Value, PV_EntityID, PV_ValueEntityID,
        PV_UnitValueID, PV_UnitID, PV_UnitName, PV_UnitDesc, PV_MeasureID, PV_MeasureName, PV_UnitValue,
        PV_SelectionID, PV_DropDownID, PV_DropDownName, PV_DropDownOptionID, PV_DropDownOptionName, PV_DropDownOptionDesc,
        RecursiveLevel
    )
AS
(
    -- Original Query
    SELECT dbo.Entity.ID AS E_ID, dbo.EntityType.Name AS E_Type,
    dbo.Property.ID AS P_ID, dbo.Property.Name AS P_Name, DataType.Name AS P_DataType, Required AS P_Required, OnlyOne AS P_OnlyOne,
    dbo.PropertyValue.ID AS PV_ID, dbo.PropertyValue.Value AS PV_Value, dbo.PropertyValue.EntityID AS PV_EntityID, dbo.PropertyValue.ValueEntityID AS PV_ValueEntityID,
    dbo.UnitValue.ID AS PV_UnitValueID, dbo.UnitOfMeasure.ID AS PV_UnitID, dbo.UnitOfMeasure.Name AS PV_UnitName, dbo.UnitOfMeasure.Description AS PV_UnitDesc, dbo.Measure.ID AS PV_MeasureID, dbo.Measure.Name AS PV_MeasureName, dbo.UnitValue.UnitValue AS PV_UnitValue,
    dbo.DropDownSelection.ID AS PV_SelectionID, dbo.DropDown.ID AS PV_DropDownID, dbo.DropDown.Name AS PV_DropDownName, dbo.DropDownOption.ID AS PV_DropDownOptionID, dbo.DropDownOption.Name AS PV_DropDownOptionName, dbo.DropDownOption.Description AS PV_DropDownOptionDesc,
    0 AS RecursiveLevel
    FROM dbo.Entity
    INNER JOIN dbo.EntityType ON dbo.EntityType.ID = dbo.Entity.TypeID
    INNER JOIN dbo.Property ON dbo.Property.EntityTypeID = dbo.Entity.TypeID
    INNER JOIN dbo.PropertyValue ON dbo.Property.ID = dbo.PropertyValue.PropertyID AND dbo.PropertyValue.EntityID = dbo.Entity.ID
    INNER JOIN dbo.DataType ON dbo.DataType.ID = dbo.Property.DataTypeID
    LEFT JOIN dbo.UnitValue ON dbo.UnitValue.ID = dbo.PropertyValue.UnitValueID
    LEFT JOIN dbo.UnitOfMeasure ON dbo.UnitOfMeasure.ID = dbo.UnitValue.UnitOfMeasureID
    LEFT JOIN dbo.Measure ON dbo.Measure.ID = dbo.UnitOfMeasure.MeasureID
    LEFT JOIN dbo.DropDownSelection ON dbo.DropDownSelection.ID = dbo.PropertyValue.DropDownSelectedID
    LEFT JOIN dbo.DropDownOption ON dbo.DropDownOption.ID = dbo.DropDownSelection.SelectedOptionID
    LEFT JOIN dbo.DropDown ON dbo.DropDown.ID = dbo.DropDownSelection.DropDownID
    WHERE dbo.EntityType.Name = @EntityType
    UNION ALL
    -- Recursive Query?
    SELECT E2.E_ID AS E_ID, dbo.EntityType.Name AS E_Type,
    dbo.Property.ID AS P_ID, dbo.Property.Name AS P_Name, DataType.Name AS P_DataType, Required AS P_Required, OnlyOne AS P_OnlyOne,
    dbo.PropertyValue.ID AS PV_ID, dbo.PropertyValue.Value AS PV_Value, dbo.PropertyValue.EntityID AS PV_EntityID, dbo.PropertyValue.ValueEntityID AS PV_ValueEntityID,
    dbo.UnitValue.ID AS PV_UnitValueID, dbo.UnitOfMeasure.ID AS PV_UnitID, dbo.UnitOfMeasure.Name AS PV_UnitName, dbo.UnitOfMeasure.Description AS PV_UnitDesc, dbo.Measure.ID AS PV_MeasureID, dbo.Measure.Name AS PV_MeasureName, dbo.UnitValue.UnitValue AS PV_UnitValue,
    dbo.DropDownSelection.ID AS PV_SelectionID, dbo.DropDown.ID AS PV_DropDownID, dbo.DropDown.Name AS PV_DropDownName, dbo.DropDownOption.ID AS PV_DropDownOptionID, dbo.DropDownOption.Name AS PV_DropDownOptionName, dbo.DropDownOption.Description AS PV_DropDownOptionDesc,
    (RecursiveLevel + 1)
    FROM Entities AS E2
    INNER JOIN dbo.Entity ON dbo.Entity.ID = E2.PV_ValueEntityID
    INNER JOIN dbo.EntityType ON dbo.EntityType.ID = dbo.Entity.TypeID
    INNER JOIN dbo.Property ON dbo.Property.EntityTypeID = dbo.Entity.TypeID
    INNER JOIN dbo.PropertyValue ON dbo.Property.ID = dbo.PropertyValue.PropertyID AND dbo.PropertyValue.EntityID = E2.E_ID
    INNER JOIN dbo.DataType ON dbo.DataType.ID = dbo.Property.DataTypeID
    INNER JOIN dbo.UnitValue ON dbo.UnitValue.ID = dbo.PropertyValue.UnitValueID
    INNER JOIN dbo.UnitOfMeasure ON dbo.UnitOfMeasure.ID = dbo.UnitValue.UnitOfMeasureID
    INNER JOIN dbo.Measure ON dbo.Measure.ID = dbo.UnitOfMeasure.MeasureID
    INNER JOIN dbo.DropDownSelection ON dbo.DropDownSelection.ID = dbo.PropertyValue.DropDownSelectedID
    INNER JOIN dbo.DropDownOption ON dbo.DropDownOption.ID = dbo.DropDownSelection.SelectedOptionID
    INNER JOIN dbo.DropDown ON dbo.DropDown.ID = dbo.DropDownSelection.DropDownID

)
SELECT E_ID, E_Type,
    P_ID, P_Name, P_DataType, P_Required, P_OnlyOne,
    PV_ID, PV_Value, PV_EntityID, PV_ValueEntityID,
    PV_UnitValueID, PV_UnitID, PV_UnitName, PV_UnitDesc, PV_MeasureID, PV_MeasureName, PV_UnitValue,
    PV_SelectionID, PV_DropDownID, PV_DropDownName, PV_DropDownOptionID, PV_DropDownOptionName, PV_DropDownOptionDesc,
    RecursiveLevel
FROM Entities
INNER JOIN [dbo].[Entity] AS dE
    ON dE.ID = PV_EntityID

问题是第二个查询,“递归的”是获取我期望的数据,因为我不能像第一个查询那样进行LEFT JOIN。 (至少根据我的理解)。

如果我删除了需要LEFT(外部)JOIN的数据的提取,则递归工作完美。我的问题是我需要两者。有没有办法可以实现这个目标?

1 个答案:

答案 0 :(得分:4)

http://msdn.microsoft.com/en-us/library/ms175972.aspx你不能在递归CTE中有左/右/外连接。

对于递归CTE,您不能使用子查询,因此我在this example之后提取。

他们使用两个CTE。第一个不是递归的,左连接是为了获得它需要的数据。第二个CTE是第一个CTE上的递归和内连接。由于CTE1不是递归的,因此它可以保留连接并为缺失的行提供默认值,并且保证在内连接中起作用。

但是,您也可以使用union和subselect复制左连接,虽然它通常没用,但很有趣。

在这种情况下,你会保留你的第一个陈述。它将匹配成功加入的所有行。

然后UNION使用另一个查询来查询,该查询删除了连接,但是有一个

NOT EXISTS(SELECT 1 FROM MISSING_ROWS_TABLE WHERE MAIN_TABLE.JOIN_CONDITION = MISSING_ROWS_TABLE.JOIN_CONDITION)

这将获取查询1中先前连接条件失败的所有行。您可以使用NULL替换从MISSING_ROWS_TABLE获得的colmuns。一旦使用不支持外连接的编码框架,我不得不这样做。由于递归CTE不允许子查询,因此必须使用第一种解决方案。