如何定义 PL/SQL 函数并从另一种语言调用它
How to define a PL/SQL function and call it from another language
我正在尝试编写一个 PL/SQL 函数来 returns 一个数字。我必须使用 SQL where 子句中的数字。函数如下:
CREATE OR REPLACE FUNCTION func_id(str_id IN STRING,num_group IN NUMBER)
RETURN NUMBER
IS
result NUMBER;
declare
temp STRING;
BEGIN
temp := substr(str_id, -least(length(str_id), 2));
result := TO_NUMBER(temp) % num_group;
RETURN result;
END;
select * from table where func_id("id",2)=1
2 和 1 只是一个例子。我想在我的 Scala 程序中调用该函数,该函数替换了变量 2 和 1.
当我 运行 SQL Developer 中的代码时,我收到此错误:
Function FUNC_ID compiled
LINE/COL ERROR
--------- -------------------------------------------------------------
5/1 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following: begin function pragma procedure subtype type <an identifier> <a double-quoted delimited-identifier> current cursor delete exists prior The symbol "begin" was substituted for "DECLARE" to continue.
13/54 PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: . , @ ; for <an identifier> <a double-quoted delimited-identifier> group having intersect minus order partition start subpartition union where connect sample
能否请您指导我如何编写 PL/SQL 函数并在另一个查询或其他任何地方调用它?
非常感谢任何帮助。
DECLARE
在该位置在句法上无效;
STRING
不是有效的数据类型,您需要 VARCHAR2
;
%
不是有效的运算符,您需要 MOD
函数;和
- 不需要中间变量。
CREATE OR REPLACE FUNCTION func_id(
str_id IN VARCHAR2,
num_group IN NUMBER
) RETURN NUMBER
IS
BEGIN
RETURN MOD(TO_NUMBER(substr(str_id, -LEAST(2, LENGTH(str_id)))), num_group);
END;
/
那么,如果你有 table:
CREATE TABLE table_name ( id ) AS
SELECT '1' FROM DUAL UNION ALL
SELECT '24' FROM DUAL UNION ALL
SELECT '10-05' FROM DUAL;
您可以使用以下方式调用该函数:
select id,
func_id(id, 23)
from table_name
输出:
ID
FUNC_ID(ID,23)
1
1
24
1
10-05
5
db<>fiddle here
我正在尝试编写一个 PL/SQL 函数来 returns 一个数字。我必须使用 SQL where 子句中的数字。函数如下:
CREATE OR REPLACE FUNCTION func_id(str_id IN STRING,num_group IN NUMBER)
RETURN NUMBER
IS
result NUMBER;
declare
temp STRING;
BEGIN
temp := substr(str_id, -least(length(str_id), 2));
result := TO_NUMBER(temp) % num_group;
RETURN result;
END;
select * from table where func_id("id",2)=1
2 和 1 只是一个例子。我想在我的 Scala 程序中调用该函数,该函数替换了变量 2 和 1.
当我 运行 SQL Developer 中的代码时,我收到此错误:
Function FUNC_ID compiled
LINE/COL ERROR
--------- -------------------------------------------------------------
5/1 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following: begin function pragma procedure subtype type <an identifier> <a double-quoted delimited-identifier> current cursor delete exists prior The symbol "begin" was substituted for "DECLARE" to continue.
13/54 PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: . , @ ; for <an identifier> <a double-quoted delimited-identifier> group having intersect minus order partition start subpartition union where connect sample
能否请您指导我如何编写 PL/SQL 函数并在另一个查询或其他任何地方调用它?
非常感谢任何帮助。
DECLARE
在该位置在句法上无效;STRING
不是有效的数据类型,您需要VARCHAR2
;%
不是有效的运算符,您需要MOD
函数;和- 不需要中间变量。
CREATE OR REPLACE FUNCTION func_id(
str_id IN VARCHAR2,
num_group IN NUMBER
) RETURN NUMBER
IS
BEGIN
RETURN MOD(TO_NUMBER(substr(str_id, -LEAST(2, LENGTH(str_id)))), num_group);
END;
/
那么,如果你有 table:
CREATE TABLE table_name ( id ) AS
SELECT '1' FROM DUAL UNION ALL
SELECT '24' FROM DUAL UNION ALL
SELECT '10-05' FROM DUAL;
您可以使用以下方式调用该函数:
select id,
func_id(id, 23)
from table_name
输出:
ID FUNC_ID(ID,23) 1 1 24 1 10-05 5
db<>fiddle here