Using Comments
As you have seen, SQL statements are instructions that Oracle processes. But what if you wanted to include text that you do not want processed and executed? Why would you ever want to do this? Here are a few reasons:
- The SQL statements we’ve been using here are all very short and very simple. But, as your SQL statement grows (in length and complexity), you’ll want to include descriptive comments (for your own future reference or for whoever has to work on the project next). You need to embed these comments in the SQL scripts, but they are obviously not intended for Oracle processing. (For an example of this, see the create.sql and populate.sql files you used in Lesson 3.)
- The same is true for headers at the top of a SQL file, perhaps containing the programmer contact information and a description and notes. (You also see this use case in the create.sql and populate.sql files.)
- Another important use for comments is to temporarily stop SQL code from being executed. If you were working with a long SQL statement and wanted to test just part of it, you could comment out some of the code so that Oracle saw it as comments and ignored it.
Oracle supports two forms of comment syntax. We’ll start with inline comments:
Input
SELECT prod_name -- this is a comment FROM products;
Analysis
You may embed comments inline using -- (two hyphens). Anything after the -- is considered comment text, making this a good option for describing columns in a CREATE TABLE statement, for example.
Here is another form of inline comment:
Input
-- This is a comment SELECT prod_name FROM products;
Analysis
A -- at the start of a line makes the entire line a comment. You can see this format comment used in the accompanying create.sql and populate.sql scripts.
You can also create multi-line comments, and comments that stop and start anywhere within the script:
Input
/* SELECT prod_name, vend_id FROM products; */ SELECT prod_name FROM products;
Analysis
/* starts a comment, and */ ends it. Anything between /* and */ is comment text. This type of comment is often used to comment out code, as shown in this example. Here, two SELECT statements are defined, but the first won’t execute because it has been commented out.