2016-07-15 30 views
0

我想分配一個輸入變量到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. 
+0

你在哪裏使用v_dept_id? 問題不清楚。 在plsql中如果使用select語句,則需要使用INTO子句將值分配給某個變量。 –

+0

剛剛添加它!然而它不起作用 – Venom

+0

我想創建一個PLSQL塊,它將輸入一個部門編號,並輸出員工的名字和姓氏以及在該部門中具有第二高薪水的員工的工資 – Venom

回答

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,我們說,我們要在工資排名第二高的所有行。

+0

感謝它的工作,但我需要刪除第一個選擇語句中的emp1,所以在第一個選擇語句,它將是'SELECT first_name,last_name,salary,department_id' – Venom

+0

是的,的確,我的歉意。別名應該是emp - 我會更新我的答案 –

0

您在單個變量中選擇4個字段。這就是爲什麼它沒有足夠的價值。您必須decalre 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. 

可以decalre相同的數據類型的新變量作爲您的選擇,並做到這一點:

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 
+0

好。接受輸入的內容讓用戶想要在運行查詢時顯示的提示中輸入90?我還沒弄明白。此外我修改它看到上面的錯誤再次 – Venom

+0

這應該解決您的問題=== http://stackoverflow.com/questions/15558929/how-do-you-pass-an-argument-to-a-pl-sql -block-within-a-sql-file-called-using-sta另請參見下面的鏈接==== http://plsql-tutorial.com/plsql-passing-parameters-procedure-function.htm – XING

+0

PL/SQL不是交互式的。提示輸入是客戶端應用程序的工作,如SQL \ * Plus,Perl或Java。如果這總是從SQL \ * Plus運行,請查看[ACCEPT](http://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_twelve005.htm)。 –