2010-01-27 57 views
1

当试图运行将结果插入到sql表中的查询时,出现此错误。 即时传递表名作为参数,如何将层次结构值赋予插入语句。错误:使用storedprocedure插入层次结构值

这里是我的代码:

declare @pathhere hierarchyid 

select @pathhere=Path from SectionDetails where [email protected] and [email protected] and [email protected] and [email protected] and [email protected] 

插入语句:

set @sqlstmt = 'insert into '[email protected]+'(StudentID, StudentName,SectionID,SectionName,BatchID,BatchName, DeptID,DeptName, SchoolID,Path) 
values('''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''+ @deptid+''','''[email protected]+''', '''[email protected]+''','+ CAST(@pathhere as hierarchyid)+')' 
exec(@sqlstmt) 

IM这一行收到错误:

'+ CAST(@pathhere as hierarchyid)+' 

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

任何人都可以请帮助我出去了如何传递层次结构值

+0

此问题尚不清楚。你能格式化你的查询吗? – Galkin 2010-01-27 05:28:57

回答

0

您试图创建一个可以作为语句执行的字符串。所以你需要把你的hierarchyid改成nvarchar(max)。

尝试:@pathhere.ToString()

+0

但如果我使用这即时获取 找不到方法'Tostring'类型'Microsoft.SqlServer.Types.SqlHierarchyId'在程序集'Microsoft.SqlServer.Types' – Innova 2010-01-27 05:33:29

+0

因此,你要更换你说的部分是造成你+'pathhere.ToString()+''出错,对吧? – 2010-01-27 05:54:46

+0

感谢您的回复..ya – Innova 2010-01-27 06:08:27