Sitelet https://github.com/microsoft/SqlScriptDOM/issues/20
Skip to content

Provide an option in the SqlNNNScriptGenerator classes to preserve comments #20

Description

Is your feature request related to a problem? Please describe.
Some use cases for the Sql*ScriptGenerator classes are actually for code "pretty printing" / formatting. However, using Sql150ScriptGenerator to re-generate the SQL text when provided with an input TSqlFragment, leads to comments (single-line and multi-line) both being excluded from the output.

Describe the solution you'd like
Provide an PreserveComments property in SqlScriptGeneratorOptions with the default being true in the 160 version, and false in the 150 or lower versions for backward compat.

Describe alternatives you've considered
Token re-writing: substituting a faux print statement instead of the comment token is a crude workaround. It fails in case the comment is a trailing suffix of a line within a multi-line T-SQL statement.

Activity

  1. dmaloo commented on Oct 12, 2021

    @dmaloo

    Would really appreciate if this issue was fixed, it will help greatly with using scriptdom to fix code issues. thank you.

  2. tutuhub commented on Feb 9, 2022

    @tutuhub

    This fix would be a huge help! Not only for me but many others. Thank you.

  3. DBojsen commented on Sep 1, 2022

    @DBojsen

    Completely agree that we need an option to preserve comments when using the script generator.

  4. ay-azara commented on Jun 30, 2023

    @ay-azara

    Just in case someone wants to tackle this the filtering seems to occur here.

    if (token.TokenType != TSqlTokenType.SingleLineComment &&
    token.TokenType != TSqlTokenType.MultilineComment &&
    token.TokenType != TSqlTokenType.WhiteSpace)
    break;

    The only place I found TSqlTokenFilter called was SqlScriptDom/Parser/TSql/TSql80ParserBaseInternal.cs which is inherited by all the other Parsers so this seems to go deeper than ScriptGenerator options.

    EDIT: I guess if ScriptGenerator controls the creation of the parser an option could be created for TsqlNNNParser but I can't figure out where that class is getting called.

    So this

    -- This is a comment
    SELECT * FROM mytable

    results in

    $Results.ParsedObjects.Batches[0].ScriptTokenStream[0] // Value: SingleLineComment
    $Results.ParsedObjects.Batches[0].ScriptTokenStream[1] // Value: Whitespace Newline
    $Results.ParsedObjects.Batches[0].ScriptTokenStream[2] // Value: SELECT
    $Results.ParsedObjects.Batches[0].FirstTokenIndex // Value: 2

    Comments would need to be excluded from semicolon adding and it might cause some tests to fail if they aren't expecting comments.

    Fiddled around with Token rewriting but unlike Arvind I didn't have any luck. Modifying TokenType or Text on ScriptTokenStream tokens and passing them into the Generator produced the same result. TsqlFragement.Batches[0].Statements[0].Expression.Value can change some part of the statement but not the keyword. UpdateTokenInfo methods are internal.

  5. JerryNixon commented on Aug 14, 2024

    @JerryNixon

    Without this, most ScriptDom use cases are dead in the water.

  6. deepakjgupta commented on Jul 14, 2025

    @deepakjgupta

    Wow! It's been almost 4 years and this issue still hasn't been fixed?

  7. ALZDBA commented on Sep 4, 2025

    @ALZDBA

    This causes all tools relying on this SQL...ScriptGenerator to become useless !! ( e.g. SqlFormatter )

    Devs have put so much effort to (finally) put comments in their code, and now that is being removed by your component

    This needs to be fixed, as now SqlFormatter is added as an extension to SSMS 21!

  8. cstyve commented on Sep 10, 2025

    @cstyve

    Removing comments from code....absolutely the worst possible action to take. Code without comments can become useless to maintain.

  9. genesisjschubert commented on Sep 10, 2025

    @genesisjschubert

    Echoing ALZDBA's comment. If this is to be used by any tool for displaying sql to users, comments are the worst thing to remove.

  10. ErikEJ commented on Sep 10, 2025

    @ErikEJ
    Contributor

    Make sure to upvote/like the initial issue here

  11. clement911 commented on Nov 7, 2025

    @clement911

    I would like to second everyone here.
    I think that's one of the top voted features.
    Can this be prioritized?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions