单行子查询returns多行更新查询|数据库 |

Single-row subquery returns more than one row for update query | PLSQL |

我有点卡住了,不知道如何编写代码。

我已经 table 调用了 STUDENT,其中包含以下数据:

STUDID | NAME | SURNAME | STUDENT_FILE       | CLASS 
-------+------+---------+--------------------+-------
154    | Vil  | Docs    | \demo\F\A.csv   | A  
132    | Nil  | Fics    | \do\F\X.csv     | A
123    | Sag  | Ter     | \de\F\Y.csv     | A
178    | Mia  | Jac     | \mo\F\Z.csv     | A
167    | Dic  | Pol     | D.csv              | A
134    | Mic  | Kil     | (null)             | A

我的预期结果是下面的格式,我需要删除所有路径名并只保留 STUDENT_FILE

列中的文件名
STUDID | NAME | SURNAME | STUDENT_FILE       | CLASS 
-------+------+---------+--------------------+-------
154    | Vil  | Docs    | A.csv              | A  
132    | Nil  | Fics    | X.csv              | A
123    | Sag  | Ter     | Y.csv              | A
178    | Mia  | Jac     | Z.csv              | A
167    | Dic  | Pol     | D.csv              | A
134    | Mic  | Kil     | (null)             | A

我的代码:

DECLARE 
   CNT NUMBER 
BEGIN
     SELECT COUNT(STUD_ID) INTO CNT FROM STUDENT;
     
     FOR i...CNT LOOP
     UPDATE STUDENT SET STUDENT_FILE=(SELECT SUBSTR(STUDENT_FILE,(INSTR(STUDENT_FILE,'\',-1,1)+1),LENGTH(STUDENT_FILE)) AS FL FROM STUDENT) where CLASS='A' and ROWNUM :=i;    
     END LOOP;
END;
/   

我收到一个错误

Single-row subquery returns more than one row

非常感谢任何解决方案,或者如何将其创建为通用存储过程?

您不需要 PL/SQL,只需一个普通的更新:

UPDATE student
   SET student_file =
          SUBSTR (student_file,
                  (  INSTR (student_file,
                            '\',
                            -1,
                            1)
                   + 1),
                  LENGTH (student_file))
 WHERE class = 'A'

顺便说一下,rownum = i 只是一种访问任何行的错误方法,它永远不会是真的(除非 i = 1)。


此外,一个更简单的选择是使用

UPDATE student
   SET student_file =
          SUBSTR (student_file, INSTR (student_file, '\', -1) + 1)
 WHERE class = 'A';