SQL Server
The sq SQL Server driver implements connectivity for
the Microsoft SQL Server
and
Azure SQL Edge
databases.
The driver implements all optional driver features.
Add source
Use sq add
to add a source. The location argument should
start with sqlserver://. For example:
sq add 'sqlserver://sakila:p_ssW0rd@localhost?database=sakila'
Notes
Active schema & catalog
SQL Server supports the concepts of schema and catalog .
When executing a sq query, you can use --src.schema to specify the active schema
(or catalog.schema).
$ sq src
@sakila/ms19
$ sq --src.schema=INFORMATION_SCHEMA '.TABLES'
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
sakila dbo rental BASE TABLE
sakila dbo customer_list VIEW
sakila dbo film_list VIEW
# output truncated ^^
When --src.schema is provided, sq explicitly renders the schema name in the generated SQL query.
So, the query above would be rendered to:
SELECT * FROM "INFORMATION_SCHEMA"."TABLES"
When --src.schema is not provided, sq doesn’t explicitly render a schema
name in the SQL query. The following two queries return the same results,
but are rendered differently:
-- $ sq '.actor'
SELECT * FROM "actor"
-- $ sq --src.schema=dbo '.actor'
SELECT * FROM "dbo"."actor"
schema() builtin caveat
For other database implementations, such as Postgres
,
sq implements --src.schema by setting the default schema
when opening the DB connection, in addition to explicitly
rendering the schema name in the SQL query.
However, SQL Server does not
support setting a default schema on a per-connection
basis; the default schema is a property of the DB user. Most of the time this different behavior
is moot. However, one consequence is that the SLQ
builtin schema() function always returns the
user’s default schema, regardless of the value of --src.schema.
$ sq -H 'schema()'
dbo
$ sq -H --src.schema=INFORMATION_SCHEMA 'schema()'
dbo
Inspect field provenance
sq inspect populates the fields below from SQL Server system catalog
views and the sp_spaceused system procedure.
Source-level fields
| Field | Source |
|---|---|
name, catalog | DB_NAME() |
schema | SCHEMA_NAME() (default schema for the connected user) |
user | not populated |
db_product | @@VERSION (full descriptive string) |
db_version | SERVERPROPERTY('ProductVersion') (numeric, e.g. 15.0.4123.1) |
size | SUM(size) * 8192 over sys.master_files WHERE database_id = DB_ID() — total bytes across all data files (size is in 8 KB pages) |
Per-table fields
| Field | Source |
|---|---|
row_count (tables) | sp_spaceused 'tbl' |
row_count (views) | live SELECT COUNT(*) (sp_spaceused does not return a row count for views) |
size | sp_spaceused 'tbl' (reserved / data / index breakdown) |