How to use the Request function from mssql

Find comprehensive JavaScript mssql.Request code examples handpicked from public code repositorys.

mssql.Request is an object that represents a SQL Server request.

151
152
153
154
155
156
157
158
159
160
        resultsSent = true;
        callback(queryError, results);
    }
};

var request = new mssql.Request(mssqlConnection);
request.query(query);
request.on('recordset', function (columns) {
    // Emitted once for each recordset in a query
    results.columns = columns;
fork icon759
star icon0
watch icon30

191
192
193
194
195
196
197
198
199
200
_getData: function(id, connstr, password, query, callback, err_callback){
    var client = this.getInfoClient(id, connstr, password, err_callback);
    if (typeof(client) == 'undefined'){
        return err_callback(id, 'Failed to get client');
    }
    var request = new mssql.Request(client);

    var sendRequest = function(){
        request.query(query, function(err, result) {
            if (err) {
fork icon61
star icon812
watch icon40

+ 22 other calls in file

How does mssql.Request work?

mssql.Request is a class in the mssql module that represents a SQL Server request and provides methods for building and executing SQL queries against the database. It encapsulates a SQL Server connection, allowing developers to send queries and retrieve data from the database. The class provides methods to execute stored procedures, prepare and execute SQL statements, and handle input/output parameters. It also provides support for batch queries and stream data retrieval. The class is designed to be used in conjunction with a Connection object, which represents a connection to a SQL Server instance.

28
29
30
31
32
33
34
35
36
37
 * @param {string} database
 * @param {mssql.ConnectionPool} client
 */
function getMigratorConfig (database, client) {
  const asyncExecQuery = async function (query) {
    const request = new mssql.Request(client)
    const goStatementRegex = new RegExp(/^\s*GO\s*$/im)

    if (query.match(goStatementRegex) !== null) {
      const batches = query.split(goStatementRegex)
fork icon14
star icon12
watch icon11

69
70
71
72
73
74
75
76
77
const maxRows = resolvePositiveNumber(
  connection.maxrows_override,
  connection.maxRows
);

const request = new mssql.Request(pool);
// Stream set a config level doesn't seem to work
request.stream = true;
request.query(query);
fork icon759
star icon0
watch icon160

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const sql = require("mssql");

const config = {
  user: "yourusername",
  password: "yourpassword",
  server: "yourserver",
  database: "yourdatabase",
  options: {
    encrypt: true, // If you're connecting to Azure SQL Database
  },
};

async function getEmployees() {
  try {
    await sql.connect(config);
    const request = new sql.Request();
    const result = await request.query("SELECT * FROM Employees");
    console.log(result);
  } catch (err) {
    console.error(err);
  }
}

getEmployees();

In this example, we're using mssql.Request to create a new request object, then we're using that request object to execute a query to select all employees from a table named Employees. Finally, we're logging the results to the console.

89
90
91
92
93
94
95
96
97
98
sql.connect(config, function(err) {
    // ... error checks
        
    // Query
        
    var request = new sql.Request();
    request.query('select 1 as number', function(err, recordset) {
        // ... error checks

        console.dir(recordset);
fork icon463
star icon0
watch icon78

+ 19 other calls in file

46
47
48
49
50
51
52
53
54
55
var connection = new sql.Connection(config, function(err) {
    // ... error checks
    
    // Query
        
    var request = new sql.Request(connection); // or: var request = connection.request();
    request.query('select 1 as number', function(err, recordset) {
        // ... error checks
        
        console.dir(recordset);
fork icon463
star icon0
watch icon0

+ 19 other calls in file

216
217
218
219
220
221
222
223
224
225
sql.connect(config, err => {
    // ... error checks

    // Query

    new sql.Request().query('select 1 as number', (err, result) => {
        // ... error checks

        console.dir(result)
    })
fork icon463
star icon0
watch icon3

+ 3 other calls in file

46
47
48
49
50
51
52
53
54
55

async function executeQuery (q, opts = {}) {
  const connection = pool

  async function execute (connection) {
    const request = new sql.Request(connection)

    for (let i = 0; i < q.values.length; i++) {
      request.input(i + 1 + '', q.values[i])
    }
fork icon202
star icon0
watch icon56

+ 3 other calls in file

127
128
129
130
131
132
133
134
135
136
    data = data.recordset;
  }
  callback && callback(err, data);
};

const request = new mssql.Request(connection);
// Allow multiple result sets
if (options.multipleResultSets) {
  request.multiple = true;
}
fork icon84
star icon51
watch icon42

+ 15 other calls in file

92
93
94
95
96
97
98
99
100
    var param = cfg.parameters[i];
    var value = formatParamValue(param, loggingEvent);
    query = query.replace(param.name, value);
}
// Fire up the request and log possible errors
new mssql.Request().query(query).then(function (recordset) {
}).catch(function (err) {
    console.log('request failed asd: ' + err);
})
fork icon2
star icon1
watch icon1

+ 11 other calls in file

28
29
30
31
32
33
34
35
36
37
    var connection = sql.connect(conStr, function(err) {
        if(err){
            defer.reject({code:500, message: utils.format('Error connecting to Sql Server: {0}, database: {1}', datasource, database)});
        }
        else{
            defer.resolve(new sql.Request(connection));
        }
    });
    return defer.promise;
};
fork icon9
star icon13
watch icon11

+ 15 other calls in file

119
120
121
122
123
124
125
126
127
128

// loop through all MongoDB documents and parse them into the correct format for inserting into SQL
for (let doc of mongoResult) {
        let query = "";
        try {
                let r = await new mssql.Request(transaction);
                query = parseDoc(doc);
                let result = await r.query(query);
                x++;
                process.stdout.write("\r\x1b[K");
fork icon5
star icon3
watch icon0

+ 41 other calls in file

72
73
74
75
76
77
78
79
80
81
    });
  });
}

get(key, callback) {
  const request = new mssql.Request(this.db);

  request.input('key', mssql.NVarChar(100), key);

  request.query('SELECT [value] FROM [store] WHERE [key] = @key', (err, results) => {
fork icon89
star icon256
watch icon19

+ 47 other calls in file

193
194
195
196
197
198
199
200
201
                for (let i = 0; i < outputs.length; i++) request.input(outputs[i]);
                return request.query(sql)
                        .then(result => Promise.resolve(result))
                        .catch(error => Promise.reject(error));
        }
        return new mssql.Request().query(sql)
                .then(result => Promise.resolve(result))
                .catch(error => Promise.reject(error));
}
fork icon56
star icon0
watch icon2

+ 7 other calls in file

61
62
63
64
65
66
67
68
69
70
    leaCode++
  }
  const dfeNumber = `${leaCode}${estabBase}`
  table.rows.add(leaCode, estabBase++, `bulk school ${(idx - schoolOffset) + 1}`, urnBase++, dfeNumber)
}
const schoolInsertRequest = new sql.Request(pool)
const start = performance.now()
try {
  await schoolInsertRequest.bulk(table)
} catch (error) {
fork icon14
star icon12
watch icon11

+ 15 other calls in file

2
3
4
5
6
7
8
9
10
11
const axios = require("axios");
const TurndownService = require('turndown');

async function main() {
  var sqlconnection = await sql.connect(database_config);
  const db_request = new sql.Request();

  const [stackData, dbData, caiCredentials] = await Promise.all([get_stackQuestions(), get_dbData(db_request), get_caiCredentials()]);
  var update_all_questions = false;
  if ('UPDATE_ALL' in process.env && process.env.UPDATE_ALL == 'Y') {
fork icon67
star icon145
watch icon18

+ 5 other calls in file

1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
	item.first = isFIRST(item.$query);

(Agent.debug || self.debug) && console.log(self.debugname, item.name, item.$query);
self.$events.query && self.emit('query', item.name, item.$query, item.$params);

var request = new database.Request(self.$transaction ? self.$transaction : self.db);
item.$params && prepare_params_request(request, item.$params);

request.query(item.$query, function(err, rows) {
	self.$bind(item, err, rows ? rows.recordset : []);
fork icon22
star icon48
watch icon0

71
72
73
74
75
76
77
78
79
80
    }
  }, function defineConnectionCallback (err) {
    if (err) {
      cb(err)
    } else {
      request = new sql.Request(connection)
      cb()
    }
  })
},
fork icon9
star icon0
watch icon53

+ 33 other calls in file

115
116
117
118
119
120
121
122
123
124
  throw err
}

items.forEach(function (item) {
  console.log('Upserting question_id: %s, title: %s', item.question_id, item.title)
  var request = new sql.Request(connection)
  request.input('question_id', sql.Int, item.question_id)
  request.input('link', sql.NVarChar, item.link)
  request.input('title', sql.NVarChar, item.title)
  request.input('creation_date', sql.DateTime, moment.unix(item.creation_date).toDate())
fork icon7
star icon4
watch icon2

43
44
45
46
47
48
49
50
51
52
	"../migrations/mssql/*"
),
driver: "mssql",
database: client.config.database,
execQuery: /* istanbul ignore next */ async (query) => {
	const request = new mssql.Request(client);
	const result = await request.batch(query);

	return {
		rows: result.recordset ? result.recordset : result,
fork icon2
star icon11
watch icon0