Here is a simplified version of the table I am looking at:
CREATE TABLE [dbo].[FrustratingTable] ( [Id] Uniqueidentifier NOT NULL , [SecondField] [datetime] , [ThirdField] varchar(128) ) I want to insert new records into this table. I have tried 3 approaches:
INSERT INTO [dbo].[FrustratingTable] (Id, SecondField, ThirdField) SELECT newid() as Id, '6/25/2015' as SecondField, 'Example' as ThirdField This approach inserts, but the resulting key isn't a nice sequential GUID like the other ones in the table
INSERT INTO [dbo].[FrustratingTable] (Id, SecondField, ThirdField) SELECT NEWSEQUENTIALID() as Id, '6/25/2015' as SecondField, 'Example' as ThirdField This fails with error
The newsequentialid() built-in function can only be used in a DEFAULT expression for a column of type 'uniqueidentifier' in a CREATE TABLE or ALTER TABLE statement. It cannot be combined with other operators to form a complex scalar expression.
INSERT INTO [dbo].[FrustratingTable] (SecondField,ThirdField) SELECT '6/25/2015' as SecondField, 'Example' as ThirdField This fails with the error
Cannot insert the value NULL into column 'id', table 'mydatabase.dbo.frustratingtable'; column does not allow nulls. INSERT fails.
Is it possible to solve this without altering the table definition?
newId()?newsequentialid()can only be used in aDEFAULTconstraint, as your error message and the MSDN documentation states. So no - without changing your table structure, you cannot fix this