基本的Oracle问题

发布于 2024-10-06 01:48:48 字数 315 浏览 5 评论 0原文

我是甲骨文新手。当我使用以下命令创建存储过程时:

CREATE OR REPLACE PROCEDURE PROCEDURE1 
AS
BEGIN

  SELECT FIRSTNAME,
         LASTNAME
    INTO FirstName,LastName
    FROM EMPLOYEE;

END PROCEDURE1;

出现以下错误:

PL/SQL 语句被忽略 必须声明标识符 FIRSTNAME ORA-00904 无效标识符

I am new to oracle. When I create a stored procedure using:

CREATE OR REPLACE PROCEDURE PROCEDURE1 
AS
BEGIN

  SELECT FIRSTNAME,
         LASTNAME
    INTO FirstName,LastName
    FROM EMPLOYEE;

END PROCEDURE1;

i get the following errors:

PL/SQL Statement Ignored
Identifier FIRSTNAME must be declared
ORA-00904 Invalid identifier

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怎会甘心 2024-10-13 01:48:48

您需要在尝试填充变量之前声明它们:

CREATE OR REPLACE PROCEDURE PROCEDURE1 
AS

 FirstName EMPLOYEE.FIRSTNAME%TYPE;
 LastName EMPLOYEE.LASTNAME%TYPE;

BEGIN

  SELECT FIRSTNAME,
         LASTNAME
    INTO FirstName,LastName
    FROM EMPLOYEE;

END PROCEDURE1;

%TYPE 表示法是与列数据类型匹配的数据类型声明的简写。如果该数据类型发生变化,您无需更新过程。

You need to declare variables before you attempt to populate them:

CREATE OR REPLACE PROCEDURE PROCEDURE1 
AS

 FirstName EMPLOYEE.FIRSTNAME%TYPE;
 LastName EMPLOYEE.LASTNAME%TYPE;

BEGIN

  SELECT FIRSTNAME,
         LASTNAME
    INTO FirstName,LastName
    FROM EMPLOYEE;

END PROCEDURE1;

The %TYPE notation is shorthand for data type declaration that matches the column data type. If that data type ever changes, you don't need to update the procedure.

凉风有信 2024-10-13 01:48:48

您需要声明变量。

CREATE OR REPLACE
PROCEDURE PROCEDURE1 AS
 V_FIRSTNAME VARCHAR2(60);
 V_LASTNAME  VARCHAR2(60);
BEGIN
        SELECT FIRSTNAME,LASTNAME
         INTO V_FIRSTNAME ,V_LASTNAME  
         FROM EMPLOYEE;
END PROCEDURE1;

回复您的评论,PL/SQL 块中的 SQL 语句只能获取 1 条记录。如果需要获取多条记录,则需要将记录存储在游标中并对其进行处理。

CREATE OR REPLACE
    PROCEDURE PROCEDURE1 AS

     CURSOR EMP_CUR IS
         SELECT FIRSTNAME,LASTNAME
         FROM EMPLOYEE;
    EMP_CUR_REC EMP_CUR%ROWTYPE;

    BEGIN
     FOR EMP_CUR_REC IN EMP_CUR LOOP
         -- do your processing
         DBMS_OUTPUT.PUT_LINE('Employee first name is ' || EMP_CUR_REC.FIRSTNAME);
         DBMS_OUTPUT.PUT_LINE('Employee last name is ' || EMP_CUR_REC.LASTNAME);
    END LOOP;
    END PROCEDURE1;

解释一下:
EMP_CUR 保存要执行的 SQL 语句。
EMP_CUR_REC 保存将由 SQL 语句获取的记录。
%ROWTYPE 表示记录将与保存数据的行具有相同的数据类型

FOR LOOP 将获取每条记录,您可以执行任何需要的处理待完成。

You need to declare the variables.

CREATE OR REPLACE
PROCEDURE PROCEDURE1 AS
 V_FIRSTNAME VARCHAR2(60);
 V_LASTNAME  VARCHAR2(60);
BEGIN
        SELECT FIRSTNAME,LASTNAME
         INTO V_FIRSTNAME ,V_LASTNAME  
         FROM EMPLOYEE;
END PROCEDURE1;

In reply to your comment, SQL statements in PL/SQL block can fetch only 1 record. If you need to fetch multiple records, you will need to store the records in a cursor and process them.

CREATE OR REPLACE
    PROCEDURE PROCEDURE1 AS

     CURSOR EMP_CUR IS
         SELECT FIRSTNAME,LASTNAME
         FROM EMPLOYEE;
    EMP_CUR_REC EMP_CUR%ROWTYPE;

    BEGIN
     FOR EMP_CUR_REC IN EMP_CUR LOOP
         -- do your processing
         DBMS_OUTPUT.PUT_LINE('Employee first name is ' || EMP_CUR_REC.FIRSTNAME);
         DBMS_OUTPUT.PUT_LINE('Employee last name is ' || EMP_CUR_REC.LASTNAME);
    END LOOP;
    END PROCEDURE1;

To explain:
EMP_CUR holds the SQL statement to be executed.
EMP_CUR_REC holds the records that will be fetched by the SQL statement.
%ROWTYPE indicates that the Record will be of the same data type as the row which holds the data

