Monday 5 June 2017

Using node mssql how do I create a database?

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?



via MotKohn

No comments:

Post a Comment