接受用户的员工编号 从“员工”表中获取员工详细信息并将其存储到 PL SQL 记录类型变量并打印相同
Posted
技术标签:
【中文标题】接受用户的员工编号 从“员工”表中获取员工详细信息并将其存储到 PL SQL 记录类型变量并打印相同【英文标题】:Accpt employee number from user fetch the employee details from the “Employee” table and store it to a PLSQL record type variable and print the same 【发布时间】:2021-08-31 08:24:03 【问题描述】:从用户那里接受员工编号,从“员工”表中获取员工详细信息,并将其存储到 PLSQL 记录类型变量中并打印出来。
SET SERVEROUTPUT ON;
DECLARE
type emp_det is record
(
emp_num number := &empnum,
emp_name varchar2(150),
emp_sal number,
dept_id number
);
emp_rec emp_det;
BEGIN
select employee_id, first_name, salary, department_id into emp_num, emp_name,
emp_sal, dept_id from hr.employees where employee_id = &empnum;
dbms_output.put_line('Employee Number :'||emp_rec.emp_num);
dbms_output.put_line('Employee Number :'||emp_rec.emp_name);
dbms_output.put_line('Employee Number :'||emp_rec.emp_sal);
dbms_output.put_line('Employee Number :'||emp_rec.dept_id);
END;
【问题讨论】:
这是一个很好的起点:***.com/help/how-to-ask 【参考方案1】:这是一种选择(如果您正在寻找):
SQL> create or replace procedure p_test (par_empno in emp.empno%type) is
2 type emp_det is record (empno number,
3 ename varchar2(10),
4 sal number
5 );
6 led emp_det;
7 begin
8 select empno, ename, sal
9 into led
10 from emp
11 where empno = par_empno;
12
13 dbms_output.put_line(led.empno ||': '|| rpad(led.ename, 10, ' ') || led.sal);
14 end;
15 /
Procedure created.
SQL> exec p_test(7369);
7369: SMITH 800
PL/SQL procedure successfully completed.
SQL>
【讨论】:
【参考方案2】:您不需要记录或过程,可以使用%TYPE
获取列数据类型:
DECLARE
emp_num HR.EMPLOYEES.EMPLOYEE_ID%TYPE := &empnum;
emp_name HR.EMPLOYEES.FIRST_NAME%TYPE;
emp_sal HR.EMPLOYEES.SALARY%TYPE;
dept_id HR.EMPLOYEES.DEPARTMENT_ID%TYPE;
BEGIN
SELECT employee_id,
first_name,
salary,
department_id
INTO emp_num,
emp_name,
emp_sal,
dept_id
FROM hr.employees
WHERE employee_id = emp_num;
dbms_output.put_line('Employee Number :'||emp_num);
dbms_output.put_line('Employee Number :'||emp_name);
dbms_output.put_line('Employee Number :'||emp_sal);
dbms_output.put_line('Employee Number :'||dept_id);
END;
/
如果你必须(你真的不需要)使用记录,那么SELECT ... INTO
记录:
DECLARE
-- Declare the record type.
TYPE employee_record IS RECORD(
emp_num NUMBER, -- You cannot set a default value.
emp_name VARCHAR2(150),
emp_sal NUMBER,
dept_id NUMBER
);
-- Declare an instance of the record.
emp_rec employee_record;
BEGIN
SELECT employee_id, -- Columns in the same order as the record.
first_name,
salary,
department_id
INTO emp_rec -- Into the record instance.
FROM hr.employees
WHERE employee_id = &empnum;
dbms_output.put_line('Employee Number :'||emp_rec.emp_num);
dbms_output.put_line('Employee Number :'||emp_rec.emp_name);
dbms_output.put_line('Employee Number :'||emp_rec.emp_sal);
dbms_output.put_line('Employee Number :'||emp_rec.dept_id);
END;
/
db小提琴here
【讨论】:
以上是关于接受用户的员工编号 从“员工”表中获取员工详细信息并将其存储到 PL SQL 记录类型变量并打印相同的主要内容,如果未能解决你的问题,请参考以下文章