如何为plsql中的列分配匿名变量?

时间:2016-07-15 07:25:14

标签: plsql

我正在尝试将输入变量分配到pl / sql。

我想通过让用户输入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:

根据以下回答编辑2,错误 我不知道如何正确输入数字

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.

2 个答案:

答案 0 :(得分:1)

Raj_Te解释了为什么你的代码不能编译。但我不太喜欢你找回第二高薪的方法。我猜你的方法有效,但目前尚不清楚该部分代码试图实现的目标。

在以下代码中,DENSE_RANK函数将返回每行的工资排名。任何具有相同薪水的行都将获得相同的排名:

SELECT
 emp.first_name
,emp.last_name
,emp.salary
,emp.department_id
FROM
 (SELECT
   emp1.first_name
  ,emp1.last_name
  ,emp1.salary
  ,emp1.department_id
  ,DENSE_RANK() OVER (PARTITION BY emp1.department_id ORDER BY emp1.salary DESC)  dr
  FROM
   employees      emp1
  WHERE emp1.department_id = &dept_id
 )   emp
WHERE 1=1
AND emp.dr = 2
;

通过陈述emp.dr = 2,我们说我们想要所有薪水排名第二的行。

答案 1 :(得分:0)

您在单个变量中选择了4个字段。这就是为什么它没有足够的价值。您必须对4个变量进行十分转换或创建包含4列的记录,然后捕获这些值。

Select  emp1.first_name, emp1.last_name, emp1.salary, emp1.department_id 
into v_dept_id  <--- trying to fetch 4 columns values to a single varibale.

您可以对与您选择的数据类型相同的新变量进行十分转换,然后执行此操作:

Select  emp1.first_name, emp1.last_name, emp1.salary, emp1.department_id 
into v_emp_first_nm,v_emp_last_nm ,v_emp_sal,v_dept_id