无法使用 TVF 结果更新 table

Cannot Update a table with TVF results

我有一个复杂的 CLR 函数,returns 一行。我可以使用交叉应用到 select 结果,效果很好,但更新或插入失败。 select 始终有效,但是插入 select 和更新失败并出现以下错误,此错误不会出现在 select 查询中。

Msg 6522, Level 16, State 1, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate "AppendCLR": 
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ArgumentOutOfRangeException: 
   at System.Collections.ArrayList.get_Item(Int32 index)
   at AppendCLR.ProcessData(String EnrollmentID,String InstituteCode)
   at AppendCLR.UserDefinedFunctions(SqlString EnrollmentID,SqlString InstituteCode)
.
The statement has been terminated.

TestCLR 是我需要用 CLR 函数 AppendCLR 的结果更新的 table。 #TestCLR_TEMP@TestCLR_TEMP 分别是临时变量和 table 变量。

这行不通:

update a set
a.ID=b.ID,
a.Branch=b.Branch,
a.CourseID=b.CourseID, 
a.EnrolDate=b.EnrolDate, 
a.CourseTag=b.CourseTag, 
a.Micro=b.Micro, 
a.StudentTag=b.StudentTag FROM TestCLR a cross apply 
dbo.AppendCLR([EnrollmentID],[InstituteCode]) b

这也行不通:

select *  into #TestCLR_TEMP from  dbo.AppendCLR([EnrollmentID],[InstituteCode])

select *  into TestCLR_TEMP from  dbo.AppendCLR([EnrollmentID],[InstituteCode])

然而这是可行的:

declare table @TestCLR_TEMP (ID bigint,
Branch varchar(100),
CourseID bigint,
EnrolDate DateTime,
CourseTag varchar(100),
Micro varchar(100),
StudentTag varchar(100))

insert into @TestCLR_TEMP select 
a.ID,
b.Branch,
b.CourseID, 
b.EnrolDate, 
b.CourseTag, 
b.Micro, 
b.StudentTag FROM TestCLR  a cross apply 
dbo.AppendCLR([EnrollmentID],[InstituteCode]) b
--I have now my results in @TestCLR_TEMP, I can perform an inner join to append these results back to my original table TestCLR

这总是有效的:

Select * from dbo.AppendCLR(123,456)

基本上所有插入,当我在 SSMS 中尝试真正的 tables.The 错误时更新都失败并没有说明任何重要的事情,但如果需要我可以 post 它。 谢谢!

我觉得有点傻,但最初的错误是 Transaction Context In Use By Another Session,同时将 select 查询的结果读取到 DataTable 中,而我在读取这个空白时得到 System.ArgumentOutOfRangeException DataTable

修复是在连接字符串中使用 Enlist=false

我知道我应该专注于错误报告,但感谢你们的努力。