Question

I'm getting this error when trying to run a query that inserts results into a table in sql. im passing the table name as parameter,how to give the hierarchy value to the insert statement.

here is my code:

declare @pathhere hierarchyid

select @pathhere=Path from SectionDetails where Sectionid=@sectionid and SectionName=@sectionname and Batchid=@batchid  and Deptid=@deptid and Schoolid=@schoolid

insert stmt:

set @sqlstmt = 'insert into '+@batch+'(StudentID, StudentName,SectionID,SectionName,BatchID,BatchName, DeptID,DeptName, SchoolID,Path)
values('''+@sectionid+''','''+@sectionname+''','''+@sectionid+''','''+@sectionname+''','''+@batchid+''','''+@batchname+''','''+ @deptid+''','''+@deptname+''', '''+@schoolid+''','+ CAST(@pathhere as hierarchyid)+')'
exec(@sqlstmt)

im getting error in this line:

'+ CAST(@pathhere as hierarchyid)+'

as Invalid operator for data type. Operator equals add, type equals hierarchyid.

can anyone pls help me out how to pass the hierarchy value

Was it helpful?

Solution

You're trying to create a string that can be executed as a statement. So you need to get your hierarchyid into nvarchar(max) instead.

try: @pathhere.ToString()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top