Format code from the command line

The /formatsql command lets you format SQL code from the command line. You can apply formatting to a single file or to all files in a folder.

Syntax

To format a single file:

dbforgesql.com /formatsql /sourcefile:<file_path> [options]

To format multiple files in a folder:

dbforgesql.com /formatsql /sourcefolder:<folder_path> [options]

Get command-line help

To view the list of switches available for /formatsql, run the following command:

dbforgesql.com /formatsql /?

Command details

  • To run dbForge Studio CLI commands, open Command Prompt and navigate to the folder that contains dbforgesql.com. The default path to this executable depends on where dbForge Studio for SQL Server is installed. If you installed dbForge Studio:

    As a standalone tool

    C:\Program Files\Devart\dbForge Studio for SQL Server
    

    As part of the dbForge Edge bundle

    C:\Program Files\Devart\dbForge Edge\dbForge Studio for SQL Server
    
  • The command applies formatting according to the specified profile or default formatting settings if no profile is provided. For more information about formatting profiles, see Customize the formatting style with profiles.

  • You can format a single file or multiple files in a folder. To apply formatting to files located in subfolders, use the /includesubfolders argument.

  • By default, formatting results overwrite the source files. To preserve the original files, specify the /outputfile or /outputfolder argument.

Examples

Format a single file

To format a single file, run the command with the source file path.

dbforgesql.com /formatsql /sourcefile:"D:\Projects\Orders.sql"

This command formats Orders.sql and overwrites the source file with the formatted version.

To preserve the original file, add an output file path to the command:

dbforgesql.com /formatsql /sourcefile:"D:\Projects\Orders.sql" /outputfile:"D:\Projects\Orders_Formatted.sql"

This command formats Orders.sql and saves the formatted version as Orders_Formatted.sql. The source file remains unchanged.

Format files in a folder

When you need to format multiple files, you can process an entire folder and its subfolders at once.

dbforgesql.com /formatsql /sourcefolder:"D:\Projects\SalesDB" /outputfolder:"D:\Projects\SalesDB\Formatted" /includesubfolders

This command formats all files in the SalesDB folder and its subfolders, and saves the formatted files to the Formatted folder. The source files remain unchanged.

Apply custom formatting to a file

To apply custom formatting to a file, specify the formatting profile path using the /profile argument.

dbforgesql.com /formatsql /sourcefile:"D:\Projects\Procedures\UpdateOrder.sql" /outputfile:"D:\Projects\Procedures\UpdateOrder_Formatted.sql" /profile:"D:\Profiles\CompanyStandard.xml"

This command formats UpdateOrder.sql using the settings defined in CompanyStandard.xml.