2

I tried the execute method:

const sql = require('../node_modules/mssql');
var dbname = 'AddressBook';
sql.connect('mssql://sa:1234@localhost/').then(pool => {
  return pool.request().input('db_name', sql.TYPES.Text, dbname).query`select db_id(@db_name) as idn`.then(result => {
    if (result[0].idn === null) {
      return pool.request().input('db_name', sql.TYPES.Text, dbname).execute`create database @db_name`;
    }
  }).catch(err => {throw(err)});
}).catch(err => console.log(err));

I get:

message: 'The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. The RPC name is invalid.',

Also tried the query method:

      return pool.request().input('db_name', sql.TYPES.Text, dbname).query`create database @db_name`;

I get

message: 'Incorrect syntax near \'@db_name\'.',

Is there a different method or am I missing something?

MotKohn
  • 3,485
  • 1
  • 24
  • 41
  • 1
    this works `return pool.request().query(\`create database ${dbname}\`);` - I think you're overusing tagged templates :p – Jaromanda X Jun 06 '17 at 04:22
  • @JaromandaX Thank you very much, it worked. But what's the difference between what I tried and yours? – MotKohn Jun 06 '17 at 04:42
  • firstly, `create database 'AddressBook'` (which is what your code would possibly try) is invalid, it's `create database AddressBook` (without the quotes) - secondly, the tagged template syntax makes the request something even less correct - I don't fully understand tagged templates, so I can't explain it – Jaromanda X Jun 06 '17 at 04:45
  • yep, removed the `'` from the "correct" version :p sorry, copy/paste laziness – Jaromanda X Jun 06 '17 at 04:48
  • @JaromandaX Thank you – MotKohn Jun 06 '17 at 04:50

0 Answers0