PLSQL Prodecure Select 来自 table 的单行产生错误 PLS-00103

PLSQL Prodecure to Select a single row from a table produces error PLS-00103

我的代码目标是引起幻象行。我了解这个过程,但我正在努力将我的第一个 sql 脚本转换为 运行

CREATE OR REPLACE PROCEDURE PHANTOM

IS

DECLARE 
ENAME CHAR;
ENAME EMPLOYEE%ROWTYPE;

BEGIN

SELECT * INTO ENAME
FROM EMPLOYEE
WHERE NAME = 'Albert';

DBMS_OUTPUT.PUT_LINE(ENAME.NAME);

DBMS_LOCK.SLEEP(15);

DBMS_OUTPUT.PUT_LINE(ENAME.NAME);

 END;
 /

这是我的第一个脚本,其中声明了一个 char 类型的变量 ENAME,并从 table EMPLOYEE 中获取 NAME 的值,其中 name 等于 Albert。然后在 DMBS 语句中调用该变量以 return 名称。我的第二个脚本只包含更新和提交以完成幻影过程。

当我尝试编译此程序时出现两个错误

错误 1

Error(5,1): PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following:     begin function pragma procedure subtype type <an identifier>    <a double-quoted delimited-identifier> current cursor delete    exists prior external language The symbol "begin" was substituted for "DECLARE" to continue. 

错误 2

Error(21,5): PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:     ( begin case declare end exception exit for goto if loop mod    null pragma raise return select update while with    <an identifier> <a double-quoted delimited-identifier>    <a bind variable> << continue close current delete fetch lock    insert open rollback savepoint set sql execute commit forall    merge pipe purge json_exists json_value json_query    json_object json_array 

我该如何解决这个问题?谢谢

你离这里不远。一个问题是您如何编写 SQL 脚本与如何编写 PL/SQL 过程之间的语法混淆。

如果你想要一个独立的脚本,即不存储代码供以后重复使用的地方,它只 运行 一次,执行一个动作,然后消失,它是

DECLARE
count_of_apples NUMBER;
BEGIN
...
END;
/

如果你想定义一个你可以重新运行任意多次的过程,那就是

CREATE OR REPLACE PROCEDURE phantom IS
-- don't need to say DECLARE, it's implied
count_of_apples NUMBER;
BEGIN
...
END phantom; -- good practice to add the name again here
/

然后你

exec phantom;

到运行那个程序。

另一个问题是您已经用两种不同的类型声明了同一个变量两次 (ENAME),这是您做不到的。不过,您不需要 CHAR 行,因此您可以删除该行,这样就没问题了。