node-mssql
Microsoft SQL Server client for Node.js
v4 upgrade warning - Version 4 contains many breaking changes, read more in the 3.x to 4.x changes section. Version 3 docs are available here.
Supported TDS drivers:
- Tedious (pure JavaScript - Windows/macOS/Linux, default)
- Microsoft / Contributors Node V8 Driver for Node.js for SQL Server (native - Windows only)
Installation
IMPORTANT: Requires Node.js 4 or newer.
npm install mssql
Quick Example
const sql = async { try const pool = await sql const result = await sqlquery`select * from mytable where id = ` consoledirresult catch err // ... error checks }
If you're on Windows Azure, add ?encrypt=true
to your connection string. See docs to learn more.
Documentation
Examples
Configuration
Drivers
Connections
Requests
Transactions
Prepared Statements
Other
- CLI
- Geography and Geometry
- Table-Valued Parameter
- Affected Rows
- JSON support
- Errors
- Informational messages
- Metadata
- Data Types
- SQL injection
- Known Issues
- Contributing
Examples
Config
const config = user: '...' password: '...' server: 'localhost' // You can use 'localhost\\instance' to connect to named instance database: '...' options: encrypt: true // Use this if you're on Windows Azure
Async/Await
const sql = { try let pool = await sql let result1 = await pool input'input_parameter' sqlInt value consoledirresult1 // Stored procedure let result2 = await pool input'input_parameter' sqlInt value consoledirresult2 catch err // ... error checks } sql
Promises
const sql = sql sql
Native Promise is used by default. You can easily change this with sql.Promise = require('myownpromisepackage')
.
ES6 Tagged template literals
const sql = sql sql
All values are automatically sanitized against sql injection.
Callbacks
const sql = sql sql
Streaming
If you plan to work with large amount of rows, you should always use streaming. Once you enable this, you must listen for events to receive data.
const sql = sql sql
Connection Pools
const sql = const pool1 = config { // ... error checks // Query pool1 // or: new sql.Request(pool1) } pool1 const pool2 = config { // ... error checks // Stored Procedure pool2 // or: new sql.Request(pool2) input'input_parameter' sqlInt 10 } pool2
ES6 Tagged template literals
config
All values are automatically sanitized against sql injection.
Configuration
const config = user: '...' password: '...' server: 'localhost' database: '...' pool: max: 10 min: 0 idleTimeoutMillis: 30000
General (same for all drivers)
- user - User name to use for authentication.
- password - Password to use for authentication.
- server - Server to connect to. You can use 'localhost\instance' to connect to named instance.
- port - Port to connect to (default:
1433
). Don't set when connecting to named instance. - domain - Once you set domain, driver will connect to SQL Server using domain login.
- database - Database to connect to (default: dependent on server configuration).
- connectionTimeout - Connection timeout in ms (default:
15000
). - requestTimeout - Request timeout in ms (default:
15000
). NOTE: msnodesqlv8 driver doesn't support timeouts < 1 second. - stream - Stream recordsets/rows instead of returning them all at once as an argument of callback (default:
false
). You can also enable streaming for each request independently (request.stream = true
). Always set totrue
if you plan to work with large amount of rows. - parseJSON - Parse JSON recordsets to JS objects (default:
false
). For more information please see section JSON support. - pool.max - The maximum number of connections there can be in the pool (default:
10
). - pool.min - The minimum of connections there can be in the pool (default:
0
). - pool.idleTimeoutMillis - The Number of milliseconds before closing an unused connection (default:
30000
).
Complete list of pool options can be found here.
Formats
In addition to configuration object there is an option to pass config as a connection string. Two formats of connection string are supported.
Classic Connection String
Server=localhost,1433;Database=database;User Id=username;Password=password;Encrypt=true
Driver=msnodesqlv8;Server=(local)\INSTANCE;Database=database;UID=DOMAIN\username;PWD=password;Encrypt=true
Connection String URI
mssql://username:password@localhost:1433/database?encrypt=true
mssql://username:password@localhost/INSTANCE/database?encrypt=true&domain=DOMAIN&driver=msnodesqlv8
Version
2.5
Drivers
Tedious
Default driver, actively maintained and production ready. Platform independent, runs everywhere Node.js runs. Officially supported by Microsoft.
Extra options:
- options.instanceName - The instance name to connect to. The SQL Server Browser service must be running on the database server, and UDP port 1444 on the database server must be reachable.
- options.useUTC - A boolean determining whether or not use UTC time for values without time zone offset (default:
true
). - options.encrypt - A boolean determining whether or not the connection will be encrypted (default:
false
). - options.tdsVersion - The version of TDS to use (default:
7_4
, available:7_1
,7_2
,7_3_A
,7_3_B
,7_4
). - options.appName - Application name used for SQL server logging.
- options.abortTransactionOnError - A boolean determining whether to rollback a transaction automatically if any error is encountered during the given transaction's execution. This sets the value for
XACT_ABORT
during the initial SQL phase of a connection.
More information about Tedious specific options: http://tediousjs.github.io/tedious/api-connection.html
Microsoft / Contributors Node V8 Driver for Node.js for SQL Server
Requires Node.js 0.12.x or newer. Windows only. This driver is not part of the default package and must be installed separately by npm install msnodesqlv8
. To use this driver, use this require syntax: const sql = require('mssql/msnodesqlv8')
.
Extra options:
- connectionString - Connection string (default: see below).
- options.instanceName - The instance name to connect to. The SQL Server Browser service must be running on the database server, and UDP port 1444 on the database server must be reachable.
- options.trustedConnection - Use Windows Authentication (default:
false
). - options.useUTC - A boolean determining whether or not to use UTC time for values without time zone offset (default:
true
).
Default connection string when connecting to port:
Driver={SQL Server Native Client 11.0};Server={#{server},#{port}};Database={#{database}};Uid={#{user}};Pwd={#{password}};Trusted_Connection={#{trusted}};
Default connection string when connecting to named instance:
Driver={SQL Server Native Client 11.0};Server={#{server}\\#{instance}};Database={#{database}};Uid={#{user}};Pwd={#{password}};Trusted_Connection={#{trusted}};
Connections
Internally, each ConnectionPool
instance is a separate pool of TDS connections. Once you create a new Request
/Transaction
/Prepared Statement
, a new TDS connection is acquired from the pool and reserved for desired action. Once the action is complete, connection is released back to the pool. Connection health check is built-in so once the dead connection is discovered, it is immediately replaced with a new one.
IMPORTANT: Always attach an error
listener to created connection. Whenever something goes wrong with the connection it will emit an error and if there is no listener it will crash your application with an uncaught error.
const pool = /* config */
Events
- error(err) - Dispatched on connection error.
connect ([callback])
Create a new connection pool. The initial probe connection is created to find out whether the configuration is valid.
Arguments
- callback(err) - A callback which is called after initial probe connection has established, or an error has occurred. Optional. If omitted, returns Promise.
Example
const pool = user: '...' password: '...' server: 'localhost' database: '...' pool
Errors
- ELOGIN (
ConnectionError
) - Login failed. - ETIMEOUT (
ConnectionError
) - Connection timeout. - EALREADYCONNECTED (
ConnectionError
) - Database is already connected! - EALREADYCONNECTING (
ConnectionError
) - Already connecting to database! - EINSTLOOKUP (
ConnectionError
) - Instance lookup failed. - ESOCKET (
ConnectionError
) - Socket error.
close()
Close all active connections in the pool.
Example
pool
Request
const request = /* [pool or transaction] */
If you omit pool/transaction argument, global pool is used instead.
Events
- recordset(columns) - Dispatched when metadata for new recordset are parsed.
- row(row) - Dispatched when new row is parsed.
- done(returnValue) - Dispatched when request is complete.
- error(err) - Dispatched on error.
- info(message) - Dispatched on informational message.
execute (procedure, [callback])
Call a stored procedure.
Arguments
- procedure - Name of the stored procedure to be executed.
- callback(err, recordsets, returnValue) - A callback which is called after execution has completed, or an error has occurred.
returnValue
is also accessible as property of recordsets. Optional. If omitted, returns Promise.
Example
const request = requestinput'input_parameter' sqlInt valuerequestrequest
Errors
- EREQUEST (
RequestError
) - Message from SQL Server - ECANCEL (
RequestError
) - Cancelled. - ETIMEOUT (
RequestError
) - Request timeout. - ENOCONN (
RequestError
) - No connection is specified for that request. - ENOTOPEN (
ConnectionError
) - Connection not yet open. - ECONNCLOSED (
ConnectionError
) - Connection is closed. - ENOTBEGUN (
TransactionError
) - Transaction has not begun. - EABORT (
TransactionError
) - Transaction was aborted (by user or because of an error).
input (name, [type], value)
Add an input parameter to the request.
Arguments
- name - Name of the input parameter without @ char.
- type - SQL data type of input parameter. If you omit type, module automatically decide which SQL data type should be used based on JS data type.
- value - Input parameter value.
undefined
ansNaN
values are automatically converted tonull
values.
Example
requestinput'input_parameter' valuerequestinput'input_parameter' sqlInt value
JS Data Type To SQL Data Type Map
String
->sql.NVarChar
Number
->sql.Int
Boolean
->sql.Bit
Date
->sql.DateTime
Buffer
->sql.VarBinary
sql.Table
->sql.TVP
Default data type for unknown object is sql.NVarChar
.
You can define your own type map.
sqlmap
You can also overwrite the default type map.
sqlmap
Errors (synchronous)
- EARGS (
RequestError
) - Invalid number of arguments. - EINJECT (
RequestError
) - SQL injection warning.
output (name, type, [value])
Add an output parameter to the request.
Arguments
- name - Name of the output parameter without @ char.
- type - SQL data type of output parameter.
- value - Output parameter value initial value.
undefined
andNaN
values are automatically converted tonull
values. Optional.
Example
requestrequest
Errors (synchronous)
- EARGS (
RequestError
) - Invalid number of arguments. - EINJECT (
RequestError
) - SQL injection warning.
pipe (stream)
Sets request to stream
mode and pulls all rows from all recordsets to a given stream.
Arguments
- stream - Writable stream in object mode.
Example
const request = requestrequeststreamstream
Version
2.0
query (command, [callback])
Execute the SQL command. To execute commands like create procedure
or if you plan to work with local temporary tables, use batch instead.
Arguments
- command - T-SQL command to be executed.
- callback(err, recordset) - A callback which is called after execution has completed, or an error has occurred. Optional. If omitted, returns Promise.
Example
const request = request
Errors
- ETIMEOUT (
RequestError
) - Request timeout. - EREQUEST (
RequestError
) - Message from SQL Server - ECANCEL (
RequestError
) - Cancelled. - ENOCONN (
RequestError
) - No connection is specified for that request. - ENOTOPEN (
ConnectionError
) - Connection not yet open. - ECONNCLOSED (
ConnectionError
) - Connection is closed. - ENOTBEGUN (
TransactionError
) - Transaction has not begun. - EABORT (
TransactionError
) - Transaction was aborted (by user or because of an error).
const request = requestmultiple = true request
NOTE: To get number of rows affected by the statement(s), see section Affected Rows.
batch (batch, [callback])
Execute the SQL command. Unlike query, it doesn't use sp_executesql
, so is not likely that SQL Server will reuse the execution plan it generates for the SQL. Use this only in special cases, for example when you need to execute commands like create procedure
which can't be executed with query or if you're executing statements longer than 4000 chars on SQL Server 2000. Also you should use this if you're plan to work with local temporary tables (more information here).
NOTE: Table-Valued Parameter (TVP) is not supported in batch.
Arguments
- batch - T-SQL command to be executed.
- callback(err, recordset) - A callback which is called after execution has completed, or an error has occurred. Optional. If omitted, returns Promise.
Example
const request = request
Errors
- ETIMEOUT (
RequestError
) - Request timeout. - EREQUEST (
RequestError
) - Message from SQL Server - ECANCEL (
RequestError
) - Cancelled. - ENOCONN (
RequestError
) - No connection is specified for that request. - ENOTOPEN (
ConnectionError
) - Connection not yet open. - ECONNCLOSED (
ConnectionError
) - Connection is closed. - ENOTBEGUN (
TransactionError
) - Transaction has not begun. - EABORT (
TransactionError
) - Transaction was aborted (by user or because of an error).
You can enable multiple recordsets in queries with the request.multiple = true
command.
bulk (table, [callback])
Perform a bulk insert.
Arguments
- table -
sql.Table
instance. - callback(err, rowCount) - A callback which is called after bulk insert has completed, or an error has occurred. Optional. If omitted, returns Promise.
Example
const table = 'table_name' // or temporary table, e.g. #temptabletablecreate = truetablecolumnstablecolumnstablerows const request = request
IMPORTANT: Always indicate whether the column is nullable or not!
TIP: If you set table.create
to true
, module will check if the table exists before it start sending data. If it doesn't, it will automatically create it. You can specify primary key columns by setting primary: true
to column's options. Primary key constraint on multiple columns is supported.
TIP: You can also create Table variable from any recordset with recordset.toTable()
.
Errors
- ENAME (
RequestError
) - Table name must be specified for bulk insert. - ETIMEOUT (
RequestError
) - Request timeout. - EREQUEST (
RequestError
) - Message from SQL Server - ECANCEL (
RequestError
) - Cancelled. - ENOCONN (
RequestError
) - No connection is specified for that request. - ENOTOPEN (
ConnectionError
) - Connection not yet open. - ECONNCLOSED (
ConnectionError
) - Connection is closed. - ENOTBEGUN (
TransactionError
) - Transaction has not begun. - EABORT (
TransactionError
) - Transaction was aborted (by user or because of an error).
cancel()
Cancel currently executing request. Return true
if cancellation packet was send successfully.
Example
const request = request request
Transaction
IMPORTANT: always use Transaction
class to create transactions - it ensures that all your requests are executed on one connection. Once you call begin
, a single connection is acquired from the connection pool and all subsequent requests (initialized with the Transaction
object) are executed exclusively on this connection. After you call commit
or rollback
, connection is then released back to the connection pool.
const transaction = /* [pool] */
If you omit connection argument, global connection is used instead.
Example
const transaction = /* [pool] */transaction
Transaction can also be created by const transaction = pool.transaction()
. Requests can also be created by const request = transaction.request()
.
Aborted transactions
This example shows how you should correctly handle transaction errors when abortTransactionOnError
(XACT_ABORT
) is enabled. Added in 2.0.
const transaction = /* [pool] */transaction
Events
- begin - Dispatched when transaction begin.
- commit - Dispatched on successful commit.
- rollback(aborted) - Dispatched on successful rollback with an argument determining if the transaction was aborted (by user or because of an error).
begin ([isolationLevel], [callback])
Begin a transaction.
Arguments
- isolationLevel - Controls the locking and row versioning behavior of TSQL statements issued by a connection. Optional.
READ_COMMITTED
by default. For possible values seesql.ISOLATION_LEVEL
. - callback(err) - A callback which is called after transaction has began, or an error has occurred. Optional. If omitted, returns Promise.
Example
const transaction = transaction
Errors
- ENOTOPEN (
ConnectionError
) - Connection not yet open. - EALREADYBEGUN (
TransactionError
) - Transaction has already begun.
commit ([callback])
Commit a transaction.
Arguments
- callback(err) - A callback which is called after transaction has committed, or an error has occurred. Optional. If omitted, returns Promise.
Example
const transaction = transaction
Errors
- ENOTBEGUN (
TransactionError
) - Transaction has not begun. - EREQINPROG (
TransactionError
) - Can't commit transaction. There is a request in progress.
rollback ([callback])
Rollback a transaction. If the queue isn't empty, all queued requests will be Cancelled and the transaction will be marked as aborted.
Arguments
- callback(err) - A callback which is called after transaction has rolled back, or an error has occurred. Optional. If omitted, returns Promise.
Example
const transaction = transaction
Errors
- ENOTBEGUN (
TransactionError
) - Transaction has not begun. - EREQINPROG (
TransactionError
) - Can't rollback transaction. There is a request in progress.
Prepared Statement
IMPORTANT: always use PreparedStatement
class to create prepared statements - it ensures that all your executions of prepared statement are executed on one connection. Once you call prepare
, a single connection is acquired from the connection pool and all subsequent executions are executed exclusively on this connection. After you call unprepare
, the connection is then released back to the connection pool.
const ps = /* [pool] */
If you omit the connection argument, the global connection is used instead.
Example
const ps = /* [pool] */psinput'param' sqlIntps
IMPORTANT: Remember that each prepared statement means one reserved connection from the pool. Don't forget to unprepare a prepared statement!
TIP: You can also create prepared statements in transactions (new sql.PreparedStatement(transaction)
), but keep in mind you can't execute other requests in the transaction until you call unprepare
.
input (name, type)
Add an input parameter to the prepared statement.
Arguments
- name - Name of the input parameter without @ char.
- type - SQL data type of input parameter.
Example
psinput'input_parameter' sqlIntpsinput'input_parameter' sql
Errors (synchronous)
- EARGS (
PreparedStatementError
) - Invalid number of arguments. - EINJECT (
PreparedStatementError
) - SQL injection warning.
output (name, type)
Add an output parameter to the prepared statement.
Arguments
- name - Name of the output parameter without @ char.
- type - SQL data type of output parameter.
Example
psps
Errors (synchronous)
- EARGS (
PreparedStatementError
) - Invalid number of arguments. - EINJECT (
PreparedStatementError
) - SQL injection warning.
prepare (statement, [callback])
Prepare a statement.
Arguments
- statement - T-SQL statement to prepare.
- callback(err) - A callback which is called after preparation has completed, or an error has occurred. Optional. If omitted, returns Promise.
Example
const ps = ps
Errors
- ENOTOPEN (
ConnectionError
) - Connection not yet open. - EALREADYPREPARED (
PreparedStatementError
) - Statement is already prepared. - ENOTBEGUN (
TransactionError
) - Transaction has not begun.
execute (values, [callback])
Execute a prepared statement.
Arguments
- values - An object whose names correspond to the names of parameters that were added to the prepared statement before it was prepared.
- callback(err) - A callback which is called after execution has completed, or an error has occurred. Optional. If omitted, returns Promise.
Example
const ps = psinput'param' sqlIntps
You can also stream executed request.
const ps = psinput'param' sqlIntps
TIP: To learn more about how number of affected rows works, see section Affected Rows.
Errors
- ENOTPREPARED (
PreparedStatementError
) - Statement is not prepared. - ETIMEOUT (
RequestError
) - Request timeout. - EREQUEST (
RequestError
) - Message from SQL Server - ECANCEL (
RequestError
) - Cancelled.
unprepare ([callback])
Unprepare a prepared statement.
Arguments
- callback(err) - A callback which is called after unpreparation has completed, or an error has occurred. Optional. If omitted, returns Promise.
Example
const ps = psinput'param' sqlIntps
Errors
- ENOTPREPARED (
PreparedStatementError
) - Statement is not prepared.
CLI
Before you can start using CLI, you must install mssql
globally with npm install mssql -g
. Once you do that you will be able to execute mssql
command.
Setup
Create a .mssql.json
configuration file (anywhere). Structure of the file is the same as the standard configuration object.
Example
echo "select * from mytable" | mssql /path/to/config
Results in:
You can also query for multiple recordsets.
echo "select * from mytable; select * from myothertable" | mssql
Results in:
If you omit config path argument, mssql will try to load it from current working directory.
Version
2.0
Geography and Geometry
node-mssql has built-in serializer for Geography and Geometry CLR data types.
select geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326)select geometry::STGeomFromText('LINESTRING (100 100 10.3 12, 20 180, 180 180)', 0)
Results in:
srid: 4326 version: 1 points: x: 47656 y: -12236 x: 47656 y: -122343 figures: attribute: 1 pointOffset: 0 shapes: parentOffset: -1 figureOffset: 0 type: 2 segments: srid: 0 version: 1 points: x: 100 y: 100 z: 103 m: 12 x: 20 y: 180 z: NaN m: NaN x: 180 y: 180 z: NaN m: NaN figures: attribute: 1 pointOffset: 0 shapes: parentOffset: -1 figureOffset: 0 type: 2 segments:
Table-Valued Parameter (TVP)
Supported on SQL Server 2008 and later. You can pass a data table as a parameter to stored procedure. First, we have to create custom type in our database.
AS TABLE ( a VARCHAR(50), b INT );
Next we will need a stored procedure.
CREATE PROCEDURE MyCustomStoredProcedure (@tvp TestType readonly) AS SELECT * FROM @tvp
Now let's go back to our Node.js app.
const tvp = // Columns must correspond with type we have created in database.tvpcolumnstvpcolumns // Add rowstvprows // Values are in same order as columns.
You can send table as a parameter to stored procedure.
const request = requestinput'tvp' tvprequest
TIP: You can also create Table variable from any recordset with recordset.toTable()
.
Affected Rows
If you're performing INSERT
, UPDATE
or DELETE
in a query, you can read number of affected rows. The rowsAffected
variable is an array of numbers. Each number represents number of affected rows by a single statement.
Example using Promises
const request = request
Example using callbacks
const request = request
Example using streaming
const request = requeststream = truerequestrequest
Version
3.0
JSON support
SQL Server 2016 introduced built-in JSON serialization. By default, JSON is returned as a plain text in a special column named JSON_F52E2B61-18A1-11d1-B105-00805F49916B
.
Example
SELECT 1 AS 'a.b.c', 2 AS 'a.b.d', 3 AS 'a.x', 4 AS 'a.y'FOR JSON PATH
Results in:
recordset = 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B': '{"a":{"b":{"c":1,"d":2},"x":3,"y":4}}'
You can enable built-in JSON parser with config.parseJSON = true
. Once you enable this, recordset will contain rows of parsed JS objects. Given the same example, result will look like this:
recordset = a: b: c: 1 d: 2 x: 3 y: 4
IMPORTANT: In order for this to work, there must be exactly one column named JSON_F52E2B61-18A1-11d1-B105-00805F49916B
in the recordset.
More information about JSON support can be found in official documentation.
Version
2.3
Errors
There are 4 types of errors you can handle:
- ConnectionError - Errors related to connections and connection pool.
- TransactionError - Errors related to creating, committing and rolling back transactions.
- RequestError - Errors related to queries and stored procedures execution.
- PreparedStatementError - Errors related to prepared statements.
Those errors are initialized in node-mssql module and its original stack may be cropped. You can always access original error with err.originalError
.
SQL Server may generate more than one error for one request so you can access preceding errors with err.precedingErrors
.
Error Codes
Each known error has name
, code
and message
properties.
Name | Code | Message |
---|---|---|
ConnectionError |
ELOGIN | Login failed. |
ConnectionError |
ETIMEOUT | Connection timeout. |
ConnectionError |
EDRIVER | Unknown driver. |
ConnectionError |
EALREADYCONNECTED | Database is already connected! |
ConnectionError |
EALREADYCONNECTING | Already connecting to database! |
ConnectionError |
ENOTOPEN | Connection not yet open. |
ConnectionError |
EINSTLOOKUP | Instance lookup failed. |
ConnectionError |
ESOCKET | Socket error. |
ConnectionError |
ECONNCLOSED | Connection is closed. |
TransactionError |
ENOTBEGUN | Transaction has not begun. |
TransactionError |
EALREADYBEGUN | Transaction has already begun. |
TransactionError |
EREQINPROG | Can't commit/rollback transaction. There is a request in progress. |
TransactionError |
EABORT | Transaction has been aborted. |
RequestError |
EREQUEST | Message from SQL Server. Error object contains additional details. |
RequestError |
ECANCEL | Cancelled. |
RequestError |
ETIMEOUT | Request timeout. |
RequestError |
EARGS | Invalid number of arguments. |
RequestError |
EINJECT | SQL injection warning. |
RequestError |
ENOCONN | No connection is specified for that request. |
PreparedStatementError |
EARGS | Invalid number of arguments. |
PreparedStatementError |
EINJECT | SQL injection warning. |
PreparedStatementError |
EALREADYPREPARED | Statement is already prepared. |
PreparedStatementError |
ENOTPREPARED | Statement is not prepared. |
Detailed SQL Errors
SQL errors (RequestError
with err.code
equal to EREQUEST
) contains additional details.
- err.number - The error number.
- err.state - The error state, used as a modifier to the number.
- err.class - The class (severity) of the error. A class of less than 10 indicates an informational message. Detailed explanation can be found here.
- err.lineNumber - The line number in the SQL batch or stored procedure that caused the error. Line numbers begin at 1; therefore, if the line number is not applicable to the message, the value of LineNumber will be 0.
- err.serverName - The server name.
- err.procName - The stored procedure name.
Informational messages
To receive informational messages generated by PRINT
or RAISERROR
commands use:
const request = requestrequest
Structure of informational message:
- info.message - Message.
- info.number - The message number.
- info.state - The message state, used as a modifier to the number.
- info.class - The class (severity) of the message. Equal or lower than 10. Detailed explanation can be found here.
- info.lineNumber - The line number in the SQL batch or stored procedure that generated the message. Line numbers begin at 1; therefore, if the line number is not applicable to the message, the value of LineNumber will be 0.
- info.serverName - The server name.
- info.procName - The stored procedure name.
Version
3.3
Metadata
Recordset metadata are accessible through the recordset.columns
property.
const request = request
Columns structure for example above:
first: index: 0 name: 'first' length: 17 type: sqlDecimal scale: 4 precision: 18 nullable: true caseSensitive: false identity: false readOnly: true second: index: 1 name: 'second' length: 4 type: sqlVarChar nullable: false caseSensitive: false identity: false readOnly: true
Data Types
You can define data types with length/precision/scale:
requestinput"name" sqlVarChar "abc" // varchar(3)requestinput"name" sql "abc" // varchar(50)requestinput"name" sql "abc" // varchar(MAX)request // varchar(8000)request // varchar(3) requestinput"name" sqlDecimal 15533 // decimal(18, 0)requestinput"name" sql 15533 // decimal(10, 0)requestinput"name" sql 15533 // decimal(10, 2) requestinput"name" sqlDateTime2 // datetime2(7)requestinput"name" sql // datetime2(5)
List of supported data types:
sql.Bit
sql.BigInt
sql.Decimal ([precision], [scale])
sql.Float
sql.Int
sql.Money
sql.Numeric ([precision], [scale])
sql.SmallInt
sql.SmallMoney
sql.Real
sql.TinyInt
sql.Char ([length])
sql.NChar ([length])
sql.Text
sql.NText
sql.VarChar ([length])
sql.NVarChar ([length])
sql.Xml
sql.Time ([scale])
sql.Date
sql.DateTime
sql.DateTime2 ([scale])
sql.DateTimeOffset ([scale])
sql.SmallDateTime
sql.UniqueIdentifier
sql.Variant
sql.Binary
sql.VarBinary ([length])
sql.Image
sql.UDT
sql.Geography
sql.Geometry
To setup MAX length for VarChar
, NVarChar
and VarBinary
use sql.MAX
length. Types sql.XML
and sql.Variant
are not supported as input parameters.
SQL injection
This module has built-in SQL injection protection. Always use parameters to pass sanitized values to your queries.
const request = requestinput'myval' sqlVarChar '-- commented'request
Known issues
Tedious
- If you're facing problems with connecting SQL Server 2000, try setting the default TDS version to 7.1 with
config.options.tdsVersion = '7_1'
(issue) - If you're executing a statement longer than 4000 chars on SQL Server 2000, always use batch instead of query (issue)
msnodesqlv8
- msnodesqlv8 has problem with errors during transactions - reported.
- msnodesqlv8 doesn't timeout the connection reliably - reported.
- msnodesqlv8 doesn't support TVP data type.
- msnodesqlv8 doesn't support request timeout.
- msnodesqlv8 doesn't support request cancellation.
- msnodesqlv8 doesn't support detailed SQL errors.
- msnodesqlv8 doesn't support Informational messages.
3.x to 4.x changes
- Library & tests are rewritten to ES6.
Connection
was renamed toConnectionPool
.- Drivers are no longer loaded dynamically so the library is now compatible with Webpack. To use
msnodesqlv8
driver, useconst sql = require('mssql/msnodesqlv8')
syntax. - Every callback/resolve now returns
result
object only. This object containsrecordsets
(array of recordsets),recordset
(first recordset from array of recordsets),rowsAffected
(array of numbers representig number of affected rows by each insert/update/delete statement) andoutput
(key/value collection of output parameters' values). - Affected rows are now returned as an array. A separate number for each SQL statement.
- Directive
multiple: true
was removed. Transaction
andPreparedStatement
internal queues was removed.- ConnectionPool no longer emits
connect
andclose
events. - Removed verbose and debug mode.
- Removed support for
tds
andmsnodesql
drivers. - Removed support for Node versions lower than 4.
Sponsors
Development is sponsored by Integromat.
License
Copyright (c) 2013-2017 Patrik Simek
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.