The FOR LOOP will fetch each record, and you can do whatever processing that needs to be done.

温柔嚣张 2024-10-13 01:48:48

我认为“AS”关键字不起作用。如果不起作用,请使用“IS”。
休息很好,而且有很好的建议。

如果您需要有关 PL/SQL 的任何帮助,可以查看此链接。它非常简单易懂;
http://plsql-tutorial.com/

这是我对您遇到的错误的解决方案;

CREATE OR REPLACE PROCEDURE PROCEDURE1 IS
  v_FIRSTNAME EMPLOYEE.FIRSTNAME%TYPE;
  v_LASTNAME EMPLOYEE.LASTNAME%TYPE;
  CURSOR EMPCURSOR IS
  SELECT FIRSTNAME, LASTNAME FROM EMPLOYEE;      
  BEGIN   
     IF NOT EMPCURSOR%ISOPEN THEN
         OPEN EMPCURSOR;
     END IF;
     LOOP
        FETCH EMPCURSOR INTO V_FIRSTNAME,V_LASTNAME;
        EXIT WHEN EMPCURSOR%NOTFOUND;
     END LOOP;
     IF EMPCURSOR%ISOPEN THEN
        CLOSE EMPCURSOR;
     END;
  END PROCEDURE1;

您还可以在循环内使用DBMS_OUTPUT.PUT_LINE(V_FIRSTNAME || ','|| V_LASTNAME)来显示输出。但为了做到这一点,您首先需要执行命令服务器输出

I think the "AS" keyword won't work. If it doesn't work, then use "IS".
Rest are fine and very good tips.

If you need any help regarding PL/SQL, then you can have a look at this link. It is very simple and easy to understand;
http://plsql-tutorial.com/

This is my solution to the error which you are getting;

CREATE OR REPLACE PROCEDURE PROCEDURE1 IS
  v_FIRSTNAME EMPLOYEE.FIRSTNAME%TYPE;
  v_LASTNAME EMPLOYEE.LASTNAME%TYPE;
  CURSOR EMPCURSOR IS
  SELECT FIRSTNAME, LASTNAME FROM EMPLOYEE;      
  BEGIN   
     IF NOT EMPCURSOR%ISOPEN THEN
         OPEN EMPCURSOR;
     END IF;
     LOOP
        FETCH EMPCURSOR INTO V_FIRSTNAME,V_LASTNAME;
        EXIT WHEN EMPCURSOR%NOTFOUND;
     END LOOP;
     IF EMPCURSOR%ISOPEN THEN
        CLOSE EMPCURSOR;
     END;
  END PROCEDURE1;

You can also use DBMS_OUTPUT.PUT_LINE(V_FIRSTNAME || ','|| V_LASTNAME), inside the loop to display the output. but in order to do that, you first need to execute the command server output on

不知所踪 2024-10-13 01:48:48

在对上面 @Sathya 的回答的回应中,@kayak 询问“我可以有类似 Select * from Tablename 或 select firstname,lastname from tablename 的东西吗,就像我们在 sql server 中一样”。

是的,您可以这样做,但您需要包含 WHERE 子句或使用游标。如果您包含一个将结果限制为单行的 WHERE 子句,您可以编写类似的内容

CREATE OR REPLACE PROCEDURE PROCEDURE1
IS
  rowEmployees EMPLOYEE%ROWTYPE;
BEGIN
  SELECT *
    INTO rowEmployees
    FROM EMPLOYEE
    WHERE EMPLOYEE_ID = 12345;
END PROCEDURE1; 

另一方面,如果您没有 WHERE 子句,因为您希望处理表中的所有行,或者您有一个 WHERE子句不将结果限制为单行,您可以按以下方式使用游标:

CREATE OR REPLACE PROCEDURE PROCEDURE1 IS
BEGIN
  FOR rowEmployees IN (SELECT *
                         FROM EMPLOYEE
                         WHERE EMPLOYEE_ID IN (12345, 67890, 111213, 141516))
  LOOP
    <do something with rowEmployees here>
  END LOOP;
END PROCEDURE1; 

共享并享受。

In a response to @Sathya's answer above @kayak asked "Can i have something like Select * from Tablename or select firstname,lastname from tablename , like we have in sql server".

Yes, you can do that, but you'll either need to include a WHERE clause or use a cursor. If you include a WHERE clause which limits your results to a single row you could write something like

CREATE OR REPLACE PROCEDURE PROCEDURE1
IS
  rowEmployees EMPLOYEE%ROWTYPE;
BEGIN
  SELECT *
    INTO rowEmployees
    FROM EMPLOYEE
    WHERE EMPLOYEE_ID = 12345;
END PROCEDURE1; 

On the other hand, if you either don't have a WHERE clause because you wish to process all rows in the table, or you have a WHERE clause which doesn't limit your results to a single row you could use a cursor in the following manner:

CREATE OR REPLACE PROCEDURE PROCEDURE1 IS
BEGIN
  FOR rowEmployees IN (SELECT *
                         FROM EMPLOYEE
                         WHERE EMPLOYEE_ID IN (12345, 67890, 111213, 141516))
  LOOP
    <do something with rowEmployees here>
  END LOOP;
END PROCEDURE1; 

Share and enjoy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文