Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

INSERT INTO auto increment identity for a one-column table

I have a table Person.

Id, Name, Phone
-------------------
1,  NULL, 123456789

This Person.Id is an auto increment identity. After a while, I found Name is useless for the current business and it always is NULL. So I deleted this column.

Later, I found one person can have multiple phone numbers. And since SQL Server doesn’t support array type, I had to move this Phone to a separated table. So, there is just one column Id left.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is a one column table good design?

I have enough reason keep this one-column table, because the Id is important for other tables (link).

But there is a problem for the INSERT query.

INSERT INTO Person(Id) OUTPUT Inserted.Id VALUES(DEFAULT)

The value either DEFAULT or NULL wouldn’t work with the error message:

DEFAULT or NULL are not allowed as explicit identity values.

Leave the parentheses empty also doesn’t work.

>Solution :

Instead of VALUES(DEFAULT), omit the column name and specify DEFAULT VALUES to assign defaults for all columns (only column in this case). Example with the OUTPUT clause:

CREATE TABLE dbo.Person(
    ID int NOT NULL IDENTITY CONSTRAINT PK_Person PRIMARY KEY
);
INSERT INTO Person OUTPUT Inserted.Id DEFAULT VALUES;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading