2

Hi I'm using the node package mssql, I'm adding a new table like this:

var companyTable = new sql.Table("companies2");
companyTable.create = true;
companyTable.columns.add('id', sql.Int);
companyTable.columns.add('company_name', sql.VarChar(100));
companyTable.rows.add(sql.Int, 'Comapny1');
companyTable.rows.add(sql.Int, 'Comapny2');
var createTable = new sql.Request();
await createTable.bulk(companyTable);

I would like the ID column to be an Identity column Does anyone know if it's possible?

mpr
  • 3,250
  • 26
  • 44

1 Answers1

0

It is a bit tricky. If your id is an identity and your company_name is not null, you'd want the following:

var companyTable = new sql.Table("companies2");
companyTable.create = true;
companyTable.columns.add('company_name', sql.VarChar, {nullable: false});
companyTable.rows.add('Comapny1');
companyTable.rows.add('Comapny2');
var createTable = new sql.Request();
await createTable.bulk(companyTable);

For us, the necessity of adding:

nullable: false

...was what was missing.

If you do need to supply the id, just add it to the list of params to rows.add:

companyTable.rows.add(1, 'Company1');
mpr
  • 3,250
  • 26
  • 44