注释可以使应用程序更易于阅读和维护。例如,您可以在语句中用注释以描述该语句在应用程序中的用途。除 Hint 外,SQL 语句中的注释不会影响语句的执行。
注释可以出现在语句中的任何关键字、参数或标点符号之间。您可以通过两种方式在语句中添加注释:
以斜杠和星号(/*)为开头的注释。斜杠和星号后跟着注释的文本。此文本可以跨越多行,并用星号和斜杠(*/)结束注释。开头和结尾的符号不必与文本用空格或换行符进行分隔。
以两个连字符(—)为开头的注释。符号后跟着注释的文本。此文本不能扩展到新行,并以换行符结束注释。
一个 SQL 语句可以同时包含这两种风格的多个注释。注释的文本可以包含数据库字符集中的任何可打印字符。
以下示例展示了多种形式的以斜杠和星号(/*)为开头的注释:
SELECT last_name, employee_id, salary + NVL(commission_pct, 0),
job_id, e.department_id
/* Select all employees whose compensation is
greater than that of Pataballa.*/
FROM employees e, departments d
/*The DEPARTMENTS table is used to get the department name.*/
WHERE e.department_id = d.department_id
AND salary + NVL(commission_pct,0) > /* Subquery: */
(SELECT salary + NVL(commission_pct,0)
/* total compensation is salary + commission_pct */
FROM employees
WHERE last_name = 'Pataballa')
ORDER BY last_name, employee_id;
以下示例中的语句包含多种形式的以两个连字符(- -)为开头的注释:
SELECT last_name, -- select the name
employee_id -- employee id
salary + NVL(commission_pct, 0), -- total compensation
job_id, -- job
e.department_id -- and department
FROM employees e, -- of all employees
departments d
WHERE e.department_id = d.department_id
AND salary + NVL(commission_pct, 0) > -- whose compensation
-- is greater than
(SELECT salary + NVL(commission_pct,0) -- the compensation
FROM employees
WHERE last_name = 'Pataballa') -- of Pataballa
ORDER BY last_name -- and order by last name
employee_id -- and employee id.
;