0

I was wondering how to I select a specific column to save to a variable in my function. I need both values which is why I have a query getting both at once. I'm using Node JS with an Azure SQL Database and I'm using tedious for my SQL NPM plugin. I'm fairly new to this so any help would be appreciated. Thanks!

Side note: the request "on done" at the very bottom that returns the row count isn't doing anything. If anyone knows why I'd love to know, it's not important but I'm just curious.

function Charge(chipId, LocID) {
        request = new Request("SELECT car_id, userOwner FROM Cars WHERE carChipId = '" + chipId + "'", function (err) {
            if (err) {
                console.log(err);
            }
        });
        var result = "";
        request.on('row', function (columns) {
            columns.forEach(function (column) {
                if (column.value === null) {
                    console.log('NULL');
                } else {
                    result += column.value + " ";
                }
            });
            console.log(result);
            result = "";
        });

        request.on('done', function (rowCount, more) {
            console.log(rowCount + ' rows returned');
        });
        connection.execSql(request);
    }  

1 Answer 1

2

Trying the following to select a specific column saved to the variable:

function charge(chipId) {
  var carId = '';
  var userOwner = '';

  request = new Request(`SELECT car_id, userOwner FROM Cars WHERE carChipId = '${chipId}'`, function(err) {
    if (err) {
      console.log(err);
    }
  });

  request.on('row', function(columns) {

    carId = columns[0].value;
    userOwner = columns[1].value;
    console.log('carId: ', carId);
    console.log('userOwner: ', userOwner);
  });

  connection.execSql(request);
}

Another option is that you can set options.useColumnNames as true (Default: false) to return rows as key-value collections. Below is the code example.

var config = {
  userName: '<userName>',
  password: '<password>',
  server: '<serverName>.database.windows.net',

  options: {
    encrypt: true,
    database: '<database>',
    useColumnNames: true
  }
};

var connection = new Connection(config);

connection.on('connect', function(err) {
  charge('<chipId>');
});

function charge(chipId) {
  var carId = '';
  var userOwner = '';

  request = new Request(`SELECT car_id, userOwner FROM Cars WHERE carChipId = '${chipId}'`, function(err) {
    if (err) {
       console.log(err);
    }
  });

  request.on('row', function(columns) {
    carId = columns.car_id.value;
    userOwner = columns.userOwner.value;
    console.log('carId: ', carId, ' userOwner: ', userOwner);
  });

  connection.execSql(request);
}

If you wanna Event: done be triggered, please use connection.execSqlBatch(request) instead of connection.execSql(request). For details, please refer to http://tediousjs.github.io/tedious/api-connection.html#function_execSqlBatch.

Sign up to request clarification or add additional context in comments.

4 Comments

I also can't seem to do more than one request from a single connection.on('connect', for whatever reason, is there something else I should know?
There is some documentation at tediousjs.github.io/tedious/api-connection.html that states that this is not supported. Only one request at a time may be executed on a connection. You can use mutiple connections instead. This post may be helpful.
Ok, I made another post about this topic would you mind reviewing my situation to make sure it would work?
I looked into this and I will share details after lunch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.