简体   繁体   English

在 Oracle 的过程中运行 SQL 查询时出错

[英]Getting error while running SQL query in Procedure in Oracle

Tring to create a simple procedure that takes a input and performs a Select Query as below:尝试创建一个简单的过程,该过程接受输入并执行如下选择查询:

CREATE OR REPLACE PROCEDURE test3(br IN VARCHAR2)
AS
a varchar(32) ;
BEGIN
SELECT * FROM BRANDS B WHERE B.BRAND_ID = br  INTO a;
dbms_output.put_line(a);
END;

I am getting the following error:我收到以下错误:

Errors: PROCEDURE TEST3
Line/Col: 5/1 PL/SQL: SQL Statement ignored
Line/Col: 5/47 PL/SQL: ORA-00933: SQL command not properly ended

Table is :表是:

BRAND_ID    NAME    ADDRESS JOIN_DATE   PASSWORD
Brand01 Brand X 503 Rolling Creek Dr Austin, AR 04/01/2021  12345abcde
Brand02 Brand Y 939 Orange Ave Coronado, CA 03/25/2021  12345abcde
Brand03 Brand Z 20 Roszel Rd Princeton, NJ  05/08/2021  12345abcde
  • You want the INTO clause before the FROM .您需要在FROM之前的INTO子句。
  • You need a %ROWTYPE variable with SELECT * .您需要一个带有SELECT *%ROWTYPE变量。
  • Following on from the previous point, you cannot use DBMS_OUTPUT.PUT_LINE() on a record;从上一点开始,您不能在记录上使用DBMS_OUTPUT.PUT_LINE() you need to extract the individual fields.您需要提取各个字段。
  • You should handle the NO_DATA_FOUND exception.您应该处理NO_DATA_FOUND异常。
  • Do NOT store passwords as plain-text.存储密码为纯文本。 Store a one-way hash of the password.存储密码的单向哈希。

Which gives:这使:

CREATE OR REPLACE PROCEDURE test3(
  br IN BRANDS.BRAND_ID%TYPE
)
AS
  a BRANDS%ROWTYPE;
BEGIN
  SELECT *
  INTO   a
  FROM   BRANDS
  WHERE  BRAND_ID = br;
  
  dbms_output.put_line(
    a.brand_id
    || ' ' || a.name
    || ' ' || a.address
    || ' ' || a.join_date
    || ' ' || a.password
  );
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    NULL;
END;
/

Then:然后:

BEGIN
  test3('Brand02');
END;
/

Outputs:输出:

 Brand02 Brand Y 939 Orange Ave Coronado, CA 25-MAR-21 12345abcde

db<>fiddle here db<> 在这里小提琴

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

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