我想分配一個輸入變量到pl/sql中。如何分配一個匿名變量到plsql中的列?
我想通過讓用戶輸入department_id並輸出名字,姓氏和薪水來查詢數據庫。表中的工資是第二高的工資。
declare
v_dept_id int;
Begin
Select emp1.first_name, emp1.last_name, emp1.salary, emp1.department_id
From employees emp1
Where (1) =
(select count(distinct(emp1.salary))
From employees emp2
Where emp2.salary > emp1.salary) ;
End;
澄清 編輯:我編輯的代碼以包括V_dept_id但是它並不運行 聲明
v_dept_id INT;
Begin
Select emp1.first_name, emp1.last_name, emp1.salary, emp1.department_id
into v_dept_id
From employees emp1
Where ((1) =
(select count(distinct(emp1.salary))
From employees emp2
Where emp2.salary > emp1.salary)) and v_dept_id = '&Enter_dept_id' ;
End;
錯誤:
Error starting at line : 4 in command -
declare
v_dept_id int;
Begin
Select emp1.first_name, emp1.last_name, emp1.salary, emp1.department_id
into v_dept_id
From employees emp1
Where ((1) =
(select count(distinct(emp1.salary))
From employees emp2
Where emp2.salary > emp1.salary)) and v_dept_id = '&Enter_dept_id' ;
End;
Error report -
ORA-06550: line 6, column 16:
PL/SQL: ORA-00947: not enough values
ORA-06550: line 5, column 1:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
EDIT2基於以下反饋的答案與錯誤 我不知道如何讓輸入一個數字正確
declare
v_dept_id number;
v_fname varchar(50);
v_lname varchar(50);
v_salary NUMBER(8,2);
Begin
Select emp1.department_id, emp1.first_name, emp1.last_name, emp1.salary
into v_dept_id, v_fname, v_lname, v_salary
From employees emp1
Where ((1) =
(select count(distinct(emp1.salary))
From employees emp2
Where emp2.salary > emp1.salary)) and v_dept_id = '&Enter_dept_id' ;
End;
提示
Error starting at line : 1 in command -
declare
v_dept_id number;
v_fname varchar(50);
v_lname varchar(50);
v_salary NUMBER(8,2);
Begin
Select emp1.department_id, emp1.first_name, emp1.last_name, emp1.salary
into v_dept_id, v_fname, v_lname, v_salary
From employees emp1
Where ((1) =
(select count(distinct(emp1.salary))
From employees emp2
Where emp2.salary > emp1.salary)) and v_dept_id = '&Enter_dept_id' ;
End;
Error report -
ORA-01403: no data found
ORA-06512: at line 8
01403. 00000 - "no data found"
*Cause: No data was found from the objects.
*Action: There was no data from the objects which may be due to end of fetch.
你在哪裏使用v_dept_id? 問題不清楚。 在plsql中如果使用select語句,則需要使用INTO子句將值分配給某個變量。 –
剛剛添加它!然而它不起作用 – Venom
我想創建一個PLSQL塊,它將輸入一個部門編號,並輸出員工的名字和姓氏以及在該部門中具有第二高薪水的員工的工資 – Venom