Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Planning your SQL Server mirroring landscapeDebugging 64 bit SSIS packages »
    comments

    How to insert values into a table with only an identity column

    Some databases have something called a sequence, you can use this to generate values like an identity but it can be used accross many tables. In SQL Server you could simulate that by creating a table that has an identity column, inserting into that table and then use the identity generated in your other tables
    The reason I am writing this is because I saw the following table

    1. CREATE TABLE Sequence (ID int identity not null primary key,Dummy tinyint)
    2. GO

    I was thinking to myself why they have that dummy value, so I spoke to one of these people who were using the system. The reason they have this dummy value is so that they can insert into this table like this

    1. INSERT INTO Sequence VALUES(1)
    2. INSERT INTO Sequence VALUES(1)
    3. INSERT INTO Sequence VALUES(1)

    And now when you query the table

    1. SELECT ID
    2. FROM  Sequence

    You get these 3 rows as output

    ID
    1
    2
    3

    So the dummy value is there so that they can insert into the table. Even though the dummy column is not big it still takes up space and it was also nullable which also takes up some space.
    Let's do this a different way. First drop the table we created before.

    1. DROP TABLE Sequence

    Now create the table like this; without the dummy value

    1. CREATE TABLE Sequence (ID int identity not null primary key)
    2. GO

    Here is how the insert looks like if you only have the identity column

    1. INSERT INTO Sequence DEFAULT VALUES
    2. INSERT INTO Sequence DEFAULT VALUES
    3. INSERT INTO Sequence DEFAULT VALUES
    4. INSERT INTO Sequence DEFAULT VALUES

    All you need to use is default values in the insert statements, it will then generate the identity

    1. SELECT ID
    2. FROM  Sequence
    ID
    1
    2
    3
    4

    Let's take a look at another example, what if we had two other columns and they had defaults on them? First create this table.

    1. CREATE TABLE Sequence2 (ID int identity not null primary key,
    2.             Somedate datetime default getdate() not null,
    3.             SomeID int default 0 not null)
    4. GO

    Now run these statements

    1. INSERT INTO Sequence2 DEFAULT VALUES
    2. INSERT INTO Sequence2 DEFAULT VALUES
    3. INSERT INTO Sequence2 DEFAULT VALUES
    4. INSERT INTO Sequence2 DEFAULT VALUES

    Now let's look what is in the table

    1. SELECT * FROM  Sequence2
    ID	Somedate	SomeID
    1	2010-04-30 12:50:14.693	0
    2	2010-04-30 12:50:14.693	0
    3	2010-04-30 12:50:14.693	0
    4	2010-04-30 12:50:14.693	0

    As you can see it used the default values for the columns with defaults and also created the identity values.




    *** Remember, if you have a SQL related question, try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    1857 views
    how to, identity, tip
    InstapaperVote on HN

    3 comments

    Arthur A nice approach, till now have never used the
    INSERT INTO MyTable DEFAULT VALUES
    Thank you
    04/30/10 @ 13:08
    Comment from: chopstik [Member]
    chopstik Nice post, Denis. I'd never even heard of Default values until this post. I'll have to bear this in mind for future reference. Thanks!
    05/01/10 @ 16:29
    Comment from: David Forck (thirster42) [Member]
    awesome.
    05/02/10 @ 19:17

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)