简体   繁体   English

PL SQL过程与选择

[英]PL SQL procedure with select

I want to execute procedure that returns me the First name, last name and the title of the author based on the state he/she is from. 我要执行一个程序,根据他/她来自的州,将我的名字,姓氏和标题返回给我。 But the code above returns me PLS-00103 error "end-of-file". 但是上面的代码向我返回PLS-00103错误“文件结束”。

CREATE OR REPLACE PROCEDURE proc1 (stattt OUT SYS_REFCURSOR) AS 
BEGIN
SELECT a.au_fname, a.au_lname, t.title
FROM authors a, titleauthor ta, titles t 
WHERE ta.au_id = a.au_id
AND t.title_id = ta.title_id
AND state =  stattt
/

I also tried to use 我也尝试使用

into proc1

but it is still doesnt work. 但它仍然无法正常工作。 This is from the sample database Pubs from MS SQL that I converted to SQL Oracle. 这来自我转换为SQL Oracle的MS SQL示例数据库Pubs。

A few objections: 一些反对意见:

  • you already know that END is missing 您已经知道缺少END
  • apart from that, INTO is required for a SELECT statement in PL/SQL (ie you have to put the result somewhere ). 除此之外,PL / SQL中的SELECT语句还需要INTO (即,必须将结果放在某处 )。 Usually, you declare local variables for that purpose. 通常,您为此目的声明局部变量。 You chose to return ref cursor - OK 您选择返回参考光标-确定
  • stattt is ref cursor, set as an out parameter for that procedure; stattt是ref游标,设置为该过程的out参数; I don't think that you can use it as a parameter in that WHERE clause. 我认为您不能在WHERE子句中将其用作参数。

Here's an example based on Scott's schema (as I don't have your tables) which shows how to do that. 这是一个基于Scott模式的示例(因为我没有您的表),该示例演示了如何执行此操作。 I'm returning all employees that work in some department (10, in this example): 我将返回在某个部门工作的所有雇员(在此示例中为10名):

SQL> CREATE OR REPLACE PROCEDURE p_test (par_deptno   IN     NUMBER,
  2                                      par_rc          OUT SYS_REFCURSOR)
  3  IS
  4  BEGIN
  5     OPEN par_rc FOR
  6        SELECT empno, ename, sal
  7          FROM emp
  8         WHERE deptno = par_deptno;
  9  END;
 10  /

Procedure created.

SQL> var l_rc refcursor
SQL>
SQL> exec p_test(10, :l_rc);

PL/SQL procedure successfully completed.

SQL> print l_rc

     EMPNO ENAME             SAL
---------- ---------- ----------
      7782 CLARK            2450
      7839 KING            10000
      7934 MILLER           1300

SQL>

Follow CREATE PROCEDURE syntax and the convention that every beginning has an end , and close BEGIN with END: 遵循CREATE PROCEDURE语法和每个开头都有end的约定,并使用END关闭BEGIN:

CREATE OR REPLACE PROCEDURE proc1 (stattt OUT SYS_REFCURSOR) AS 
BEGIN
SELECT a.au_fname, a.au_lname, t.title
FROM authors a, titleauthor ta, titles t 
WHERE ta.au_id = a.au_id
AND t.title_id = ta.title_id
AND state =  stattt
END;
/ 
 Header AS [declaration statements ...] BEGIN ... [EXCEPTION ...] END; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM