1

我正在尝试以下查询,

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 的数据的获取,那么递归就可以完美地工作。我的问题是我两者都需要。有没有办法我可以做到这一点?

4

1 回答 1

4

根据http://msdn.microsoft.com/en-us/library/ms175972.aspx,您不能在递归 CTE 中加入左/右/外连接。

对于递归 CTE,您也不能使用子查询,因此我建议遵循此示例

他们使用两个 CTE。第一个不是递归的,通过左连接来获取它需要的数据。第二个 CTE 是递归的,并且在第一个 CTE 上进行内部连接。由于 CTE1 不是递归的,它可以左连接并为丢失的行提供默认值,并保证在内连接中工作。

但是,您也可以使用联合和子选择复制左连接,尽管它通常不是很有用,但很有趣。

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

然后将该查询与另一个删除连接但具有

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

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

于 2013-10-02T16:21:54.463 回答