错误:使用存储过程插入层次结构值

发布于 2024-08-19 15:42:49 字数 881 浏览 2 评论 0原文

当尝试运行将结果插入到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫轩蝶泪 2024-08-26 15:42:49

您正在尝试创建一个可以作为语句执行的字符串。因此,您需要将 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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文