错误:使用storedprocedure插入层次结构值

时间:2010-01-27 04:57:19

标签: sql-server-2008 hierarchyid

尝试运行将结果插入sql表中的查询时出现此错误。 我将表名作为参数传递,如何将层次结构值赋给insert语句。

这是我的代码:

declare @pathhere hierarchyid

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

插入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)

我在这一行得到错误:

'+ CAST(@pathhere as hierarchyid)+'

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

任何人都可以帮我解决如何传递层次结构值

1 个答案:

答案 0 :(得分:0)

您正在尝试创建一个可以作为语句执行的字符串。所以你需要将hierarchyid变为nvarchar(max)。

尝试:@pathhere.ToString()