Oracle 存储过程编译错误

Oracle Stored Procedure compilation error

我是 PL/SQL 的新手。任何人都可以帮助修复我的编译错误吗?非常感谢您的帮助。此外,在我想调用此过程来检查并添加新用户之后。

create or replace procedure CheckAddUser ( userid in varchar2(20))
as
declare vartmp number;
begin
    SELECT nvl((SELECT distinct 1 FROM crm_admin.LTY_USER_STORE WHERE usr_nm = userid  ), 0) INTO :varTmp FROM dual;    
    IF (:varTmp = 0) THEN
       dbms_output.put_line('the user ' || ':userid' || ' does not exist');

    elsif (:varTmp = 1) THEN
       dbms_output.put_line('the user ' || ':userid' || '  already exist');
    End if;
end;

试试这个:

create or replace procedure checkadduser(userid in varchar2)
as
    vartmp number;
begin
    select coalesce(max(1), 0) into vartmp
    from dual
    where exists (
            select 1
            from crm_admin.lty_user_store
            where usr_nm = userid
            );
    if vartmp = 0 then
        dbms_output.put_line('the user ' || userid || ' does not exist');
    elsif vartmp = 1 then
       dbms_output.put_line('the user ' || userid || '  already exist');
    end if;
end;
/

所做的更改:

  1. 从参数中删除了大小
  2. 删除了 declare 关键字 - 不是过程语法的一部分
  3. 修改查询以在找到行后立即停止搜索,return 1 否则为 0。

    select coalesce(max(1), 0) into varTmp
    from dual
    where exists (
            select 1
            from crm_admin.lty_user_store
            where usr_nm = userid
            );
    

    如果 usr_nm 在您的 table 中是唯一的,这也会很好地工作(这个 可以 使用,即使它不是唯一的但可以如果每个 usr_nm 的行数可以任意大,则性能稍差):

    select coalesce(max(1), 0)
    into varTmp
    from crm_admin.lty_user_store
    where usr_nm = userid
    
  4. 不要将 : 与变量和参数一起使用。

不要将 ":" 与变量一起使用。 我做了一些我可能会使用的更改:

--I recommended to change procedure to function, then you can use it in SQL
create or replace 
procedure CheckAddUser ( userid in varchar2)
as
  --Best practics, use self-describing variables
    isuserexist number(1,0); -- vartmp
    message_suff varchar2(30):=' does not exist';
begin
  --Best practics, first check the parameters
  if trim(userid) is null then
    raise_application_error(-20000, 'Param userid is empty');
  end if;

    select count(*) into isuserexist
  from crm_admin.lty_user_store 
  where usr_nm = userid;

  --only one if, reads easier
  if isUserExist > 0 then
    message_suff:= ' already exist';
  end if;

  dbms_output.put_line('the user ' || ':userid' || message_suff);
end;