select语句在程序中不起作用

时间:2014-03-11 07:18:33

标签: sql oracle plsql

create table Employee(Id number,
                  Name varchar(20),
                  Age number,
                  DId number,
                  Salary number,
         primary key(Id),
         foreign key(DId) references Department on delete cascade);

declare
  total number;
  procedure myFunction(x in number) is 
     begin
       insert into Employee values(17,'Jaskaran Singh',31,1,200000);
       dbms_output.put_line('successfully executed');
       select * from Employee;
      end;
   begin
     myFunction(3);
   end;

2 个答案:

答案 0 :(得分:0)

要从存储过程返回数据,您应该创建一个游标并返回如下所示的选择:

CREATE OR REPLACE PACKAGE TYPES
AS
   TYPE DATA_CURSOR IS REF CURSOR;
END;

然后在你的代码中将会像:

CREATE OR REPLACE PROCEDURE MYPROC(RESULTSET OUT TYPES.DATA_CURSOR)  AS
   BEGIN
       INSERT INTO EMPLOYEE VALUES(17,'Jaskaran Singh',31,1,200000);
       DBMS_OUTPUT.PUT_LINE('successfully executed');

    OPEN RESULTSET FOR
      SELECT * FROM EMPLOYEE;
      END;

执行部分如

DECLARE THE_RESULT_SET OUT TYPES.DATA_CURSOR;
   BEGIN
     MYPROC(3, THE_RESULT_SET);
    --     You can now get the THE_RESULT_SET and take the result from it...
   END;

重要:如果您想要了解另一种情况,您可以获得该结果(相同代码),并循环播放您想要的任何内容并打印THE_RESULT_SET的结果

答案 1 :(得分:0)

如果要打印EMPLOYEES表中的内容,可以将光标环绕在EMPLOYEES表上,并相应地打印每一行。这是一个例子:

DECLARE
  TOTAL NUMBER;

  PROCEDURE MYFUNCTION(X IN NUMBER) IS 
    BEGIN
      INSERT INTO EMPLOYEE VALUES(17,'Jaskaran Singh',31,1,200000);
      DBMS_OUTPUT.PUT_LINE('successfully executed');

      FOR aRow IN (SELECT * FROM EMPLOYEE) LOOP
        DBMS_OUTPUT.PUT_LINE('ID=' || aRow.ID ||
                             ' NAME=''' || aRow.NAME || '''' ||
                             ' AGE=' || aRow.AGE ||
                             ' DID=' || aRow.DID ||
                             ' SALARY=' || aRow.SALARY);
      END LOOP;
  END;
BEGIN
  MYFUNCTION(3);
END;

分享并享受。