Hi,
i'm trying to transform my hierarchy using procedure as follows:
DECLARE @FilterId NVARCHAR(MAX) = 'CUSTOMER_IFRS'
DECLARE @unid as NVARCHAR(MAX) = '1410'
DECLARE @id HIERARCHYID
SELECT @id = HID FROM dbo.HIERARCHIES WHERE FilterId =@FilterId and id = @unid;
DECLARE @SQL NVARCHAR(MAX) = 'SELECT OFSA_ID as '+@FilterId+', sName AS LEAF_DESCRIPTION'
DECLARE @ProductMaxLevel as NVARCHAR(MAX)
SELECT @ProductMaxLevel = MAX(HID_Level) FROM HIERARCHIES WHERE FilterId = @FilterId
DECLARE @usableLevels NVARCHAR(MAX)
DECLARE LevelHierarchy CURSOR FOR
SELECT DISTINCT HID_Level FROM HIERARCHIES WITH (NOLOCK) WHERE FilterId = 'CUSTOMER_IFRS' order by 1 asc;
OPEN LevelHierarchy;
FETCH NEXT FROM LevelHierarchy INTO @usableLevels;
WHILE @usableLevels <= @ProductMaxLevel-1
BEGIN
SET @SQL = @SQL +'
,(SELECT a.Ofsa_id FROM dbo.HIERARCHIES a where a.FilterId ='''+@FilterId+''' and '+@id+'.IsDescendantOf(a.HID) = 1 and a.Tree_Level = '+@usableLevels+') AS LEVEL_0'+@usableLevels+'_ID,
(SELECT a.sName FROM dbo.HIERARCHIES a where a.FilterId ='''+@FilterId+''' and '+@id+'.IsDescendantOf(a.HID) = 1 and a.Tree_Level = '+@usableLevels+') AS LEVEL_0'+@usableLevels+'_DESC'
PRINT @usableLevels
FETCH NEXT FROM LevelHierarchy INTO @usableLevels;
END;
CLOSE LevelHierarchy;
DEALLOCATE LevelHierarchy;
SET @SQL =@SQL +' FROM HIERARCHIES Where FilterId ='''+@FilterId+''' and id = '+@unid+''
SELECT @SQL
Unfortunatelly, i'm getting error
Msg 403, Level 16, State 1, Line 22
Invalid operator for data type. Operator equals add, type equals hierarchyid.
I think the error is related to part where I'm trying to pass @id parameter into SQL Statement ('+@id+'.IsDescendantOf(a.HID) = 1)
If anyone can help me in order to to solve the problem.
Any advice/help/code corecction would be very much appreciated.
Thanks
A