I have got two tables.
Devices
- DeviceId
- DeviceName
and
DeviceAttribute
- DeviceAttributeId
- Attributeid
- DeviceId
Now, I am try to write a query, in which I pass in a number of attribute Ids, and I want to find all devices that have ALL of the Ids passed in.
I am passing the Ids in as XML, and I am then able to transform this data into a temporary table, which each Id in its own row.
So, at this point, assume all the attributes I want to match are in a table –
CREATE TABLE #TempAttribute (AttributeId int)
So, I may want to find all devices which have attributeIds 3,34 and 67
I have got a query which concatenates all the devices attribute Ids into one column, but I’m not sure if this is going in the right direction:
SELECT deviceID
, [Name]
, (SELECT CAST(cast(attributeID as varchar(10)) + ', ' AS VARCHAR(MAX))
FROM deviceattribute
WHERE (deviceID = device.deviceID)
order by attributeID
FOR XML PATH ('')
) AS attribs
FROM device
1 Answer