在 SELECT IN 中使用 Oracle 参数时出现问题
我在将一串数字插入 sql 查询时遇到问题
SELECT *
FROM tablename a
WHERE a.flokkurid IN (3857,3858,3863,3285)
ORDER BY sjodategund, rodun
...或者:
SELECT *
FROM tablename a
WHERE a.flokkurid IN (:strManyNumbers)
ORDER BY sjodategund, rodun
...使用此代码:
using (OracleCommand sel = new OracleCommand(SQL, connectionstring)) {
sel.Parameters.Add(":strManyNumbers",
OracleDbType.Varchar2,
"Client",
ParameterDirection.Input);
}
因此,如果我运行此查询,我会得到:
ORA-01722: 无效数字
,但如果我只插入一个数字,即“3857”,它将返回带有数据的查询“OK”。
I have a problem when inserting a string of numbers into sql query
SELECT *
FROM tablename a
WHERE a.flokkurid IN (3857,3858,3863,3285)
ORDER BY sjodategund, rodun
...or:
SELECT *
FROM tablename a
WHERE a.flokkurid IN (:strManyNumbers)
ORDER BY sjodategund, rodun
...with this code:
using (OracleCommand sel = new OracleCommand(SQL, connectionstring)) {
sel.Parameters.Add(":strManyNumbers",
OracleDbType.Varchar2,
"Client",
ParameterDirection.Input);
}
So if i run this query i get:
ORA-01722: invalid number
but if i insert just one number, i.e. "3857" it will return query OK with data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要传递一组值,您需要使用 Oracle 的表或数组类型。
首先,创建一个表类型(例如 NUMBER):
当您为查询创建参数时,将其声明为关联 PL/SQL 数组:
然后分配一些值:
并且您的查询需要强制转换:
To pass a set of values, you need to use Oracle's table or array types.
At first, you create a table type (e.g. for NUMBER):
When you create the parameter for the query, declare it as an associative PL/SQL array:
Then assign some values:
And your query needs a cast:
这不是参数的工作方式。您不能指定“集合”作为参数,您必须在字符串中组装 SQL 查询。并注意 SQL 注入。
此外,您可能还想看看这些:
更新
Codo 的回答对于 Oracle 有一个非常有趣的方法。我现在无法测试它,但它看起来确实很有希望。
这里有一个非常相似的问题: OracleParameter and IN Clause ,正如 @DCookie 所指出的。它不是精确的重复,因为当数组中项目的类型发生变化时,SQL 转换也会发生变化。
That's not how parameters work. You cannot specify a "set" as a parameter, you have to assemble the SQL query in the string. And watch out for SQL Injection.
In addition, you might want to take a look at these:
Update
Codo's answer has a very interesting approach for Oracle. I cannot test it right now, but it sure looks promising.
There's a very similar question here: OracleParameter and IN Clause , as pointed out by @DCookie. It's not an exact duplicate because when the type of the item in array changes, the SQL cast also changes.
希望这对你有用。
我见过其他一些代码也可以执行此操作。
中这要容易得多
就其价值而言,在 Microsoft SQL Server Harv Sather
Hope this works for you.
I have seen some other code that does this as well.
For what its worth, this is much easier to do in Microsoft SQL Server
Harv Sather