使用参数在 DB2 中创建函数时出错
Error creating function in DB2 with params
我对 db2 中的函数有疑问
函数查找一条记录,returns根据第一个和第二个是否被用户记录的数字
函数内的查询是这样的
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = '0010111003')
) AS T WHERE POLLIFE = '0010111003'
并且工作完美
我用这段代码创建函数
CREATE FUNCTION LIBWEB.BNOWPAPOL(POL CHAR)
RETURNS DECIMAL(7,2)
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN (
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = POL)
) AS T WHERE POLLIFE = POL
)
命令运行正常
WARNING: 17:55:40 [CREATE - 0 row(s), 0.439 secs] Command processed.
No rows were affected
当我想执行查询时出现错误
SELECT LIBWEB.BNOWPAPOL('0010111003') FROM DATAS.DUMMY -- dummy has only one row
我明白了
[Error Code: -204, SQL State: 42704] [SQL0204] BNOWPAPOL in LIBWEB
type *N not found.
我检测到,当我删除参数时,函数工作正常!
使用此代码
CREATE FUNCTION LIBWEB.BNOWPAPOL()
RETURNS DECIMAL(7,2)
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN (
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = '0010111003')
) AS T WHERE POLLIFE = '0010111003'
)
为什么?
这条语句:
SELECT LIBWEB.BNOWPAPOL('0010111003') FROM DATAS.DUMMY
导致此错误:
[Error Code: -204, SQL State: 42704] [SQL0204] BNOWPAPOL in LIBWEB
type *N not found.
传递给 BNOWPAPOL() 函数的参数值作为带引号的字符串提供,没有定义(无 CAST)。 SELECT 语句假定它是一个 VARCHAR 值,因为可能随时给出不同长度的字符串并将其作为 VARCHAR 传递给服务器。
原始函数定义说:
CREATE FUNCTION LIBWEB.BNOWPAPOL(POL CHAR)
函数签名是为单字节 CHAR 生成的。 (可以重载函数定义以处理不同的输入,并使用签名来区分函数版本。)
由于从客户端传递了一个VARCHAR,而服务器只找到了一个CHAR函数版本,所以返回的错误适合。更改函数定义或 CASTing 为匹配类型可以解决此类问题。 (请注意,如果值为 CAST,则 CHAR(1) 参数只能正确处理单字符输入。)
我对 db2 中的函数有疑问
函数查找一条记录,returns根据第一个和第二个是否被用户记录的数字
函数内的查询是这样的
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = '0010111003')
) AS T WHERE POLLIFE = '0010111003'
并且工作完美
我用这段代码创建函数
CREATE FUNCTION LIBWEB.BNOWPAPOL(POL CHAR)
RETURNS DECIMAL(7,2)
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN (
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = POL)
) AS T WHERE POLLIFE = POL
)
命令运行正常
WARNING: 17:55:40 [CREATE - 0 row(s), 0.439 secs] Command processed. No rows were affected
当我想执行查询时出现错误
SELECT LIBWEB.BNOWPAPOL('0010111003') FROM DATAS.DUMMY -- dummy has only one row
我明白了
[Error Code: -204, SQL State: 42704] [SQL0204] BNOWPAPOL in LIBWEB type *N not found.
我检测到,当我删除参数时,函数工作正常!
使用此代码
CREATE FUNCTION LIBWEB.BNOWPAPOL()
RETURNS DECIMAL(7,2)
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN (
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = '0010111003')
) AS T WHERE POLLIFE = '0010111003'
)
为什么?
这条语句:
SELECT LIBWEB.BNOWPAPOL('0010111003') FROM DATAS.DUMMY
导致此错误:
[Error Code: -204, SQL State: 42704] [SQL0204] BNOWPAPOL in LIBWEB type *N not found.
传递给 BNOWPAPOL() 函数的参数值作为带引号的字符串提供,没有定义(无 CAST)。 SELECT 语句假定它是一个 VARCHAR 值,因为可能随时给出不同长度的字符串并将其作为 VARCHAR 传递给服务器。
原始函数定义说:
CREATE FUNCTION LIBWEB.BNOWPAPOL(POL CHAR)
函数签名是为单字节 CHAR 生成的。 (可以重载函数定义以处理不同的输入,并使用签名来区分函数版本。)
由于从客户端传递了一个VARCHAR,而服务器只找到了一个CHAR函数版本,所以返回的错误适合。更改函数定义或 CASTing 为匹配类型可以解决此类问题。 (请注意,如果值为 CAST,则 CHAR(1) 参数只能正确处理单字符输入。)