'Add Identity column with npm mssql
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?
Solution 1:[1]
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');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mpr |
