1

ここでのこの質問に関して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)
4

2 に答える 2

1

私はそのように CLR sproc を実行したことがありません。アトミック文字列ではなく、文字列配列を返しているようです。

RETURNS TABLE (c nvarchar null、d nvarchar null)

繰り返しますが、一見すると、上記の c と d は配列要素ではなく、文字列を想定しています。

于 2012-05-04T15:16:00.813 に答える
1

Ienumerable 内の KeyValuePair を使用して、これを行うことができました。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public class CLRFunctions
{

    private static IEnumerable<KeyValuePair<double, double>> CoordinatesEnumerable(double Lat, double Long)
    {
        return new Dictionary<double, double> { { Lat, Long } };
    }

    [SqlFunction(FillRowMethodName = "FillRow")]
    public static IEnumerable ToLatLong(double East, double North)
    {
        return CoordinatesEnumerable(East, North);
    }

    private static void FillRow(Object obj, out SqlDouble Lat, out SqlDouble Long)
    {
        KeyValuePair<double, double> Coordinates = (KeyValuePair<double, double>)obj;
        Lat = new SqlDouble(Coordinates.Key);
        Long = new SqlDouble(Coordinates.Value);
    }

}
于 2012-05-08T10:14:02.400 に答える