IBM DB2准备语句中的标量函数参数

huangapple go评论71阅读模式
英文:

IBM DB2 Scalar Function Parameters in Prepared Statement

问题

我有一个db2表t1,其中一个列是通过使用一个接受两个字符串参数的标量函数来填充的。

function(string foo, string bar)

在准备的SQL代码如下:

UPDATE t1 SET col1 = function(?, 'foobar') WHERE t1.id = ?

当我现在向准备的语句中添加一个参数时,我收到一个错误,指出第一个参数的类型错误。

for (Entry entry : entries) {
    preparedStatement.setString(1, entry.getDesc());
    preparedStatement.setInt(2, entry.getId());
}

我已经尝试过将第一个参数用单引号括起来,但没有起作用。是否还有其他人遇到过这个问题并找到了解决方案?
主要问题是我不确定db2引擎如何为标量函数准备第一个字符串参数,以至于首先引发错误。

英文:

I have a db2 table t1 where one of the columns is populated by utilizing a scalar function that takes two string params.

function(string foo, string bar)

In prepare the sql code as follows:

UPDATE t1 SET col1 = function(?, 'foobar') WHERE t1.id = ?

When i now add a parameter to the prepared statement i get an error that the first parameter is of the wrong type.

for (Entry entry : entries) {
    preparedStatement.setString(1, entry.getDesc());
    preparedStatement.setInt(2, entry.getId());
}

I already tried wrapping the first param in single quotes but that didn't work. Did anyone else have this problem and has come to a solution?
The main problem is that i'm now sure how the db2 engine prepares the first string parameter for the scalar function so that the error is thrown in the first place.

答案1

得分: 0

尝试使用 function(cast(? as varchar(xxx)), 'foobar'),其中 xxx 是函数第一个字符串参数的相应长度。在这种情况下,函数的第一个参数必须是 varchar 数据类型。如果它的第一个参数是另一种数据类型,则请使用相应的转换和 set<Datatype>。你需要这样做,因为可能会有多个具有相同名称和参数数量的函数,但数据类型不同。在查询编译(准备)期间,Db2 必须确切地知道要使用哪个函数。

英文:

Try function(cast(? as varchar(xxx)), &#39;foobar&#39;), where xxx the corresponding length of the 1-st string parameter of the function. The function's 1-st parameter must be of varchar data type in this case. If its 1-st parameter is of another data type, then use the corresponding cast and set&lt;Datatype&gt;.
You need to do it, since there may be multiple functions with the same name and number of parameters, but with different data types. And Db2 must know which function to use exactly during the query compilation (prepare).

huangapple
  • 本文由 发表于 2020年8月26日 01:45:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63584434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定