Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server-2008
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
 
<pre>
    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</pre>

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
Posted 6-Mar-13 3:20am
AlmirM666

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
    SELECT [StudentID],
    CAST(CAST([CommentsOnTeacher] AS VARCHAR(8000)) +
    CAST([CommentsOnSubject] AS VARCHAR(8000)) AS TEXT)
    AS [AllComments]
    FROM [dbo].[StudentComments]
  Permalink  
Comments
Maciej Los at 6-Mar-13 11:44am
   
+5!
Sandeep Mewara at 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)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200


Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 6 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100