Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Plsql 如何解决这个pl sql oracle查询? 使用While循环编写PL/SQL程序,以显示所有DEPTNO、DNAME和LOC 从DEPT表中。假设两个deptno之间的差值为10。_Plsql - Fatal编程技术网

Plsql 如何解决这个pl sql oracle查询? 使用While循环编写PL/SQL程序,以显示所有DEPTNO、DNAME和LOC 从DEPT表中。假设两个deptno之间的差值为10。

Plsql 如何解决这个pl sql oracle查询? 使用While循环编写PL/SQL程序,以显示所有DEPTNO、DNAME和LOC 从DEPT表中。假设两个deptno之间的差值为10。,plsql,Plsql,我是beginer,我很困惑如何解决这个问题。请帮助解决这个问题。以下是您如何完成要求的操作,但请记住,这不是最佳的方法。如果您的目标是在PLSQL中的While循环上训练自己,那么就开始吧 DECLARE CURSOR C_DEPTS IS SELECT DEPTNO, DNAME, LOC FROM DEPT; V_DEPTNO VARCHAR2 (255); V_DNAME VARCHAR2 (255); V_LOC VARC

我是beginer,我很困惑如何解决这个问题。请帮助解决这个问题。

以下是您如何完成要求的操作,但请记住,这不是最佳的方法。如果您的目标是在PLSQL中的While循环上训练自己,那么就开始吧

DECLARE
   CURSOR C_DEPTS
   IS
      SELECT DEPTNO, DNAME, LOC FROM DEPT;

   V_DEPTNO   VARCHAR2 (255);
   V_DNAME    VARCHAR2 (255);
   V_LOC      VARCHAR2 (255);
BEGIN
   OPEN C_DEPTS;

   FETCH C_DEPTS INTO V_DEPTNO, V_DNAME, V_LOC;

   WHILE C_DEPTS%FOUND
   LOOP
      DBMS_OUTPUT.PUT_LINE ('DEPTNO = ' || V_DEPTNO);
      DBMS_OUTPUT.PUT_LINE ('DNAME = ' || V_DNAME);
      DBMS_OUTPUT.PUT_LINE ('LOC = ' || V_LOC);

      FETCH C_DEPTS INTO V_DEPTNO, V_DNAME, V_LOC;
   END LOOP;
END;
像这样的

SQL> set serveroutput on
SQL> declare
  2    i            dept.deptno%type;   -- loop counter
  3    l_max_deptno dept.deptno%type;   -- upper limit
  4    l_dept_row   dept%rowtype;       -- will contain the whole DEPT table row
  5  begin
  6    -- MIN -> i (which will be the starting point); MAX -> l_max_deptno (which will be the end)
  7    select min(deptno), max(deptno)
  8      into i, l_max_deptno
  9      from dept;
 10
 11    while i <= l_max_deptno
 12    loop
 13      -- select the whole row into L_DEPT_ROW
 14      select *
 15        into l_dept_row
 16        from dept
 17        where deptno = i;
 18
 19      dbms_output.put_line(l_dept_row.deptno ||' - '||
 20                           l_dept_row.dname  ||' - '||
 21                           l_dept_row.loc);
 22      -- increment counter by 10 (because, as you said, the difference is 10)
 23      i := i + 10;
 24    end loop;
 25  end;
 26  /
10 - ACCOUNTING - NEW YORK
20 - RESEARCH - DALLAS
30 - SALES - CHICAGO
40 - OPERATIONS - BOSTON

PL/SQL procedure successfully completed.

SQL>

这里我必须在循环中使用if-else,而不是游标。我是通过for循环来解决这个问题的,但要求是通过while来解决的。这里是我的for循环解决方案。我希望您将其转换为while循环:声明x编号:=0;如果i.deptno-x=10,则在select*from dept循环中为i开始,然后DBMS_OUTPUT.PUT_LINE“部门号:”| i.deptno;DBMS_OUTPUT.PUT_LINE“部门:”|| i.dname;DBMS_OUTPUT.PUT_LINE'Loc:'|| i.Loc;x:=x+10;如果结束;端环;终止声明x编号:=0;如果i.deptno-x=10,则在select*from dept循环中为i开始,然后DBMS_OUTPUT.PUT_LINE“部门号:”| i.deptno;DBMS_OUTPUT.PUT_LINE“部门:”|| i.dname;DBMS_OUTPUT.PUT_LINE'Loc:'|| i.Loc;x:=x+10;如果结束;端环;终止非常感谢你。