ここでのこの質問に関してSQL CLR return two new columns私は単純な SQL CLR 関数を作成しようとしています。そこでは、関数に 2 つの文字列を渡すことができ、2 つの NEW 列が返されます。
だから私は次のデータを持っているとしましょう:-
Col A Col B
Bob Joe
Jane John
Col A と Col B を CLR 関数に渡して、次のようなものを返すようにしたい (Col C と D は新しい列です):-
Col A Col B Col C Col D
Bob Joe BobCLR JoeCLR
Jane John JaneCLR JohnCLR
私は次のコードを持っています:-
[SqlFunction(FillRowMethodName = "FillRow")]
public static IEnumerable MyCLRFunction(string A, string B)
{
String[] values = new String[2];
values[0] = A+"CLR";
values[1]= B+"CLR";
return values;
}
private static void FillRow(Object obj, out string C, out string D)
{
String[] row = (object[])obj;
C = (string)row[0];
D = (string)row[1];
}
CREATE ASSEMBLY を使用して、アセンブリを SQL Server に登録できます。
次のように、SQL Server で関数 ok を作成できます。
CREATE FUNCTION dbo.MyCLRFunction(@a [nvarchar](4000), @b [nvarchar](4000))
RETURNS TABLE
(c [nvarchar](4000) null, d [nvarchar](4000) null) with execute as caller
AS
EXTERNAL NAME [MyNamespace].[CLRFunctions].[MyCLRFunction]
しかし、私がするとき: -
SELECT * FROM MyCLRFunction('Bob','Joe')
私は得ています:-
Msg 6260, Level 16, State 1, Line 1
An error occurred while getting new row from user defined Table Valued Function :
System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.String[]'.
System.InvalidCastException:
at CLRFunctions.FillRow(Object obj, String& C, String& D)