Are there multiline comment delimiters in SQL that are vendor agnostic?

20,604

Solution 1

C style comments are standard in SQL 2003 and SQL 2008 (but not in SQL 1999 or before). The following DBMS all support C style comments:

  • Informix
  • PostgreSQL
  • MySQL
  • Oracle
  • DB2
  • Sybase
  • Ingres
  • Microsoft SQL Server
  • SQLite (3.7.2 and later)
  • Teradata and Aster

That is not every possible DBMS, but it is more or less every major SQL DBMS. (I'll willingly add notes about any other DBMS that does - or does not - support C style comments.)

The SQL 2003 standard documents comment notations thus:

<comment> ::= <simple comment> | <bracketed comment>

<simple comment> ::=
    <simple comment introducer> [ <comment character> ... ] <newline>

<simple comment introducer> ::=
    <minus sign> <minus sign> [ <minus sign> ... ]

<bracketed comment> ::=
    <bracketed comment introducer> <bracketed comment contents>
    <bracketed comment terminator>

<bracketed comment introducer> ::= <slash> <asterisk>

<bracketed comment terminator> ::= <asterisk> <slash>

<bracketed comment contents> ::= [ { <comment character> | <separator> }... ]

<comment character> ::= <nonquote character> | <quote>

IBM Informix Dynamic Server (IDS or Informix) supports C style comments. It also supports '{ ... }' as potentially multi-line comments, except in contexts where it means something else - that something else being a LIST or SET or MULTISET literal. (You might find that DB-Access gets confused by C style comments; that is a separate issue.)

Solution 2

The ISO standard only defines single-line comments beginning with two dashes. Some vendors support C-style commenting (i.e. multiline beginning with /* and ending with */), but it should not be considered "vendor agnostic".

Share:
20,604
ojblass
Author by

ojblass

Only a little special.

Updated on August 12, 2022

Comments

  • ojblass
    ojblass over 1 year

    While editing, building up, or cherry picking from SQL statements I can find myself interacting with one of four popular database tools. I have been resorting to single line commenting for DB2 and Informix. I have created macros in vim to make this slightly more efficient but I was wondering if I am working too hard.

  • Ben Hoffstein
    Ben Hoffstein about 15 years
    Fair enough, it was not clear which SQL standard was being discussed (and my information was outdated). Nonetheless, I still assert that C-style comments are not completely vendor agnostic.
  • ojblass
    ojblass about 15 years
    I think the best I can hope for is SQL 1999 :( well at least I have vim everywhere.
  • Jonathan Leffler
    Jonathan Leffler about 15 years
    It depends on the versions you are using, but I think you'll find that the products are way ahead of the standard. I found comments from 2003 for MySQL having (minor) issues with /* comments */, but utility level, not server level. I think you'll find that most servers have supported it since 2000.
  • Mechanical snail
    Mechanical snail over 12 years
    sqlite (version 3.7.2) also supports them.
  • jyoungdev
    jyoungdev almost 12 years
    It seems DB2 does not support C-style comments: 3rdstage.blogspot.com/2009/04/…
  • Jonathan Leffler
    Jonathan Leffler almost 12 years
    @apollodude217: I don't have DB2 to play with to double check, but this section on SQL comments in the SQL manual indicates that it does. I don't think it is a new feature in DB2 10.1, for all that it is the 10.1 manual I'm pointing at. (PS: Please let me know if that's not a public link.)
  • jyoungdev
    jyoungdev almost 12 years
    @JonathanLeffler Hmmm, you're right. Previously, I had tried a C-style comment in a query in Control Center and it looked like the parser did not recognize it as a comment, but I just tried again with a different query and it worked. Must have had the syntax goofed up.