-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquerySQL.js
52 lines (46 loc) · 1.5 KB
/
querySQL.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
// Create connection to database
var config =
{
userName: '', // update me ** NEEDS TO BE SET IN VAR
password: '', // update me ** NEEDS TO BE SET IN VAR
server: '', // update me ** NEEDS TO BE SET IN VAR
options:
{ encrypt: true
}
}
var connection = new Connection(config);
// Attempt to connect and execute queries if connection goes through
connection.on('connect', function(err)
{
if (err)
{
console.log(err)
}
else
{
queryDatabase()
}
}
);
// Query Database for OrgId and Public Address
function queryDatabase()
{ console.log('Reading rows from the Table...');
// Read all rows from table
request = new Request(
"SELECT * FROM [KeyTable];",
//"SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid",
function(err, rowCount, rows)
{
console.log(rowCount + ' row(s) returned');
process.exit();
}
);
request.on('row', function(columns) {
columns.forEach(function(column) {
console.log("%s\t%s", column.metadata.colName, column.value);
});
});
connection.execSql(request);
}