How to insert into a table with just one IDENTITY column (SQL Express)

11,119
 INSERT INTO dbo.TableWithOnlyIdentity DEFAULT VALUES

This works just fine in my case. How are you trying to get those rows into the database? SQL Server Mgmt Studio? SQL query from .NET app?

Running inside Visual Studio in the "New Query" window, I get:

The DEFAULT VALUES SQL construct or statement is not supported.

==> OK, so Visual Studio can't handle it - that's not the fault of SQL Server, but of Visual Studio. Use the real SQL Management Studio instead - it works just fine there!

Using ADO.NET also works like a charm:

using(SqlConnection _con = new SqlConnection("server=(local);
                             database=test;integrated security=SSPI;"))
{
    using(SqlCommand _cmd = new SqlCommand
            ("INSERT INTO dbo.TableWithOnlyIdentity DEFAULT VALUES", _con))
    {
        _con.Open();
        _cmd.ExecuteNonQuery();
        _con.Close();
    }
}   

Seems to be a limitation of VS - don't use VS for serious DB work :-) Marc

Share:
11,119
Thomas Sandberg
Author by

Thomas Sandberg

Developer at KodeCompagniet

Updated on June 21, 2022

Comments

  • Thomas Sandberg
    Thomas Sandberg about 2 years

    Pretty much the same as this question.

    But I can't seem to get this to work with SQL Server Express in Visual Studio 2008.

    Same Table layout, One column with identity and primary key.

  • Thomas Sandberg
    Thomas Sandberg almost 15 years
    Trying to run it from the visual studio designer(query). get an error message. Don't remember the exact text now.
  • Misko
    Misko almost 15 years
    The table only has 1 column, and it is the identity.
  • Thomas Sandberg
    Thomas Sandberg almost 15 years
    Thanks, this works like a charm in the app. Can't trust VS to do everything... Obviously.