A simple way to connect to Microsoft SQL (MS-SQL) databases from Node.js.
Run queries or stored procedures. Based on tedious by Mike D Pilsbury.
npm install node-mssql-connector
MSSQLConnector = ;MSSQLClient =settings:max: 20min: 0idleTimeoutMillis: 30000detailerror: true # To show detail error informationconnection:userName: ""password: ""server: ""options:database: ""
# Initiliazed clientMSSQLClient;
First define the query statement.
query = MSSQLClient
Set the parameters. Values with leading
@ are defined as parameters.
If there aren't any parameter you do not need the following line.
Important You have to use the correct datatype from tedious. They can be found here (column "Constant"): http://pekim.github.io/tedious/api-datatypes.html
Execute the query. The result will be returned as an
Array with an
Object for each result.
You can also add a list of (Array) parameters into one param statement, e.g.
query = MSSQLClient.query( " SELECT ID, Name, Lastname FROM Table WHERE id IN (@idlist) ") query.param( "idlist", "Int", [23,34,67] )
Check that every parameter in this array has the given type.
The stored procedure in database looks like:
CREATE PROCEDURE [dbo].[sp_demo]@ID Int,@Total Tinyint = 0 Output,@Teststring Varchar(100) = 'Default' OutputASSET @Teststring = "Modifed"SET @Total = 100SELECT ID, Name, LastnameFROM TableWHERE ID = @id
The difference to SQL Statment is that you have to call the
storedprod method of MSSQLClient. If your stored procedure has output params (like in this example) then you must define this via
storedprod = MSSQLClientstoredprodstoredprodstoredprodstoredprod
For recent changes please see the Changelog.
To run a local (test) enviroment for mssql use the awesome tool from fgrehm via Virtual Box: https://github.com/fgrehm/vagrant-mssql-express
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.