Coding Ref

How to comment and uncomment in PostgreSQL

How to comment and uncomment in PostgreSQL

In PostgresSQL, there are two ways to comment code.

  1. Using the -- symbol
  2. Using the /* and */ symbols

Using --

You can add comments to your SQL code by using the -- symbol. This symbol indicates that everything following it on the same line should be treated as a comment, and it will be ignored by the PostgreSQL server when the code is executed.

Here is an example of how to add a comment to a SQL query in PostgreSQL:

SELECT * FROM users -- This is a comment
WHERE age > 18;

In this example, the line -- This is a comment will be ignored by the PostgreSQL server, and it will only execute the SELECT statement.

To uncomment a line that has been commented out using the -- symbol, simply remove the -- and any text following it on the same line. For example, if you have the following commented-out line in your SQL code:

-- SELECT * FROM users WHERE age > 18;*

You can uncomment it by removing the -- symbol and any text following it, like this:

SELECT * FROM users WHERE age > 18;

Once you have removed the -- symbol and any text following it, the line will no longer be treated as a comment, and it will be executed by the PostgreSQL server when the code is run.

Using /* and */ to add block comments

To add a block comment in PostgreSQL, you can use the /* and */ symbols. Anything between these symbols will be treated as a comment, and it will be ignored by the PostgreSQL server when the code is executed.

Here is an example of how to use a block comment:

/* This is a block comment
It can span multiple lines */
SELECT * FROM users
WHERE age > 18;

In this example, the lines /\* This is a block comment and It can span multiple lines \*/ will be ignored by the PostgreSQL server, and only the SELECT statement will be executed.

To uncomment a block comment, simply remove the /* and */ symbols, along with any text between them.

For example, if you have the following commented-out block of code:

/*
SELECT * FROM users
WHERE age > 18;
*/

You can uncomment it by removing the /\* and \*/ symbols, like this:

SELECT * FROM users
WHERE age > 18;

Once you have removed the /\* and \*/ symbols, the block of code will no longer be treated as a comment, and it will be executed by the PostgreSQL server when the code is run.