使用光标显示部门名称.创建一个 PL/SQL 块以使用光标显示部门表中的所有部门名称 [英] Display department names using Cursors.Create a PL/SQL block to display all the department names from the Department table using cursors
问题描述
表格:
Column name Data type Constraints
DEPARTMENT_ID NUMBER(5) PK
DEPARTMENT_NAME VARCHAR2(25) NOT NULL
LOCATION_ID VARCHAR2(15)
这个我试过了.但我仍然没有得到我的输出.有人能帮我得到输出吗.
I have tried this. But still i didn’t get my output. Can somebody help me to get the output.
set serveroutput on;
DECLARE
DEPARTMENT_NAME VARCHAR2(25);
CURSOR dep_cursor IS
SELECT
DEPARTMENT_NAME
FROM
department;
BEGIN
OPEN dep_cursor;
FETCH dep_cursor INTO DEPARTMENT_NAME;
DBMS_OUTPUT.PUT_LINE(‘Department Names are :’ || DEPARTMENT_NAME);
CLOSE dep_cursor;
END;
/
错误信息:未声明绑定变量~".
Error message: Bind variable "~" not declared.
示例输出:
Department Names are :
ADMIN
DEVELOPMENT
TESTING
推荐答案
是关于那些花哨"的您在此处使用的单引号:
It is about those "fancy" single quotes you used here:
DBMS_OUTPUT.PUT_LINE(‘Department Names are :’ || DEPARTMENT_NAME);
应该
DBMS_OUTPUT.PUT_LINE('Department Names are :' || DEPARTMENT_NAME);
截至其他错误"你已经做了:没有循环,光标只返回一行,然后显示它.我建议您切换到游标 for 循环,因为它更易于维护:
As of other "errors" you've made: without a loop, cursor returns only one row and you then display it. I'd suggest you to switch to a cursor for loop as it is simpler to maintain:
begin
for cur_r in (select department_name from department) loop
dbms_output.put_line(cur_r.department_name);
end loop;
end;
/
这就是你需要的一切;没有声明部分,没有打开或关闭游标,不用担心退出循环...... Oracle 为你做.
This is everything you need; no declaration section, no opening nor closing a cursor, no worrying about exiting the loop ... Oracle does it for you.
这篇关于使用光标显示部门名称.创建一个 PL/SQL 块以使用光标显示部门表中的所有部门名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!