Pretty Printer for T-SQL?

后端 未结 9 2001
野趣味
野趣味 2021-02-04 06:56

I\'m looking for a good T-SQL Pretty Printer so that all the code looks consistent between developers in our project. Preferably a free/open source one, but paid for isn\'t out

相关标签:
9条回答
  • 2021-02-04 07:39

    A free and open source alternative to the paid for options that everyone else has listed is Poor Man's T-SQL Formatter. It integrates nicely with SQL Server Management Studio. The options for configuring how the SQL is formatted are somewhat limited when compared to the other tools, but for the price it's hard to complain, and if you really want to change it, the source is available for you to tweak.

    0 讨论(0)
  • 2021-02-04 07:40

    I had the same issue but I was looking for a command line tool. I have finally coded a Python script based on sqlparse that is a Python package mature (10+ years), still very active and aiming to parse and format SQL statements.

    In post here my tiny Python script pretty_print_sql.py in case other users would be interested:

    import argparse
    import sqlparse
    
    # Parse command line arguments
    parser = argparse.ArgumentParser(prog="pretty_print_sql")
    parser.add_argument("file", type=argparse.FileType("r"), nargs="+")
    args = parser.parse_args()
    
    # Pretty print input files
    for file in args.file:
        print(sqlparse.format(file.read(), reindent=True, keyword_case='upper'))
    

    To use it:

    pretty_print_sql.py input.sql > pretty-output.sql
    

    To install sqlparse using pip for personal usage:

    python3 -m pip install sqlparse --user --upgrade
    

    To install sqlparse using pipenv (within a project):

    python3 -m pipenv install sqlparse
    
    0 讨论(0)
  • 2021-02-04 07:40

    Other than SQLinForm, Toad for SQL Server comes with an SQL formatter out of the box, among plenty of other advantages over management studio...

    0 讨论(0)
提交回复
热议问题