delimiter $$ CREATE FUNCTION `getChildidList`(rootId varchar(38)) RETURNS text DETERMINISTIC BEGIN DECLARE sTemp text; DECLARE sTempChd text; SET@@group_concat_max_len = 102400; SET sTemp = '$'; SET sTempChd = rootId; WHILE sTempChd IS NOT NULL DO SET sTemp = concat(sTemp, ',', sTempChd); SELECT group_concat(ORGA001) INTO sTempChd FROM hrsw_t_organ WHERE FIND_IN_SET(ORGA002, sTempChd) > 0; END WHILE; SET@@group_concat_max_len = 1024; RETURN SUBSTRING(sTemp,3); END; $$ 使用Oracle重写,Oracle版本为12c
时间: 2024-02-15 20:27:56 浏览: 32
对于这个MySQL函数,可以使用以下语句在Oracle 12c中进行重写:
CREATE OR REPLACE FUNCTION getChildidList(rootId VARCHAR2)
RETURN VARCHAR2
DETERMINISTIC
IS
sTemp VARCHAR2(32767);
sTempChd VARCHAR2(32767);
BEGIN
sTemp := '$';
sTempChd := rootId;
WHILE sTempChd IS NOT NULL LOOP
sTemp := sTemp || ',' || sTempChd;
SELECT LISTAGG(ORGA001, ',') WITHIN GROUP (ORDER BY ORGA001) INTO sTempChd
FROM hrsw_t_organ
WHERE REGEXP_LIKE(sTempChd, '(^|,)'|| ORGA002 ||'(,|$)');
END LOOP;
RETURN SUBSTR(sTemp, 3);
END;
/
需要注意的是,Oracle没有像MySQL一样的group_concat函数,可以使用LISTAGG函数来代替。同时,Oracle中没有FIND_IN_SET函数,可以使用正则表达式来实现类似的功能。在上面的代码中,使用REGEXP_LIKE函数来判断sTempChd是否包含ORGA002。
相关问题
DELIMITER $$ CREATE FUNCTION fn_select_studentname(std_id INT) RETURNS VARCHAR(50) BEGIN DECLARE name VARCHAR(50); SELECT name INTO name FROM student WHERE id = std_id; RETURN name; END$$ DELIMITER ;
好的,你的第一个问题是一个 SQL 语句。这是一个创建一个名为 fn_select_studentname 的函数,输入参数为 std_id,返回类型为 VARCHAR(50)。函数的作用是查询出给定 std_id 对应的 student 表的 name 字段,并将其返回。这个函数是使用 MySQL 的语法编写的。其中 DELIMITER 是设置分隔符,目的是区分 SQL 语句中的分号和函数体中的分号。请问你还有其他需要了解的问题吗?
DELIMITER $$ use dbstudent$$ create function fn_select_studentname (student_id int) returns smallint begin declare student_name varchar(50); select student_name = name from student where id = student_id; return student_name; end$$ delimiter ;如何修改
这个函数使它不仅能够返回学生名字,还可以返回学生年龄?
要修改这个函数使它不仅能够返回学生名字,还可以返回学生年龄,需要进行以下几个步骤:
1. 首先,在函数的返回类型中将 smallint 改为一个更合适的类型,例如一个存储年龄的整数类型,比如 int。
2. 在函数中添加一个新的变量 age,用于存储年龄信息。
3. 在查询语句中添加一个新的字段 age,将查询结果放入 age 变量中。
4. 最后,将 age 变量作为函数返回值返回。
下面是修改后的函数代码:
DELIMITER $$
use dbstudent$$
create function fn_select_student_info (student_id int)
returns int
begin
declare student_name varchar(50);
declare age int;
select student_name = name, age = age
from student
where id = student_id;
return age;
end$$
delimiter ;
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)