Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
i'm trying to transform my hierarchy using procedure as follows:

SQL
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 @SQL
                                PRINT @usableLevels




                                FETCH NEXT FROM LevelHierarchy INTO @usableLevels;
                    END;
                    CLOSE LevelHierarchy;
                    DEALLOCATE LevelHierarchy;




        SET @SQL =@SQL +' FROM HIERARCHIES Where FilterId ='''+@FilterId+''' and id = '+@unid+''
        --EXECUTE sp_executesql @SQL
        SELECT @SQL


Unfortunatelly, i'm getting error

ASM
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
Posted

1 solution

Generally, one get the above error when you concatenate two columns of Text datatype.
See for details here: Invalid operator for data type. Operator equals add, type equals text.[^]

As mentioned there, to work around this error you need to CAST the TEXT column into VARCHAR first before concatenating the columns. Sample:
SQL
SELECT [StudentID],
CAST(CAST([CommentsOnTeacher] AS VARCHAR(8000)) +
CAST([CommentsOnSubject] AS VARCHAR(8000)) AS TEXT)
AS [AllComments]
FROM [dbo].[StudentComments]
 
Share this answer
 
Comments
Maciej Los 6-Mar-13 11:44am    
+5!
Sandeep Mewara 6-Mar-13 11:47am    
Thanks Mac. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900