How to use mssql - 10 common examples

To help you get started, we’ve selected a few mssql examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
autostart: true
    },
    beforeConnect: conn => {
        conn.on('debug', message => console.info(message));
        conn.on('error', err => console.error(err));
        conn.removeAllListeners();
    }
}

var connectionStringTest: sql.ConnectionPool = new sql.ConnectionPool("connectionstring", (err) => {
    if (err) {
        return err;
    }
});

var connection: sql.ConnectionPool = new sql.ConnectionPool(config, function (err: any) {
    if (err != null) {
        console.warn("Issue with connecting to SQL Server!");
    }
    else {
        connection.query`SELECT ${1} as value`.then(res => { });
        var requestQuery = new sql.Request(connection);

        var getArticlesQuery = "SELECT * FROM TABLE";

        requestQuery.query(getArticlesQuery, function (err, result) {
            if (err) {
                console.error(`Error happened calling Query: ${err.name} ${err.message}`);
            }
            // checking to see if the articles returned as at least one.
            else if (result.recordset.length > 0) {
            }
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
function test_promise_returns() {
    // Methods return a promises if the callback is omitted.
    var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
    connection.connect().then(() => { });
    connection.close().then(() => { });
    connection.query('SELECT 1').then((recordset) => { });
    connection.query('SELECT 1 as value').then(res => { });
    connection.query`SELECT ${1}`.then((recordset) => { });
    connection.batch('create procedure #temporary as select * from table').then((recordset) => { });
    connection.batch('create procedure #temporary as select * from table;select 1 as value').then((recordset) => { });
    connection.batch`create procedure #temporary as select ${1} from table`.then((recordset) => { });
    connection.batch`create procedure #temporary as select ${1} from table`.then((recordset) => { });

    var preparedStatment = new sql.PreparedStatement(connection);
    preparedStatment.prepare("SELECT @myValue").then(() => { });
    preparedStatment.execute({ myValue: 1 }).then((recordSet) => { });
    preparedStatment.unprepare().then(() => { });

    var transaction = new sql.Transaction(connection);
    transaction.begin().then(() => { });
    transaction.commit().then(() => { });
    transaction.rollback().then(() => { });

    var request = new sql.Request();
    request.batch('create procedure #temporary as select * from table;select 1 as value').then((recordset) => { });
    request.batch('create procedure #temporary as select * from table;select 1 as value').then((recordset) => { });
    request.batch`create procedure #temporary as select * from table;select ${1} as value`.then((recordset) => { });
    request.batch`create procedure #temporary as select * from table;select ${1} as value`.then((recordset) => { });
    request.bulk(new sql.Table("table_name")).then(() => { });
    request.query('SELECT 1').then((recordset) => { });
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
else {
                console.info(returnValue);
            }
        });

        var requestStoredProcedureWithOutput = new sql.Request(connection);
        var testId: number = 0;
        var testString: string = 'test';

        requestStoredProcedureWithOutput.input("name", sql.VarChar, "abc");               // varchar(3)
        requestStoredProcedureWithOutput.input("name", sql.VarChar(50), "abc");           // varchar(MAX)
        requestStoredProcedureWithOutput.output("name", sql.VarChar);                     // varchar(8000)
        requestStoredProcedureWithOutput.output("name", sql.VarChar, "abc");              // varchar(3)

        requestStoredProcedureWithOutput.input("name", sql.Decimal, 155.33);              // decimal(18, 0)
        requestStoredProcedureWithOutput.input("name", sql.Decimal(10), 155.33);          // decimal(10, 0)
        requestStoredProcedureWithOutput.input("name", sql.Decimal(10, 2), 155.33);       // decimal(10, 2)

        requestStoredProcedureWithOutput.input("name", sql.DateTime2, new Date());        // datetime2(7)
        requestStoredProcedureWithOutput.input("name", sql.DateTime2(5), new Date());     // datetime2(5)

        requestStoredProcedure.execute('StoredProcedureName', function (err, recordsets, returnValue) {
            if (err != null) {
                console.error(`Error happened calling Query: ${err.name} ${err.message}`);
            }
            else {
                console.info(requestStoredProcedureWithOutput.parameters['output'].value);
            }
        });

        requestStoredProcedure.execute('StoredProcedureName', function (err, recordsets, returnValue) {
            if (err != null) {
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
console.info(returnValue);
            }
        });

        var requestStoredProcedureWithOutput = new sql.Request(connection);
        var testId: number = 0;
        var testString: string = 'test';

        requestStoredProcedureWithOutput.input("name", sql.VarChar, "abc");               // varchar(3)
        requestStoredProcedureWithOutput.input("name", sql.VarChar(50), "abc");           // varchar(MAX)
        requestStoredProcedureWithOutput.output("name", sql.VarChar);                     // varchar(8000)
        requestStoredProcedureWithOutput.output("name", sql.VarChar, "abc");              // varchar(3)

        requestStoredProcedureWithOutput.input("name", sql.Decimal, 155.33);              // decimal(18, 0)
        requestStoredProcedureWithOutput.input("name", sql.Decimal(10), 155.33);          // decimal(10, 0)
        requestStoredProcedureWithOutput.input("name", sql.Decimal(10, 2), 155.33);       // decimal(10, 2)

        requestStoredProcedureWithOutput.input("name", sql.DateTime2, new Date());        // datetime2(7)
        requestStoredProcedureWithOutput.input("name", sql.DateTime2(5), new Date());     // datetime2(5)

        requestStoredProcedure.execute('StoredProcedureName', function (err, recordsets, returnValue) {
            if (err != null) {
                console.error(`Error happened calling Query: ${err.name} ${err.message}`);
            }
            else {
                console.info(requestStoredProcedureWithOutput.parameters['output'].value);
            }
        });

        requestStoredProcedure.execute('StoredProcedureName', function (err, recordsets, returnValue) {
            if (err != null) {
                console.error(`Error happened calling Query: ${err.name} ${err.message}`);
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
function test_promise_returns() {
    // Methods return a promises if the callback is omitted.
    var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
    connection.connect().then(() => { });
    connection.close().then(() => { });
    connection.query('SELECT 1').then((recordset) => { });
    connection.query('SELECT 1 as value').then(res => { });
    connection.query`SELECT ${1}`.then((recordset) => { });
    connection.batch('create procedure #temporary as select * from table').then((recordset) => { });
    connection.batch('create procedure #temporary as select * from table;select 1 as value').then((recordset) => { });
    connection.batch`create procedure #temporary as select ${1} from table`.then((recordset) => { });
    connection.batch`create procedure #temporary as select ${1} from table`.then((recordset) => { });

    var preparedStatment = new sql.PreparedStatement(connection);
    preparedStatment.prepare("SELECT @myValue").then(() => { });
    preparedStatment.execute({ myValue: 1 }).then((recordSet) => { });
    preparedStatment.unprepare().then(() => { });

    var transaction = new sql.Transaction(connection);
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
function test_prepared_statement_constructor() {
    // Request can be constructed with a connection, preparedStatment, transaction or no arguments
    var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
    var preparedStatment = new sql.PreparedStatement(connection);
    var transaction = new sql.Transaction(connection);

    var preparedSatement1 = new sql.PreparedStatement(connection);
    var preparedSatement2 = new sql.PreparedStatement(transaction);
}
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
function test_prepared_statement_constructor() {
    // Request can be constructed with a connection, preparedStatment, transaction or no arguments
    var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
    var preparedStatment = new sql.PreparedStatement(connection);
    var transaction = new sql.Transaction(connection);

    var preparedSatement1 = new sql.PreparedStatement(connection);
    var preparedSatement2 = new sql.PreparedStatement(transaction);
}
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
function test_prepared_statement_constructor() {
    // Request can be constructed with a connection, preparedStatment, transaction or no arguments
    var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
    var preparedStatment = new sql.PreparedStatement(connection);
    var transaction = new sql.Transaction(connection);

    var preparedSatement1 = new sql.PreparedStatement(connection);
    var preparedSatement2 = new sql.PreparedStatement(transaction);
}
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
function test_request_constructor() {
    // Request can be constructed with a connection, preparedStatment, transaction or no arguments
    var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
    var preparedStatment = new sql.PreparedStatement(connection);
    var transaction = new sql.Transaction(connection);

    var request1 = new sql.Request(connection);
    var request2 = new sql.Request(preparedStatment);
    var request3 = new sql.Request(transaction);
    var request4 = new sql.Request();
}
github DefinitelyTyped / DefinitelyTyped / types / mssql / mssql-tests.ts View on Github external
function test_prepared_statement_constructor() {
    // Request can be constructed with a connection, preparedStatment, transaction or no arguments
    var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
    var preparedStatment = new sql.PreparedStatement(connection);
    var transaction = new sql.Transaction(connection);

    var preparedSatement1 = new sql.PreparedStatement(connection);
    var preparedSatement2 = new sql.PreparedStatement(transaction);
}