pl / sql控件结构获取错误

时间:2014-02-23 00:00:13

标签: sql oracle plsql

阙 使用control Structure编写程序以显示客户的信用额度。应要求用户输入客户ID。并且根据其信用额度,应显示以下消息之一。如果员工的信用额度超过4500美元,将显示最后一条消息。

此代码有什么问题请回复

     declare         
 v_emp_id employees.employee_id%type;           
     v_cust_num customers.customers#%type;       
      v_creditlimit customers.creditlimit%type;             

      begin 
        select employee_id ,customer# into v_emp_id ,    
        v_cust_num from employees natural join customers      
        where v_emp_id = '&v_emp_id';               

     if v_creditlimit < 1000 then      
     dbms_output.put_line ('credit limit less than 1000');        
        elseif v_creditlimit< 2000 then      
        dbms_output.put_line ('credit limit less than 2000');        
          elseif v_creditlimit< 3000 then       
          dbms_output.put_line ('credit limit less than 3000');        
        elseif v_creditlimit< 4000 then          
        dbms_output.put_line ('credit limit less than 4000');      
        elseif v_creditlimit< 6000 then      
        dbms_output.put_line ('credit limit less than 6000');             
       end if;       
    end;          
    /              

1 个答案:

答案 0 :(得分:0)

首先要开始使用 v_creditlimit 未初始化或分配给查询中的任何值第二件事v_emp_id是您声明的变量,并且您将其用作表的字段

尝试以下代码

    create table customers1 (customers# int,creditlimit int,employee_id int);

select * from customers1;

begin
insert into customers1 values(11,123,1);
insert into customers1 values(12,345,2);
commit;
end;
--------------------------------------------------------------------------------------
create table employees(employee_id int, first_name varchar2(20), last_name varchar2(20));

select * from employees;

begin
insert into employees values(1,'a','b');
insert into employees values(2,'c','d');
insert into employees values(3,'e','f');
commit;
end;
--------------------------------------------------------------------------------------
DECLARE
  v_emp_id employees.employee_id%type;
  v_cust_num customers1.customers#%type;
  v_creditlimit customers1.creditlimit%type;
BEGIN

  dbms_output.put_line('Enter the employee id.');
  v_emp_id := &emp_id;

  SELECT customers# INTO v_cust_num
  FROM employees NATURAL JOIN customers1 WHERE employee_id = v_emp_id;

  SELECT creditlimit into v_creditlimit
  FROM employees NATURAL JOIN customers1 WHERE employee_id = v_emp_id;

 dbms_output.put_line(v_cust_num); 
 dbms_output.put_line(v_creditlimit);

   IF v_creditlimit < 1000 THEN
    dbms_output.put_line ('credit limit less than 1000');
  elsif v_creditlimit BETWEEN 1001 and 2000 THEN
    dbms_output.put_line ('credit limit BETWEEN 1001 and 2000');
  elsif v_creditlimit BETWEEN 2001 and 3000 THEN
    dbms_output.put_line ('credit limit BETWEEN 2001 and 3000');
  elsif v_creditlimit BETWEEN 3001 and 4000 THEN
    dbms_output.put_line ('credit limit BETWEEN 3001 and 4000');
  elsif v_creditlimit BETWEEN 5001 and 6000 THEN
    dbms_output.put_line ('credit limit BETWEEN 5001 and 6000');
  END IF;

END;