错误:使用存储过程插入层次结构值
当尝试运行将结果插入到 sql 中的表中的查询时,我收到此错误。 我将表名作为参数传递,如何将层次结构值赋予插入语句。
这是我的代码:
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)+'
as 数据类型的无效运算符。运算符等于add,类型等于hierarchyid。
任何人都可以帮我解决如何传递层次结构值
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试创建一个可以作为语句执行的字符串。因此,您需要将 hierarchyid 转换为 nvarchar(max) 。
尝试:
@pathhere.ToString()
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()