发送值的数组Oracle过程中使用WHERE IN子句 [英] Sending an array of values to Oracle procedure to use in WHERE IN clause

查看:984
本文介绍了发送值的数组Oracle过程中使用WHERE IN子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Oracle中的存储过程,如下所示:

I have a stored procedure in Oracle as shown below:

CREATE PROCEDURE MY_TEST_PROC(
  CUR OUT SYS_REFCURSOR,
  PARAM_THAT_WILL_BE _USED_INSIDE_WHERE_IN
)
AS
BEGIN
  OPEN CUR FOR 
    SELECT * 
      FROM MY_TABLE 
     WHERE COL1 IN (here I want to put values received from C#)
END;

在ASP.NET应用程序方面我有几个选项的选择要素。我想在我的WHERE子句中使用这些列表项。我知道我可以有一个VARCHAR2输入参数在我的存储过程,从列表中的项目做一个逗号分隔的字符串,其发送的程序。有两个问题一起去是这样的:

On the ASP.NET application side I have a select element with several options. I want to use these list items in my WHERE clause. I know that I can have a VARCHAR2 input parameter in my stored proc, make a comma separated string from the list items, send it to the procedure. There are two concerns with going this way:


  1. 我让我的网站容易受到SQL注入

  2. 在我的存储过程我必须使用EXECUTE(SELECT ...)的模式,我想避免的。

我怎样才能把这些列表项的存储过程和使用它们的WHERE IN子句里?我使用ODP.NET和听说过UDT的,但不知道如何使用它。

How can I send these list items to the stored procedure and use them inside the WHERE IN clause? I'm using ODP.NET and have heard of UDT but don't know how to use it.

推荐答案

您可以添加这个逗号分隔的输入参数为VARCHAR()和使用以下WHERE语句:

You can add this comma separated input parameter as a varchar() and use following where statement:

where (','||PARAM_THAT_WILL_BE||',' like '%,'||COL1||',%')

例如,如果 PARAM_THAT_WILL_BE ='2,3,4,5' COL1 = 3 我们得到:

for example if PARAM_THAT_WILL_BE='2,3,4,5' and col1=3 we get:

where (',2,3,4,5,' like '%,3,%') 

如果COL1值是在此列表中这是真的。
在这里,所以你可以避免担忧你不使用动态查询1)和2)。

and it's TRUE if COL1 value is in this list. Here you don't use a dynamic query so you avoid concerns 1) and 2).

这篇关于发送值的数组Oracle过程中使用WHERE IN子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