The ability to export a script of database objects has for a long time been a feature of SQL Server Management Studio. With the new OSS cross platform tooling from Microsoft we can now have ths feature everywhere. mssql-scripter allows you to export schema and or data from SQL Server from pretty much any terminal
I do a lot of my SQL Server sandbox stuff on macOS using a Docker image of SQL Server 2017 with VS Code and the mssql plugin as my editor. With the new mssql-scripter tool I can now export SQL scripts of my database objects and data directly from the VS Code terminal.
For example here is my VS Code window listing all the databases in my Docker hosted SQL Server 2017 server…
From here I can open the terminal in VS Code and run the mssql-scripter tool to generate a new SQL Script called InitialDb.sql in my working directory…
Once this has run I get my new script file and inside is the SQL Script to create that database and all it’s objects…
This tool supports a great deal of arguments for specifying what you want to script from the database. For example we can Script Schema only, data only, Schema and Data then we can go down to exactly what types of objects to script for example Statistics, logins etc….
To get a full list of what mssql-scripter supports at the console run…
mssql-scripter –help
Here’s a small set of the arguments that –help lists…
Instructions for installing mssql-scripter on each platform can be found on their GitHub installation page