首页 文章

Pl SQL Oracle PLS-00103:遇到符号“CREATE”

提问于
浏览
0

此代码接受zipcode,city和state的输入,然后将其插入到表创建的Address中 . 在插入数据之前,它将检查zipcode是否已经在表中,如果是,则调用过程(错误)以显示错误代码 .

我得到一个错误代码pls-00103:在尝试执行代码时遇到符号“CREATE” . 到目前为止,这是我的代码 . 在此先感谢您的帮助 .

drop table address;

create table address(zipcode NUMBER, city varchar2(30), state varchar2(20));

create or replace procedure error as
begin
  dbms_output.put_line('Error Zip Code already found in table');
end error;

declare
 zzip number;
 ccity varchar2(30);
 sstate varchar2(30);

create or replace procedure location(p_zipcode NUMBER,
                                     p_city varchar2,
                                     p_state varchar2) is
zip address.zipcode%type;
cit address.city%type;
st address.state%type;

begin
  select count(*) from address into zip where zipcode = zip;
  if any_rows_found then 
    error;
  else
  Insert into address values(zip, cit, st);
  end if;
end location;

begin
  select &zipcode into zzip from dual;
  select &city into ccity from dual;
  select &state into sstate from dual;
  procedure location(zzip, ccity, sstate);
end;
/

2 回答

  • 1

    我不确定你要做什么,但以下内容可能更接近你的想法:

    drop table address;
    
    create table address(zipcode NUMBER, city varchar2(30), state varchar2(20));
    
    declare
     zzip number;
     ccity varchar2(30);
     sstate varchar2(30);
    
     procedure error is
     begin
      dbms_output.put_line('Error Zip Code already found in table');
     end error;
    
     procedure location(p_zipcode NUMBER, p_city varchar2, p_state varchar2) is
      zip_count   NUMBER;
     begin
      select count(*)
        into zip_count
        from address
        where zipcode = p_zipcode;
    
      if zip_count > 0 then 
        error;
      else
       Insert into address
         (zipcode, city, state)
       values
         (p_zipcode, p_city, p_state);
      end if;
     end location;
    
    begin
     select &zipcode into zzip from dual;
     select &city into ccity from dual;
     select &state into sstate from dual;
    
     location(zzip, ccity, sstate);
    end;
    /
    

    祝你好运 .

  • 0

    我不知道我是否正确理解你的问题,但我想解决一些问题,以解决你的问题

    • 首先,如果要创建过程/函数,请在单独的工作表中进行编译 . 不要与其他匿名块一起编译它,因为大多数时候,如果你不用'/'结束你的其他块,肯定会产生错误 .

    • 其次,您的DECLARE语句放错位置,如果您要创建一个匿名块,请确保DECLARE,BEGIN和END排队,不要在匿名块内创建过程/函数 .

    • 第三,您在程序中声明变量并使用它们但没有初始值,因此它只会将空值传递给过程中的DML语句 . 只需直接使用参数 .

    • 第四,避免创建仅包含dbms_output.put_line的过程 . 它很傻 .

    • 最后,您的匿名块应该调用您的过程,使用'&',请避免在pl / sql中使用'&'作为'&'是SQL * Plus中的一个功能,并且在PL / SQL中没有任何意义,相反,您可以使用':'作为绑定变量 . 但你使用'&'不是绑定变量,所以你应该删除它;

    试试这个:

    drop table address;
    
     /
    
    create table address(zipcode NUMBER, city varchar2(30), state varchar2(20));
    
     / 
    
    create or replace procedure location(p_zipcode NUMBER, 
                                         p_city varchar2,    
                                         p_state varchar2) is
    
    zip address.zipcode%type;
    
    
    begin
      select count(*) 
        from address 
        into zip 
        where zipcode = p_zipcode 
                 and city =p_city 
                    and state = p_state;
    
      if zip > 0 then 
       dbms_output.put_line('Error Zip Code already found in table');
      else
      Insert into address values(p_zipcode, p_city, p_state);
      end if;
    end location;
    
    /
    
    
    begin
    
     location(:zzip, :ccity, :sstate);
    
    end;
    

相关问题