Wednesday, 2 October 2013

Unexpected conversion error in recursive query

Unexpected conversion error in recursive query

I've tried this but keep coming up with an error. "Msg 245, Level 16,
State 1, Line 5 Conversion failed when converting the varchar value
'Accessory' to data type int." I'm not certain why I'm getting it, it
appears to be trying to convert EntityType to an ID? but I'm not certain
where or why.
My query I'm using is as follows,
DECLARE @EntityType varchar(25)
SET @EntityType = 'Accessory';
WITH EntityRec (
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 E1.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 AS E1
INNER JOIN dbo.EntityType ON dbo.EntityType.ID = E1.TypeID
INNER JOIN dbo.Property ON dbo.Property.EntityTypeID = E1.TypeID
INNER JOIN dbo.PropertyValue ON dbo.Property.ID =
dbo.PropertyValue.PropertyID AND dbo.PropertyValue.EntityID = E1.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 EntityRec AS E2
INNER JOIN dbo.EntityType ON dbo.EntityType.ID = E2.E_Type
INNER JOIN dbo.Property ON dbo.Property.EntityTypeID = E2.E_Type
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
INNER JOIN dbo.Entity AS E1 ON E1.ID = E2.PV_ValueEntityID
)
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 EntityRec
INNER JOIN [dbo].[Entity] AS dE
ON dE.ID = PV_EntityID
I've tried many variations of the above query trying to tweak it. My
current problem with it is, I'm not sure what exactly I'm doing wrong. I
understand what the error means I'm just not sure why/where it's trying to
do that.
Other Info
I have a Db Structure like so,
dbo.Entity: ID PK, TypeID FK (References EntityType)
dbo.EntityType: ID PK, Name
dbo.Property: ID PK, EntityTypeID FK, Name, DataType, Required, etc.
dbo.PropertValue: ID PK, EntityID FK, PropertyID FK, ~Value, ~UnitValueID
FK, ~DropDownSelectedID FK, ~ValueEntityID FK
Where "~" means nullable. The value, UnitValueID, DropDownSelectedID, and
ValueEntityID are possible values. (You're going to have only one non-null
value there).
Ex. "Accessory" is an EntityType. A property of Accessory is Type. Type is
a property that accepts the EntityType AccessoryType. "AccessoryType" has
3 properties (DimA, DimB, DimC) whose values are EntityType of
"Dimension". How would I construct a query that gets all Accessories (When
@EntityType = "Accessory"), All AccessoryTypes that are values of those
Accessories, and All Dimensions that are values of those AccessoryTypes?
Any assistance in fixing this would be greatly appreciated.

No comments:

Post a Comment