繁体   English   中英

如何将数组/列表从程序集返回到SQL Server中的存储过程?

[英]How to return an array/list from an assembly to a stored procedure in SQL Server?

我有一个存储过程,该过程接受一个字符串并提取以'@'开头的子字符串。 我决定为此使用C#DLL,并使用SQL程序集访问此DLL。 因此,我在C#中开发了以下方法:

[Microsoft.SqlServer.Server.SqlFunction]
public static ? GetSubStrings(string text)
{
       List<string> IDs=new List<string>();
       foreach (Match match in Regex.Matches(text, @"(?<!\w)@\w+"))
       {
           try
           {
                IDs.Add(match.Value.Replace("@", ""));
           }
           catch (NullReferenceException) { continue; }
       }
       return IDs;
}

我加了“?” 请在返回类型的位置签名,因为我不知道此方法应返回哪种返回类型? 请告诉我解决方案。 我找不到任何存储此类数据的SQL类型。

为此,您需要使函数成为表值函数 ,这将使函数返回表而不是单个结果。 为此,您需要使用TableDefinition属性,并将返回类型设置为IEnumerable

[Microsoft.SqlServer.Server.SqlFunction(TableDefinition="IDs nvarchar(max)"]
public static IEnumerable GetSubStrings(string text)
{
       List<string> IDs=new List<string>();
       foreach (Match match in Regex.Matches(text, @"(?<!\w)@\w+"))
       {
           try
           {
                IDs.Add(match.Value.Replace("@", ""));
           }
           catch (NullReferenceException) { continue; }
       }
       return IDs;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM