Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert id (autogenerated, only column)

How should my SQL-Statement (MS-SQL) look like if I wan't to insert a row into a table, which contains only one column with the autogenerated id?

Following two queries won't work:

INSERT INTO MyTable (MyTableId) VALUES (Null)
-- or simply
INSERT INTO MyTable 

Thx for any tipps. sl3dg3

like image 808
sl3dg3 Avatar asked Apr 13 '11 14:04

sl3dg3


1 Answers

INSERT INTO MyTable (MyTableId) VALUES (Null) implicitly tries to insert a null into the identity column, so that won't work as identity columns may never be nullable and without a SET option you cannot insert an arbitrary value anyway, instead you can use:

INSERT INTO MyTable DEFAULT VALUES
like image 128
Alex K. Avatar answered Sep 22 '22 12:09

Alex K.