Search code examples
t-sqlinserttemp-tables

Insert into sql temp table with a default column


I'm creating a temp table in tsql with 1 column having a default value as below:

DECLARE @MyTable Table (MyName varchar(40) Primary Key not null, Updated SmallInt NULL DEFAULT 0)

When I run an insert statement, I get an error that "Column name or number of supplied values does not match table definition.". But the second column is suppose to get the default value and I do not want to specify that. Any workarounds?

INSERT @MyTable 
VALUES ('Value1'),
       ('Value2')

Solution

  • You need to specify the columns that get a value and leave away those that do not:

    INSERT @MyTable (MyName) VALUES ('Value1')