How can I create an Oracle stored procedure which accepts a variable number of parameter values used to feed a IN clause?
如何创建一个Oracle存储过程,该过程接受用于提供IN子句的可变数量的参数值?
This is what I am trying to achieve. I do not know how to declare in PLSQL for passing a variable list of primary keys of the rows I want to update.
这就是我想要实现的目标。我不知道如何在PLSQL中声明传递我想要更新的行的主键的变量列表。
FUNCTION EXECUTE_UPDATE
( <parameter_list>
value IN int)
RETURN int IS
BEGIN
[...other statements...]
update table1 set col1 = col1 - value where id in (<parameter_list>)
RETURN SQL%ROWCOUNT ;
END;
Also, I would like to call this procedure from C#, so it must be compatible with .NET capabilities.
另外,我想从C#调用此过程,因此它必须与.NET功能兼容。
Thanks, Robert
7 个解决方案
#1
25
Using CSV is probably the simplest way, assuming you can be 100% certain that your elements won't themselves contain strings.
使用CSV可能是最简单的方法,假设您可以100%确定您的元素本身不包含字符串。
An alternative, and probably more robust, way of doing this is to create a custom type as a table of strings. Supposing your strings were never longer than 100 characters, then you could have:
另一种可能更强大的方法是创建一个自定义类型作为字符串表。假设您的字符串永远不会超过100个字符,那么您可以:
CREATE TYPE string_table AS TABLE OF varchar2(100);
You can then pass a variable of this type into your stored procedure and reference it directly. In your case, something like this:
然后,您可以将此类型的变量传递到存储过程并直接引用它。在你的情况下,这样的事情:
FUNCTION EXECUTE_UPDATE(
identifierList string_table,
value int)
RETURN int
IS
BEGIN
[...other stuff...]
update table1 set col1 = col1 - value
where id in (select column_value from table(identifierList));
RETURN SQL%ROWCOUNT;
END
The table()
function turns your custom type into a table with a single column "COLUMN_VALUE", which you can then treat like any other table (so do joins or, in this case, subselects).
table()函数将您的自定义类型转换为具有单个列“COLUMN_VALUE”的表,然后您可以像处理任何其他表一样处理(所以请加入或在本例中为子选择)。
The beauty of this is that Oracle will create a constructor for you, so when calling your stored procedure you can simply write:
这样做的好处是Oracle将为您创建一个构造函数,因此在调用存储过程时,您只需编写:
execute_update(string_table('foo','bar','baz'), 32);
I'm assuming that you can handle building up this command programatically from C#.
我假设您可以通过C#以编程方式处理构建此命令。
As an aside, at my company we have a number of these custom types defined as standard for lists of strings, doubles, ints and so on. We also make use of Oracle JPublisher to be able to map directly from these types into corresponding Java objects. I had a quick look around but I couldn't see any direct equivalents for C#. Just thought I'd mention it in case Java devs come across this question.
顺便说一句,在我的公司,我们有许多这些自定义类型被定义为字符串,双精度,整数等列表的标准。我们还使用Oracle JPublisher能够直接从这些类型映射到相应的Java对象。我快速浏览了一下,但是我看不到C#的直接等价物。我以为我会提到它,以防Java开发人员遇到这个问题。
#2
2
I found the following article by Mark A. Williams which I thought would be a useful addition to this thread. The article gives a good example of passing arrays from C# to PL/SQL procs using associative arrays (TYPE myType IS TABLE OF mytable.row%TYPE INDEX BY PLS_INTEGER):
我找到了Mark A. Williams的以下文章,我认为这篇文章对这个帖子很有帮助。本文提供了一个使用关联数组将数组从C#传递到PL / SQL过程的一个很好的示例(TYPE myType IS TABLE OF mytable.row%TYPE INDEX BY PLS_INTEGER):
Great article by Mark A. Williams
马克A.威廉姆斯的伟大文章
#3
1
I think there is no direct way to create procedures with variable number of parameters. However there are some, at least partial solutions to the problem, described here.
我认为没有直接的方法来创建具有可变数量参数的过程。然而,这里描述的问题有一些,至少是部分解决方案。
- If there are some typical call types procedure overloading may help.
- If there is an upper limit in the number of parameters (and their type is also known in advance), default values of parameters may help.
- The best option is maybe the usage of cursor variables what are pointers to database cursors.
如果有一些典型的调用类型,则重载过程可能有所帮助。
如果参数数量存在上限(并且其类型也是预先知道的),则参数的默认值可能会有所帮助。
最好的选择可能是使用游标变量什么是指向数据库游标的指针。
Unfortunately I have no experience with .NET environments.
不幸的是,我没有.NET环境的经验。
#4
0
Why not just use a long parameter list and load the values into a table constructor? Here is the SQL/PSM for this trick
为什么不使用长参数列表并将值加载到表构造函数中?这是这个技巧的SQL / PSM
UPDATE Foobar
SET x = 42
WHERE Foobar.keycol
IN (SELECT X.parm
FROM (VALUES (in_p01), (in_p02), .., (in_p99)) X(parm)
WHERE X.parm IS NOT NULL);
#5
-1
Why does it have to be a stored procedure? You could build up a prepared statement programmatically.
为什么它必须是存储过程?您可以以编程方式构建预准备语句。
#6
-1
I've not done it for Oracle, but with SQL Server you can use a function to convert a CSV string into a table, which can then be used in an IN clause. It should be straight-forward to re-write this for Oracle (I think!)
我没有为Oracle做过,但是使用SQL Server,您可以使用函数将CSV字符串转换为表格,然后可以在IN子句中使用。应该直接为Oracle重写这个(我想!)
CREATE Function dbo.CsvToInt ( @Array varchar(1000))
returns @IntTable table
(IntValue nvarchar(100))
AS
begin
declare @separator char(1)
set @separator = ','
declare @separator_position int
declare @array_value varchar(1000)
set @array = @array + ','
while patindex('%,%' , @array) <> 0
begin
select @separator_position = patindex('%,%' , @array)
select @array_value = left(@array, @separator_position - 1)
Insert @IntTable
Values (Cast(@array_value as nvarchar))
select @array = stuff(@array, 1, @separator_position, '')
end
return
end
Then you can pass in a CSV string (e.g. '0001,0002,0003') and do something like
然后你可以传入一个CSV字符串(例如'0001,0002,0003')并执行类似的操作
UPDATE table1 SET
col1 = col1 - value
WHERE id in (SELECT * FROM csvToInt(@myParam))
#7
-1
There is an article on the AskTom web site that shows how to create a function to parse the CSV string and use this in your statement in a similar way to the SQL Server example given.
AskTom网站上有一篇文章,展示了如何创建一个解析CSV字符串的函数,并在语句中以与给定的SQL Server示例类似的方式使用它。
See Asktom
#1
25
Using CSV is probably the simplest way, assuming you can be 100% certain that your elements won't themselves contain strings.
使用CSV可能是最简单的方法,假设您可以100%确定您的元素本身不包含字符串。
An alternative, and probably more robust, way of doing this is to create a custom type as a table of strings. Supposing your strings were never longer than 100 characters, then you could have:
另一种可能更强大的方法是创建一个自定义类型作为字符串表。假设您的字符串永远不会超过100个字符,那么您可以:
CREATE TYPE string_table AS TABLE OF varchar2(100);
You can then pass a variable of this type into your stored procedure and reference it directly. In your case, something like this:
然后,您可以将此类型的变量传递到存储过程并直接引用它。在你的情况下,这样的事情:
FUNCTION EXECUTE_UPDATE(
identifierList string_table,
value int)
RETURN int
IS
BEGIN
[...other stuff...]
update table1 set col1 = col1 - value
where id in (select column_value from table(identifierList));
RETURN SQL%ROWCOUNT;
END
The table()
function turns your custom type into a table with a single column "COLUMN_VALUE", which you can then treat like any other table (so do joins or, in this case, subselects).
table()函数将您的自定义类型转换为具有单个列“COLUMN_VALUE”的表,然后您可以像处理任何其他表一样处理(所以请加入或在本例中为子选择)。
The beauty of this is that Oracle will create a constructor for you, so when calling your stored procedure you can simply write:
这样做的好处是Oracle将为您创建一个构造函数,因此在调用存储过程时,您只需编写:
execute_update(string_table('foo','bar','baz'), 32);
I'm assuming that you can handle building up this command programatically from C#.
我假设您可以通过C#以编程方式处理构建此命令。
As an aside, at my company we have a number of these custom types defined as standard for lists of strings, doubles, ints and so on. We also make use of Oracle JPublisher to be able to map directly from these types into corresponding Java objects. I had a quick look around but I couldn't see any direct equivalents for C#. Just thought I'd mention it in case Java devs come across this question.
顺便说一句,在我的公司,我们有许多这些自定义类型被定义为字符串,双精度,整数等列表的标准。我们还使用Oracle JPublisher能够直接从这些类型映射到相应的Java对象。我快速浏览了一下,但是我看不到C#的直接等价物。我以为我会提到它,以防Java开发人员遇到这个问题。
#2
2
I found the following article by Mark A. Williams which I thought would be a useful addition to this thread. The article gives a good example of passing arrays from C# to PL/SQL procs using associative arrays (TYPE myType IS TABLE OF mytable.row%TYPE INDEX BY PLS_INTEGER):
我找到了Mark A. Williams的以下文章,我认为这篇文章对这个帖子很有帮助。本文提供了一个使用关联数组将数组从C#传递到PL / SQL过程的一个很好的示例(TYPE myType IS TABLE OF mytable.row%TYPE INDEX BY PLS_INTEGER):
Great article by Mark A. Williams
马克A.威廉姆斯的伟大文章
#3
1
I think there is no direct way to create procedures with variable number of parameters. However there are some, at least partial solutions to the problem, described here.
我认为没有直接的方法来创建具有可变数量参数的过程。然而,这里描述的问题有一些,至少是部分解决方案。
- If there are some typical call types procedure overloading may help.
- If there is an upper limit in the number of parameters (and their type is also known in advance), default values of parameters may help.
- The best option is maybe the usage of cursor variables what are pointers to database cursors.
如果有一些典型的调用类型,则重载过程可能有所帮助。
如果参数数量存在上限(并且其类型也是预先知道的),则参数的默认值可能会有所帮助。
最好的选择可能是使用游标变量什么是指向数据库游标的指针。
Unfortunately I have no experience with .NET environments.
不幸的是,我没有.NET环境的经验。
#4
0
Why not just use a long parameter list and load the values into a table constructor? Here is the SQL/PSM for this trick
为什么不使用长参数列表并将值加载到表构造函数中?这是这个技巧的SQL / PSM
UPDATE Foobar
SET x = 42
WHERE Foobar.keycol
IN (SELECT X.parm
FROM (VALUES (in_p01), (in_p02), .., (in_p99)) X(parm)
WHERE X.parm IS NOT NULL);
#5
-1
Why does it have to be a stored procedure? You could build up a prepared statement programmatically.
为什么它必须是存储过程?您可以以编程方式构建预准备语句。
#6
-1
I've not done it for Oracle, but with SQL Server you can use a function to convert a CSV string into a table, which can then be used in an IN clause. It should be straight-forward to re-write this for Oracle (I think!)
我没有为Oracle做过,但是使用SQL Server,您可以使用函数将CSV字符串转换为表格,然后可以在IN子句中使用。应该直接为Oracle重写这个(我想!)
CREATE Function dbo.CsvToInt ( @Array varchar(1000))
returns @IntTable table
(IntValue nvarchar(100))
AS
begin
declare @separator char(1)
set @separator = ','
declare @separator_position int
declare @array_value varchar(1000)
set @array = @array + ','
while patindex('%,%' , @array) <> 0
begin
select @separator_position = patindex('%,%' , @array)
select @array_value = left(@array, @separator_position - 1)
Insert @IntTable
Values (Cast(@array_value as nvarchar))
select @array = stuff(@array, 1, @separator_position, '')
end
return
end
Then you can pass in a CSV string (e.g. '0001,0002,0003') and do something like
然后你可以传入一个CSV字符串(例如'0001,0002,0003')并执行类似的操作
UPDATE table1 SET
col1 = col1 - value
WHERE id in (SELECT * FROM csvToInt(@myParam))
#7
-1
There is an article on the AskTom web site that shows how to create a function to parse the CSV string and use this in your statement in a similar way to the SQL Server example given.
AskTom网站上有一篇文章,展示了如何创建一个解析CSV字符串的函数,并在语句中以与给定的SQL Server示例类似的方式使用它。
See Asktom