EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 7369 SMITH CLERK 7902 17-DEC-80 800 NULL 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7565 EMILY SALESMAN 7698 20-FEB-81 1250 800 30 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7655 JONES MANAGER 7839 2-APR-81 2975 NULL 20 7698 BLAKE MANAGER 7839 1-MAY-91 2850 NULL 30 7782 CLARK MANAGER 7839 9-JUN-81 2450 NULL 10 7788 SCOTT ANALYST 7655 21-MAR-87 3000 NULL 20 7844 TURNER SALESMAN 7698 18-SEP-81 1500 NULL 30 7876 ADAMS CLERK 7788 24-APR-87 1100 NULL 20 7900 JAMES CLERK 7698 3-DEC-81 950 NULL 30 7902 FORD ANALYST 7655 3-DEC-81 3000 NULL 20 7934 MILLER CLERK 7782 3-JAN-81 1300 NULL 10 Given this table called 'EMP', please create a stored procedure, given a department number, pass the number of employees and the total salary of all employees in it out to the calling statement using output parameters.
时间: 2024-02-10 17:07:16 浏览: 90
Sure, here's a stored procedure that takes in a department number and returns the number of employees and total salary of all employees in that department:
```
CREATE PROCEDURE sp_get_dept_stats
@deptno INT,
@num_employees INT OUTPUT,
@total_salary DECIMAL(10,2) OUTPUT
AS
BEGIN
SELECT @num_employees = COUNT(*), @total_salary = SUM(SAL)
FROM EMP
WHERE DEPTNO = @deptno
END
```
To call this stored procedure and get the output parameters, you can use the following code:
```
DECLARE @num_employees INT, @total_salary DECIMAL(10,2)
EXEC sp_get_dept_stats 30, @num_employees OUTPUT, @total_salary OUTPUT
SELECT @num_employees AS NumEmployees, @total_salary AS TotalSalary
```
This will call the stored procedure with department number 30 and output the number of employees and total salary for that department.
阅读全文