I have a complex CLR function which returns a single row. I can use Cross Apply to select results which works fine but an Update or Insert fails. The select always works however insert,select into and update fails with below error, this error doesnt shows up in select queries.
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
is the table which I need to update with the results of the CLR Function AppendCLR
. #TestCLR_TEMP
and @TestCLR_TEMP
are temp and table variables respectively.
This doesnt works :
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
This doesnt works too :
select * into #TestCLR_TEMP from dbo.AppendCLR([EnrollmentID],[InstituteCode])
OR
select * into TestCLR_TEMP from dbo.AppendCLR([EnrollmentID],[InstituteCode])
However this works :
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
This always works :
Select * from dbo.AppendCLR(123,456)
Basically all insert, updates fails when I try them on real tables.The error in SSMS doesnt says anything important but I can post it if needed. Thanks!