Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 错误:使用StoredProcess插入层次结构值_Sql Server 2008_Hierarchyid - Fatal编程技术网

Sql server 2008 错误:使用StoredProcess插入层次结构值

Sql server 2008 错误:使用StoredProcess插入层次结构值,sql-server-2008,hierarchyid,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=@sc

我在尝试运行将结果插入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)+'
数据类型的as
运算符无效。运算符等于add,类型等于hierarchyid。


有人能帮我传递层次结构值吗?你正在尝试创建一个可以作为语句执行的字符串。因此,您需要将您的hierarchyid改为nvarchar(max)


试试看:
@pathhere.ToString()

这个问题不清楚。您可以设置查询格式吗?但是如果我使用此im getting,在程序集“Microsoft.SqlServer.Types”中找不到类型“Microsoft.SqlServer.Types.SqlHierarchyId”的方法“Tostring”,所以您要用
“+@pathhere.Tostring()+”
”替换您所说的导致错误的部分,对吗?这给了您什么:选择路径,Path.Tostring()从SectionDetails,其中Sectionid=@Sectionid和SectionName=@SectionName和Batchid=@Batchid和Deptid=@Deptid和Schoolid=@Schoolid创建过程复制学校(@tablename varchar(500),@id varchar(500),@name varchar(500))作为开始声明@pathhere hierarchyid declare@sql varchar(max)从SchoolDetails中选择@pathhere=Path,其中Schoolid=@id set@sql='insert到'++@tablename+'(Schoolid,SchoolName,School_Ownerid,School_OwnerName,Path)值('++@id+','++@name+','++@pathhere+')exec(@sql)end我需要如何在动态传递表名时传递层次结构值Path(即)@pathhere以插入stmt。我需要传递path参数的格式。请查看SectionDetails中的
选择path,path.ToString(),并查看它显示了什么。您想创建一个字符串来处理内容。将
exec(@sql)
更改为
选择@sql
,这样您就可以看到它试图运行的内容。