Fork me on GitHub

Request

A Request instance represents a request that can be executed on a connection.

new Request(sql, callback)

var Request = require('tedious').Request;

request = new Request("select 42, 'hello world'", function(err, rowCount) {
  ...
});
connection.execSql(request);
sql

The SQL to be executed (or a procedure name, if the request is to be used with connection.callProcedure).

callback
function (err, rowCount, rows) { }

The callback is called when the request has completed, either successfully or with an error. If an error occurs during execution of the statement(s), then err will describe the error.

As only one request at a time may be executed on a connection, another request should not be initiated until this callback is called.

err

If an error occured, a string with details of the error.

rowCount

The number of rows emitted as result of executing the SQL.

rows

Rows as a result of executing the SQL.

Will only be avaiable if Connection's config.options.rowCollectionOnRequestCompletion is true.

Event: columnMetadata

function (columns) { }

This event, describing result set columns, will be emitted before row events are emitted.

columns

An array like object, where the columns can be accessed either by index or name. Columns with a name that is an integer are not accessible by name, as it would be interpreted as an array index.

Each column has these properties.

colName
The column's name.
type.name
The column's type, such as VarChar, Int or Binary.
precision
The precision. Only applicable to types such as numeric and decimal.
scale
The scale. Only applicable to types such as numeric and decimal.
dataLength
The length, for types such as char, varchar, narchar and nvarchar.

Event: row

function (columns) { }

A row resulting from execution of the SQL.

columns

An array like object, where the columns can be accessed either by index or name. Each column has two properties, metadata and value.

metadata
The same data that is exposed in the columnMetadata event.
value

The column's value. It will be null for a NULL.

If there are multiple columns with the same name, then this will be an array of the values.

Event: done

This is a relatively low-level event, driven by the receipt of a TDS Done token. Most uses of Tedious can ignore this event, and should rely on the Request's callback function to know when the request has completed.

function (rowCount, more, rows) { }

All rows from a result set have been provided (through row events).

rowCount

The number of result rows. May be undefined if not available.

more

If there are more results to come (probably because multiple statements are being executed), then true.

rows

Rows as a result of executing the SQL.

Will only be avaiable if Connection's config.options.rowCollectionOnDone is true.

Event: doneInProc

This is a relatively low-level event, driven by the receipt of a TDS DoneInProc token. Most uses of Tedious can ignore this event, and should rely on the Request's callback function to know when the request has completed.

function (rowCount, more, rows) { }

All rows from a statement in a stored procedure have been provided (through row events).

rowCount

The number of result rows. May be undefined if not available.

more

If there are more result sets to come, then true.

rows

Rows as a result of executing the SQL.

Will only be avaiable if Connection's config.options.rowCollectionOnDone is true.

Event: doneProc

This is a relatively low-level event, driven by the receipt of a TDS DoneProc token. Most uses of Tedious can ignore this event, and should rely on the Request's callback function to know when the request has completed.

function (rowCount, more, returnStatus, rows) { }

A stored procedure has been completed.

rowCount

The number of result rows. May be undefined if not available.

more

If there are more result sets to come, then true.

returnStatus

The value returned from a stored procedure.

rows

Rows as a result of executing the SQL.

Will only be avaiable if Connection's config.options.rowCollectionOnDone is true.

Event: returnValue

function (parameterName, value, metadata) { }

A value for an output parameter (that was added to the request with addOutputParameter(...)).

See also Using Parameters.

parameterName

The parameter name. (Does not start with '@'.)

value

The parameter's output value.

metadata

The same data that is exposed in the columnMetadata event.

request.addParameter(name, type, value)

Add an input parameter to the request.

request.addParameter('city', TYPES.VarChar, 'London');

See also Using Parameters.

name

The parameter name. This should correspond to a parameter in the SQL, or a parameter that a called procedure expects.

The name should not start '@'.

type

One of the supported data types.

value

The value that the parameter is to be given. The Javascript type of the argument should match that documented for data types.

request.addOutputParameter(name, type)

Add an output parameter to the request.

request.addOutputParameter('id', TYPES.Int);

The parameter's value will be provide by an emitted returnValue event.

See also Using Parameters.

name

The parameter name. This should correspond to a parameter in the SQL, or a parameter that a called procedure expects.

type

One of the supported data types.