Getting Started with gsqlcmd

Getting Started with gsqlcmd

gsqlcmd is a command-line tool for executing SQL scripts, downloading, converting, importing, and exporting data.

For example, the following commands import AAPL dividends from www.nasdaq.com into a database:

gsqlcmd download https://www.nasdaq.com/symbol/AAPL/dividend-history dividends.htm

gsqlcmd convert "SELECT 'AAPL' AS Symbol, RecordDate, CashAmount FROM dividends.htm" dividends.csv

gsqlcmd import db dividends.csv /table=dividends /merge

You can do this even in a single import command:

gsqlcmd import db "SELECT 'AAPL' AS Symbol, RecordDate, CashAmount FROM https://www.nasdaq.com/symbol/AAPL/dividend-history" /table=dividends /merge

gsqlcmd allows automating operations easily using task files.

For example, you can import dividends for all symbols from the symbols.txt file using a command:

gsqlcmd import db "SELECT :symbol AS Symbol, RecordDate, CashAmount FROM https://www.nasdaq.com/symbol/{symbol}/dividend-history" /table=dividends /merge /taskfile=symbols.txt

In the examples above, gsqlcmd extracts data from an HTML page.

It also allows extracting data from plain text, CSV, XML, and JSON.

The examples above use the named connection, "db".

It can be a database on SQL Server, Oracle, DB2, MySQL, PostgreSQL, SQLite, or any other supported platform.

gsqlcmd supports executing SQL scripts for any supported platform. It understands platform-specific features and SQL dialects.

So, you can execute scripts using a command like this in the same manner for any platform:

gsqlcmd exec db application-install.sql

You can use gsqlcmd to convert and export data into plain text, CSV, HTML, XML, and JSON.

For example:

gsqlcmd exec db "SELECT * FROM dividends" dividends.txt
gsqlcmd exec db "SELECT * FROM dividends" dividends.csv
gsqlcmd exec db "SELECT * FROM dividends" dividends.htm
gsqlcmd exec db "SELECT * FROM dividends" dividends.xml
gsqlcmd exec db "SELECT * FROM dividends" dividends.json

You can tune results using a lot of formatting and template options.

As you can suppose, you can use the exec mode to export data and the import mode to import.

gsqlcmd Enterprise edition has an even better solution.

You can export and import data in a single command:

gsqlcmd db1 import "db2::SELECT * FROM dividends" /table=dividends /merge

Moreover, db1 and db2 can be on different database platforms like SQL Server and MySQL.

Of course, database tables can have different structures and column names.

We recommend trying examples shipped with the gsqlcmd download package. You will find a lot of templates for your solutions.

Also, take a look at the following topics: