错误:使用StoredProcedure的插入层级值
-
23-09-2019 - |
题
我想运行一个查询时收到此错误结果插入到SQL表。 IM传递表的名称作为参数,如何给层次值插入语句。
这是我的代码:
declare @pathhere hierarchyid
select @pathhere=Path from SectionDetails where Sectionid=@sectionid and SectionName=@sectionname and Batchid=@batchid and Deptid=@deptid and Schoolid=@schoolid
插入语句:
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.
任何人都可以请帮助我如何通过层次值
解决方案
您正试图创建一个可以作为一个语句执行的字符串。所以,你需要得到你的HIERARCHYID到为nvarchar(最大)代替。
尝试:@pathhere.ToString()
不隶属于 StackOverflow