[英]Oracle/c#: How do i use bind variables with select statements to return multiple records?
I have a question regarding Oracle bind variables and select statements. 我对Oracle绑定变量和选择语句有疑问。
What I would like to achieve is do a select on a number different values for the primary key. 我想实现的是对多个不同的主键值进行选择。 I would like to pass these values via an array using bind values.
我想通过使用绑定值的数组传递这些值。
select * from tb_customers where cust_id = :1
int[] cust_id = { 11, 23, 31, 44 , 51 };
I then bind a DataReader
to get the values into a table. 然后,我绑定一个
DataReader
以将值放入表中。
The problem is that the resulting table only contains a single record (for cust_id=51
). 问题在于结果表仅包含一个记录(对于
cust_id=51
)。 Thus it seems that each statement is executed independently (as it should), but I would like the results to be available as a collective (single table). 因此,似乎每个语句都是独立执行的(应该如此),但是我希望结果可以作为一个集合(单个表)使用。
A workaround is to create a temporary table, insert all the values of cust_id
and then do a join against tb_customers
. 一种解决方法是创建一个临时表,插入
cust_id
所有值,然后对tb_customers
进行tb_customers
。 The problem with this approach is that I would require temporary tables for every different type of primary key, as I would like to use this against a number of tables (some even have combined primary keys). 这种方法的问题在于,我需要为每种不同类型的主键都使用临时表,因为我想对许多表(有些甚至组合主键)使用临时表。
Is there anything I am missing? 我有什么想念的吗?
Not asking the question as to why you would want to do this to begin with. 不问关于为什么要这样做的问题。 Shouldn't the sql statement be something like
sql语句不应该像
select * from tb_customers where cust_id = 11 or 23 or ...
Edit: 编辑:
I am limited in Oracle but when I look at the documentation I think that you might have to do something like this: 我受Oracle的限制,但是当我查看文档时,我认为您可能必须执行以下操作:
variable i number
exec :i := 11
select * from tb_customers where cust_id = :i
This would allow you to take advantage of binding. 这将使您能够利用绑定。 You will have to add each record return to your own collection since it will still only return one at a time.
您将必须将每个记录返回添加到您自己的集合中,因为它一次仍只会返回一个。
I know this was asked a while ago but not a brilliant answer. 我知道这是前一段时间提出的,但不是一个很好的答案。
I would do something like this - please excuse the crude psudo code 我会做这样的事情-请原谅伪装的伪代码
string bindList = "";
for(int ii=0;ii<cust_id.count;++ii)
{
if(ii == 0)
{
bindList += ":" + ii;
}
else
{
bindList += ",:" + ii;
}
OracleParameter param = new OracleParameter();
param.dbType = types.int;
param.value = cust_id[ii];
command.Parameters.Add(param);
}
query = "select * from tb_customers where cust_id in(" + bindList + ")";
So then query ends up having in(:1,:2,:3,etc) and each of these are bound separately. 因此,查询最终将具有in(:1,:2,:3,etc),并且每个都分别绑定。
There is also a similar question here: OracleParameter and IN Clause 这里还有一个类似的问题: OracleParameter和IN子句
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.