MSSQL Query

Runs a SQL statement against Microsoft SQL Server. Returns the rows as Arrow RecordBatch.

Configuration

- mssql_query:
    name: get_orders
    credentials_path: /etc/mssql/credentials.json
    query: "SELECT * FROM orders WHERE date = @P1"
    parameters:
      - "{{event.data.date}}"
    batch_size: 5000

Fields

FieldTypeDefaultDescription
namestringrequiredTask name.
credentials_pathstringrequiredPath to the credentials file.
querystring/resourcerequiredSQL statement. Supports templating and resource files.
parameterslistValues for @P1, @P2, etc. Sent as text; the server converts them to the types the statement needs.
batch_sizeint10000Rows per Arrow RecordBatch.
max_connectionsint10Connection pool size.
connection_timeoutduration30sTimeout to open a connection and log in.
query_timeoutduration2mTimeout to get a connection and the first result, and for each later answer of the server while the rows are read.
depends_onlistUpstream task names.
retryobjectRetry configuration.

Connection errors, deadlocks, lock timeouts, and the server errors Microsoft lists as transient (such as a busy or unavailable Azure SQL database) are retried. SQL errors such as a syntax error or a constraint violation are not, nor is a statement that ran past query_timeout, nor a query that failed after it sent batches.

Credentials file

{
  "host": "sql-server.example.com",
  "port": 1433,
  "database": "mydb",
  "username": "user",
  "password": "pass",
  "trust_server_certificate": false,
  "encrypt": true
}
FieldDefaultDescription
port1433Server port.
trust_server_certificatefalseAccept the server certificate without validating it, for self-signed certificates.
encrypttrueEncrypt the whole connection. With false, only the login is encrypted.

Output

Rows are streamed and emitted as one event per batch_size rows, from the statement’s first result. A query without rows emits one empty batch with the result columns. A statement without a result set, such as an INSERT, emits one empty batch.

SQL ServerArrow
bitBoolean
tinyintUInt8
smallint, intInt16, Int32
bigint, and a nullable integer column of any widthInt64
realFloat32
float, money, smallmoneyFloat64
decimal, numericUtf8, exact decimal text
char, varchar, nchar, nvarchar, text, ntext, xml, uniqueidentifierUtf8
dateDate32
timeTime64(Microsecond)
datetime, smalldatetime, datetime2Timestamp(Microsecond)
datetimeoffsetTimestamp(Microsecond, UTC)
binary, varbinary, imageBinary

Other types, such as sql_variant and user-defined types, fail the event with the column name. Convert them in the query, for example CAST(value AS nvarchar(max)). For exact money values, cast them to decimal(19, 4).