在 Oracle 中不可能将完全不同的数据集组合成一个块。匿名块用于执行一系列 PL/SQL 命令。一个查询(即任何返回结果集的查询,例如:)SELECT ...
只能返回一个结果,因此它需要是三个单独的查询。
如果您正在使用SQL*Plus
所有这些,则可以从命令行和/或使用.sql
脚本文件来完成。您也可以在命令行上指定参数(我不知道这是否是您对第一个查询的意思,但我还是添加了它)。
脚本(foo.sql
):
-- Set page size to something larger than the expected result set.
-- NOTE: You don't want to put 0 (unlimited) as it supresses the column headers.
SET pagesize 50000
-- Get the parameter from the command line:
VAR region VARCHAR2(256)
EXEC :region := '&1'
SELECT 'Processing for region: ' || :region
FROM dual;
-- Query 1
promp "Employee detail in specified region:"
select department_name, count(employee_id) "number of employees"
from HR.employees, HR.departments, HR.locations, HR.countries, HR.regions
where employees.DEPARTMENT_ID = departments.DEPARTMENT_ID
AND departments.location_ID = locations.location_ID
AND locations.country_ID = countries.country_ID
AND countries.region_ID = regions.region_ID
AND regions.region_name = :region
group by department_name
;
-- Query 2:
promp "Average salary by position:"
SELECT job_ID, AVG(salary) as "avg salary"
FROM HR.employees
GROUP BY job_ID;
-- Query 3:
promp "Average salaries by department:"
SELECT department_ID, AVG(salary) as "avg salary"
FROM HR.employees
GROUP BY department_ID;
exit
输出:
$ sqlplus myuser/mypass@lvm01xe @sample.sql Americas
SQL*Plus: Release 11.2.0.2.0 Production on Sun Nov 18 09:11:11 2012
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
PL/SQL procedure successfully completed.
'PROCESSINGFORREGION:'||:REGION
--------------------------------------------------------------------------------
Processing for region: Americas
"Employee detail in specified region:"
DEPARTMENT_NAME number of employees
------------------------------ -------------------
Administration 1
Accounting 2
IT 5
Purchasing 6
Executive 3
Shipping 45
Finance 6
Marketing 2
8 rows selected.
"Average salary by position:"
JOB_ID avg salary
---------- ----------
IT_PROG 5760
AC_MGR 12000
AC_ACCOUNT 8300
ST_MAN 7280
PU_MAN 11000
AD_ASST 4400
AD_VP 17000
SH_CLERK 3215
FI_ACCOUNT 7920
FI_MGR 12000
PU_CLERK 2780
SA_MAN 12200
MK_MAN 13000
PR_REP 10000
AD_PRES 24000
SA_REP 8350
MK_REP 6000
ST_CLERK 2785
HR_REP 6500
19 rows selected.
"Average salaries by department:"
DEPARTMENT_ID avg salary
------------- ----------
100 8600
30 4150
7000
90 19333.3333
20 9500
70 10000
110 10150
50 3475.55556
80 8955.88235
40 6500
60 5760
10 4400
12 rows selected.
Disconnected from Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production