简体   繁体   English

如何传递数组并将其返回给存储过程?

[英]How to pass and Return a Array to stored procedure?

我想知道如何将数组传递给存储过程,该存储过程又将从c#.net返回数组?

There are different options here depending on the scenario. 根据情况,这里有不同的选项。 I'm using SQL Server for a lot of the examples below, but much of it is broadly transferable between systems. 我在下面的许多示例中都使用SQL Server,但是其中很多可以在系统之间广泛移植。

For a relatively small array (ideally vector) you can construct a delimited string (tab delimited, comma delimited, whatever), and pass that into your DB and parse - usually manually (DBMS often lack a "split" routine), but it is very easy to obtain a pre-written "split" implementation (for example, as a UDF in SQL Server). 对于相对较小的数组(理想的向量),您可以构造一个定界的字符串(制表符定界,逗号定界,等等),并将其传递到数据库中并进行解析-通常是手动进行的(DBMS通常缺少“拆分”例程),但是非常容易获得预先编写的“拆分”实现(例如,作为SQL Server中的UDF)。 Typical usage: 典型用法:

SELECT st.*
FROM dbo.SplitUDF(@myarg) #udf
INNER JOIN SOME_TABLE st ON st.ID = #udf.Value

Xml is another option, especially for complex data; Xml是另一种选择,特别是对于复杂数据; SQL Server 2005 and above has inbuilt xml parsing, but that should not be assumed in general. SQL Server 2005及更高版本具有内置的xml解析功能,但通常不应假定这样做。

Table-valued parameters are another option, but this is SQL Server 2008 only - it might well be what you are looking for, though. 表值参数是另一个选择,但这仅是SQL Server 2008-尽管它很可能是您要寻找的。

Another option, especially for large data, is to pump the data into the server separately (bulk insert, SQLBulkCopy , "bcp", SSIS whatever) and process the data via SQL once it is there. 另一个选择(特别是对于大数据)是将数据分别泵入服务器(批量插入, SQLBulkCopy ,“ bcp”,SSIS等),并在存在数据后通过SQL处理数据。

To get array/tabular data out, a standard SELECT should be your default option, although you can of course also construct xml or delimited character data. 为了获取数组/表格数据,标准的SELECT应该是您的默认选项,尽管您当然也可以构造xml或分隔字符数据。 The latter can be accomplished via a quirk of SQL: 后者可以通过SQL的古怪来完成:

DECLARE @foo varchar(max)
SET @foo = ''
SELECT @foo = @foo + st.SomeColumn + '|' -- pipe-delimited, note trailing |
FROM SOME_TABLE st

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

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